forked from kacperkan/conerf-kubric-dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trio_top.py
196 lines (169 loc) · 5.21 KB
/
trio_top.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import logging
import numpy as np
import kubric as kb
from kubric.renderer.blender import Blender as KubricBlender
logging.basicConfig(level="INFO") # < CRITICAL, ERROR, WARNING, INFO, DEBUG
world_matrix = {
"bunny": np.array(
(
(-1.0, 3.2584136988589307e-07, 0.0, 0.7087775468826294),
(-3.2584136988589307e-07, -1.0, 0.0, -1.2878063917160034),
(0.0, 0.0, 1.0, 0.0),
(0.0, 0.0, 0.0, 1.0),
),
),
"suzanne": np.array(
(
(1.0, 0.0, 0.0, -0.8567398190498352),
(0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, 0.0, 0.0, 1.0),
)
),
"teapot": np.array(
(
(1.0, 0.0, 0.0, -0.9078792333602905),
(0.0, 1.0, 0.0, 1.2115877866744995),
(0.0, 0.0, 1.0, 0.0),
(0.0, 0.0, 0.0, 1.0),
)
),
}
points = {
"bunny": np.array(
(
(
0.044713765382766724,
-1.0193415880203247,
0.8044384121894836,
1.0,
),
(
0.056191492825746536,
-0.31232786178588867,
0.8044384121894836,
1.0,
),
(0.0, 0.0, 0.0, 1.0),
(1.0, 0.0, 0.0, 1.0),
),
),
"suzanne": np.array(
(
(-1.0, 0.0, 0.0, 1.0),
(-0.2928931713104248, 2.9802322387695312e-08, 0.0, 1.0),
(0.0, 0.0, 0.0, 1.0),
(1.0, 0.0, 0.0, 1.0),
)
),
"teapot": np.array(
(
(
0.044713765382766724,
-1.0193415880203247,
0.8044384121894836,
1.0,
),
(
0.056191492825746536,
-0.31232786178588867,
0.8044384121894836,
1.0,
),
(0.0, 0.0, 0.0, 1.0),
(1.0, 0.0, 0.0, 1.0),
),
),
}
def interpolate_position(
t: np.ndarray, handles: np.ndarray, world_matrix: np.ndarray
) -> np.ndarray:
p0, p1, p2, p3 = handles[:, np.newaxis]
t = t[..., np.newaxis]
r = 1 - t
out = r ** 3 * p0 + 3 * r ** 2 * t * p1 + 3 * r * t ** 2 * p2 + t ** 3 * p3
out = out / out[..., [-1]]
return (world_matrix @ out.T).T[..., :-1]
# --- create scene and attach a renderer and simulator
num_frames = 480
scene = kb.Scene(resolution=(256, 256), background=kb.get_color("white"))
scene.frame_end = num_frames # < numbers of frames to render
scene.frame_rate = 24 # < rendering framerate
scene.ambient_illumination = kb.Color(0.05, 0.05, 0.05)
renderer = KubricBlender(scene)
# --- populate the scene with objects, lights, cameras
rng = np.random.RandomState(0)
wall_material = kb.FlatMaterial(
color=kb.get_color("white"), indirect_visibility=True
)
bunny = kb.FileBasedObject(
render_filename="objects/bunny.obj",
name="bunny",
scale=(4.89, 4.89, 4.89),
position=(0, -1, -0.47044),
quaternion=(0.0, 0.0, 0.707, 0.707),
material=kb.PrincipledBSDFMaterial(color=kb.random_hue_color(rng=rng)),
)
suzanne = kb.FileBasedObject(
render_filename="objects/suzanne.obj",
name="suzanne",
scale=(0.316, 0.316, 0.316),
position=(0, 0, 0.001821),
quaternion=(0.5, 0.5, 0.5, 0.5),
material=kb.PrincipledBSDFMaterial(color=kb.random_hue_color(rng=rng)),
)
teapot = kb.FileBasedObject(
render_filename="objects/teapot.obj",
name="teapot",
scale=(0.19, 0.19, 0.19),
position=(0, 1, -0.28363),
quaternion=(0.707, 0.70, 0.0, 0.0),
material=kb.PrincipledBSDFMaterial(color=kb.random_hue_color(rng=rng)),
)
scene += bunny
scene += suzanne
scene += teapot
scene += kb.Cube(
scale=(0.1, 100, 100),
position=(-4, 0, 0),
material=wall_material,
static=True,
background=True,
)
scene += kb.DirectionalLight(
name="sun", position=(4, 0, 3), look_at=(0, 0, 0), intensity=1.5
)
camera = kb.PerspectiveCamera(
name="camera",
position=(0, 0.0, 6.0),
quaternion=(1.0, 0.0, 0.0, 1.0),
)
scene.camera = camera
xs = np.linspace(-np.pi / 2, np.pi / 2, num_frames)
positions = {
"bunny": interpolate_position(
np.abs(np.cos(xs * 8.33)), points["bunny"], world_matrix["bunny"]
),
"teapot": interpolate_position(
np.abs(np.cos(xs * 5.13)), points["teapot"], world_matrix["teapot"]
),
"suzanne": interpolate_position(
np.abs(np.cos(xs * 7.11)), points["suzanne"], world_matrix["suzanne"]
),
}
for frame in range(1, num_frames + 1):
bunny.position = positions["bunny"][frame - 1]
bunny.keyframe_insert("position", frame)
teapot.position = positions["teapot"][frame - 1]
teapot.keyframe_insert("position", frame)
suzanne.position = positions["suzanne"][frame - 1]
suzanne.keyframe_insert("position", frame)
# --- renders the output
kb.as_path("output_top").mkdir(exist_ok=True)
np.save("output_top/suzanne.npy", positions["suzanne"])
np.save("output_top/teapot.npy", positions["teapot"])
np.save("output_top/bunny.npy", positions["bunny"])
np.save("output_top/camera_pos.npy", np.array(camera.position))
renderer.save_state("output_top/trio_top.blend")
frames_dict = renderer.render()
kb.write_image_dict(frames_dict, "output_top")