-
Notifications
You must be signed in to change notification settings - Fork 55
/
blender_script.py
358 lines (297 loc) · 11.9 KB
/
blender_script.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""
Blender script to render images of 3D models.
This script is designed to render data used in the [OpenLRM project](https://github.com/3DTopia/OpenLRM).
Modified from https://github.com/cvlab-columbia/zero123/blob/main/objaverse-rendering/scripts/blender_script.py
Original script licensed under MIT, found at the root of its repository.
Modifications are licensed under Apache 2.0.
"""
import argparse
import math
import os
import random
import sys
import time
import urllib.request
from typing import Tuple
from mathutils import Vector
import numpy as np
import bpy
parser = argparse.ArgumentParser()
parser.add_argument(
"--object_path",
type=str,
required=True,
help="Path to the object file",
)
parser.add_argument("--output_dir", type=str, default="./views")
parser.add_argument(
"--engine", type=str, default="CYCLES", choices=["CYCLES", "BLENDER_EEVEE"]
)
parser.add_argument("--num_images", type=int, default=32)
parser.add_argument("--resolution", type=int, default=1024)
argv = sys.argv[sys.argv.index("--") + 1 :]
args = parser.parse_args(argv)
print('===================', args.engine, '===================')
context = bpy.context
scene = context.scene
render = scene.render
render.engine = args.engine
render.image_settings.file_format = "PNG"
render.image_settings.color_mode = "RGBA"
render.resolution_x = args.resolution
render.resolution_y = args.resolution
render.resolution_percentage = 100
scene.cycles.device = "GPU"
scene.cycles.samples = 128
scene.cycles.diffuse_bounces = 1
scene.cycles.glossy_bounces = 1
scene.cycles.transparent_max_bounces = 3
scene.cycles.transmission_bounces = 3
scene.cycles.filter_width = 0.01
scene.cycles.use_denoising = True
scene.render.film_transparent = True
# Set the device_type
cycles_preferences = bpy.context.preferences.addons["cycles"].preferences
cycles_preferences.compute_device_type = "CUDA" # or "OPENCL"
cuda_devices = cycles_preferences.get_devices_for_type("CUDA")
for device in cuda_devices:
device.use = True
def compose_RT(R, T):
return np.hstack((R, T.reshape(-1, 1)))
def sample_point_on_sphere(radius: float) -> Tuple[float, float, float]:
theta = random.random() * 2 * math.pi
phi = math.acos(2 * random.random() - 1)
return (
radius * math.sin(phi) * math.cos(theta),
radius * math.sin(phi) * math.sin(theta),
radius * math.cos(phi),
)
def sample_spherical(radius=3.0, maxz=3.0, minz=0.):
correct = False
while not correct:
vec = np.random.uniform(-1, 1, 3)
vec[2] = np.abs(vec[2])
vec = vec / np.linalg.norm(vec, axis=0) * radius
if maxz > vec[2] > minz:
correct = True
return vec
def sample_spherical(radius_min=1.5, radius_max=2.0, maxz=1.6, minz=-0.75):
correct = False
while not correct:
vec = np.random.uniform(-1, 1, 3)
# vec[2] = np.abs(vec[2])
radius = np.random.uniform(radius_min, radius_max, 1)
vec = vec / np.linalg.norm(vec, axis=0) * radius[0]
if maxz > vec[2] > minz:
correct = True
return vec
def set_camera_location(camera, option: str):
assert option in ['fixed', 'random', 'front']
if option == 'fixed':
x, y, z = 0, -2.25, 0
elif option == 'random':
# from https://blender.stackexchange.com/questions/18530/
x, y, z = sample_spherical(radius_min=1.9, radius_max=2.6, maxz=1.60, minz=-0.75)
elif option == 'front':
x, y, z = 0, -np.random.uniform(1.9, 2.6, 1)[0], 0
camera.location = x, y, z
# adjust orientation
direction = - camera.location
rot_quat = direction.to_track_quat('-Z', 'Y')
camera.rotation_euler = rot_quat.to_euler()
return camera
def add_lighting(option: str) -> None:
assert option in ['fixed', 'random']
# delete the default light
bpy.data.objects["Light"].select_set(True)
bpy.ops.object.delete()
# add a new light
bpy.ops.object.light_add(type="AREA")
light = bpy.data.lights["Area"]
if option == 'fixed':
light.energy = 30000
bpy.data.objects["Area"].location[0] = 0
bpy.data.objects["Area"].location[1] = 1
bpy.data.objects["Area"].location[2] = 0.5
elif option == 'random':
light.energy = random.uniform(80000, 120000)
bpy.data.objects["Area"].location[0] = random.uniform(-2., 2.)
bpy.data.objects["Area"].location[1] = random.uniform(-2., 2.)
bpy.data.objects["Area"].location[2] = random.uniform(1.0, 3.0)
# set light scale
bpy.data.objects["Area"].scale[0] = 200
bpy.data.objects["Area"].scale[1] = 200
bpy.data.objects["Area"].scale[2] = 200
def reset_scene() -> None:
"""Resets the scene to a clean state."""
# delete everything that isn't part of a camera or a light
for obj in bpy.data.objects:
if obj.type not in {"CAMERA", "LIGHT"}:
bpy.data.objects.remove(obj, do_unlink=True)
# delete all the materials
for material in bpy.data.materials:
bpy.data.materials.remove(material, do_unlink=True)
# delete all the textures
for texture in bpy.data.textures:
bpy.data.textures.remove(texture, do_unlink=True)
# delete all the images
for image in bpy.data.images:
bpy.data.images.remove(image, do_unlink=True)
# load the glb model
def load_object(object_path: str) -> None:
"""Loads a glb model into the scene."""
if object_path.endswith(".glb"):
bpy.ops.import_scene.gltf(filepath=object_path, merge_vertices=True)
elif object_path.endswith(".fbx"):
bpy.ops.import_scene.fbx(filepath=object_path)
else:
raise ValueError(f"Unsupported file type: {object_path}")
def scene_bbox(single_obj=None, ignore_matrix=False):
bbox_min = (math.inf,) * 3
bbox_max = (-math.inf,) * 3
found = False
for obj in scene_meshes() if single_obj is None else [single_obj]:
found = True
for coord in obj.bound_box:
coord = Vector(coord)
if not ignore_matrix:
coord = obj.matrix_world @ coord
bbox_min = tuple(min(x, y) for x, y in zip(bbox_min, coord))
bbox_max = tuple(max(x, y) for x, y in zip(bbox_max, coord))
if not found:
raise RuntimeError("no objects in scene to compute bounding box for")
return Vector(bbox_min), Vector(bbox_max)
def scene_root_objects():
for obj in bpy.context.scene.objects.values():
if not obj.parent:
yield obj
def scene_meshes():
for obj in bpy.context.scene.objects.values():
if isinstance(obj.data, (bpy.types.Mesh)):
yield obj
def normalize_scene(box_scale: float):
bbox_min, bbox_max = scene_bbox()
scale = box_scale / max(bbox_max - bbox_min)
for obj in scene_root_objects():
obj.scale = obj.scale * scale
# Apply scale to matrix_world.
bpy.context.view_layer.update()
bbox_min, bbox_max = scene_bbox()
offset = -(bbox_min + bbox_max) / 2
for obj in scene_root_objects():
obj.matrix_world.translation += offset
bpy.ops.object.select_all(action="DESELECT")
def setup_camera():
cam = scene.objects["Camera"]
cam.location = (0, 1.2, 0)
cam.data.lens = 24
cam.data.sensor_width = 32
cam.data.sensor_height = 32 # affects instrinsics calculation, should be set explicitly
cam_constraint = cam.constraints.new(type="TRACK_TO")
cam_constraint.track_axis = "TRACK_NEGATIVE_Z"
cam_constraint.up_axis = "UP_Y"
return cam, cam_constraint
def save_images(object_file: str) -> None:
"""Saves rendered images of the object in the scene."""
os.makedirs(args.output_dir, exist_ok=True)
reset_scene()
# load the object
load_object(object_file)
object_uid = os.path.basename(object_file).split(".")[0]
normalize_scene(box_scale=2)
add_lighting(option='random')
camera, cam_constraint = setup_camera()
# create an empty object to track
empty = bpy.data.objects.new("Empty", None)
scene.collection.objects.link(empty)
cam_constraint.target = empty
# prepare to save
img_dir = os.path.join(args.output_dir, object_uid, 'rgba')
pose_dir = os.path.join(args.output_dir, object_uid, 'pose')
os.makedirs(img_dir, exist_ok=True)
os.makedirs(pose_dir, exist_ok=True)
for i in range(args.num_images):
# set the camera position
camera_option = 'random' if i > 0 else 'front'
camera = set_camera_location(camera, option=camera_option)
# render the image
render_path = os.path.join(img_dir, f"{i:03d}.png")
scene.render.filepath = render_path
bpy.ops.render.render(write_still=True)
# save camera RT matrix (C2W)
location, rotation = camera.matrix_world.decompose()[0:2]
RT = compose_RT(rotation.to_matrix(), np.array(location))
RT_path = os.path.join(pose_dir, f"{i:03d}.npy")
np.save(RT_path, RT)
# save the camera intrinsics
intrinsics = get_calibration_matrix_K_from_blender(camera.data, return_principles=True)
with open(os.path.join(args.output_dir, object_uid,'intrinsics.npy'), 'wb') as f_intrinsics:
np.save(f_intrinsics, intrinsics)
def download_object(object_url: str) -> str:
"""Download the object and return the path."""
# uid = uuid.uuid4()
uid = object_url.split("/")[-1].split(".")[0]
tmp_local_path = os.path.join("tmp-objects", f"{uid}.glb" + ".tmp")
local_path = os.path.join("tmp-objects", f"{uid}.glb")
# wget the file and put it in local_path
os.makedirs(os.path.dirname(tmp_local_path), exist_ok=True)
urllib.request.urlretrieve(object_url, tmp_local_path)
os.rename(tmp_local_path, local_path)
# get the absolute path
local_path = os.path.abspath(local_path)
return local_path
def get_calibration_matrix_K_from_blender(camera, return_principles=False):
"""
Get the camera intrinsic matrix from Blender camera.
Return also numpy array of principle parameters if specified.
Intrinsic matrix K has the following structure in pixels:
[fx 0 cx]
[0 fy cy]
[0 0 1]
Specified principle parameters are:
[fx, fy] - focal lengths in pixels
[cx, cy] - optical centers in pixels
[width, height] - image resolution in pixels
"""
# Render resolution
render = bpy.context.scene.render
width = render.resolution_x * render.pixel_aspect_x
height = render.resolution_y * render.pixel_aspect_y
# Camera parameters
focal_length = camera.lens # Focal length in millimeters
sensor_width = camera.sensor_width # Sensor width in millimeters
sensor_height = camera.sensor_height # Sensor height in millimeters
# Calculate the focal length in pixel units
focal_length_x = width * (focal_length / sensor_width)
focal_length_y = height * (focal_length / sensor_height)
# Assuming the optical center is at the center of the sensor
optical_center_x = width / 2
optical_center_y = height / 2
# Constructing the intrinsic matrix
K = np.array([[focal_length_x, 0, optical_center_x],
[0, focal_length_y, optical_center_y],
[0, 0, 1]])
if return_principles:
return np.array([
[focal_length_x, focal_length_y],
[optical_center_x, optical_center_y],
[width, height],
])
else:
return K
if __name__ == "__main__":
try:
start_i = time.time()
if args.object_path.startswith("http"):
local_path = download_object(args.object_path)
else:
local_path = args.object_path
save_images(local_path)
end_i = time.time()
print("Finished", local_path, "in", end_i - start_i, "seconds")
# delete the object if it was downloaded
if args.object_path.startswith("http"):
os.remove(local_path)
except Exception as e:
print("Failed to render", args.object_path)
print(e)