File size: 19,348 Bytes
2cdb96e |
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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse, sys, os, math, re
import bpy
from mathutils import Vector, Matrix
import numpy as np
import cv2
import signal
from contextlib import contextmanager
from loguru import logger
from typing import Any, Callable, Dict, Generator, List, Literal, Optional, Set, Tuple
import random
class TimeoutException(Exception): pass
logger.info('Rendering started.')
@contextmanager
def time_limit(seconds):
def signal_handler(signum, frame):
raise TimeoutException("Timed out!")
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)
parser = argparse.ArgumentParser(description='Renders given obj file by rotation a camera around it.')
parser.add_argument(
'--seed', type=int, default=0,
help='number of views to be rendered')
parser.add_argument(
'--views', type=int, default=4,
help='number of views to be rendered')
parser.add_argument(
'obj', type=str,
help='Path to the obj file to be rendered.')
parser.add_argument(
'--output_folder', type=str, default='/tmp',
help='The path the output will be dumped to.')
parser.add_argument(
'--scale', type=float, default=1,
help='Scaling factor applied to model. Depends on size of mesh.')
parser.add_argument(
'--format', type=str, default='PNG',
help='Format of files generated. Either PNG or OPEN_EXR')
parser.add_argument(
'--resolution', type=int, default=512,
help='Resolution of the images.')
parser.add_argument(
'--engine', type=str, default='CYCLES',
help='Blender internal engine for rendering. E.g. CYCLES, BLENDER_EEVEE, ...')
parser.add_argument(
'--gpu', type=int, default=0,
help='gpu.')
parser.add_argument(
'--animation_idx', type=int, default=0,
help='The index of animation')
parser.add_argument(
'--camera_option', type=str, default='fixed',
help='Camera Options')
parser.add_argument(
'--fixed_animation_length', type=int, default=-1,
help='Set animation length to fixed number of framnes')
parser.add_argument(
'--step_angle', type=int, default=3,
help='Angle in degree for each step camera rotation')
parser.add_argument(
'--downsample', type=int, default=1,
help='Downsample ratio. No downsample by default')
argv = sys.argv[sys.argv.index("--") + 1:]
args = parser.parse_args(argv)
model_identifier = os.path.split(args.obj)[1].split('.')[0]
synset_idx = args.obj.split('/')[-2]
save_root = os.path.join(os.path.abspath(args.output_folder), synset_idx, model_identifier, f'{args.animation_idx:03d}')
# Set up rendering
context = bpy.context
scene = bpy.context.scene
render = bpy.context.scene.render
render.engine = args.engine# 'BLENDER_EEVEE'
render.image_settings.color_mode = 'RGBA' # ('RGB', 'RGBA', ...)
render.image_settings.file_format = args.format # ('PNG', 'OPEN_EXR', 'JPEG, ...)
render.resolution_x = args.resolution
render.resolution_y = args.resolution
render.resolution_percentage = 100
bpy.context.scene.cycles.filter_width = 0.01
bpy.context.scene.render.film_transparent = True
render_depth_normal = False
bpy.context.scene.cycles.device = 'GPU'
bpy.context.scene.cycles.diffuse_bounces = 1
bpy.context.scene.cycles.glossy_bounces = 1
bpy.context.scene.cycles.transparent_max_bounces = 1
bpy.context.scene.cycles.transmission_bounces = 1
bpy.context.scene.cycles.samples = 16
bpy.context.scene.cycles.use_denoising = True
bpy.context.scene.cycles.denoiser = 'OPTIX'
bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'CUDA'
bpy.context.scene.cycles.device = 'GPU'
def enable_cuda_devices():
prefs = bpy.context.preferences
cprefs = prefs.addons['cycles'].preferences
cprefs.get_devices()
# Attempt to set GPU device types if available
for compute_device_type in ('CUDA', 'OPENCL', 'NONE'):
try:
cprefs.compute_device_type = compute_device_type
print("Compute device selected: {0}".format(compute_device_type))
break
except TypeError:
pass
# Any CUDA/OPENCL devices?
acceleratedTypes = ['CUDA', 'OPENCL', 'OPTIX']
acceleratedTypes = ['CUDA', 'OPENCL']
accelerated = any(device.type in acceleratedTypes for device in cprefs.devices)
print('Accelerated render = {0}'.format(accelerated))
# If we have CUDA/OPENCL devices, enable only them, otherwise enable
# all devices (assumed to be CPU)
print(cprefs.devices)
for idx, device in enumerate(cprefs.devices):
device.use = (not accelerated or device.type in acceleratedTypes)# and idx == args.gpu
print('Device enabled ({type}) = {enabled}'.format(type=device.type, enabled=device.use))
return accelerated
enable_cuda_devices()
context.active_object.select_set(True)
bpy.ops.object.delete()
# Import textured mesh
bpy.ops.object.select_all(action='DESELECT')
try:
with time_limit(1000):
imported_object = bpy.ops.import_scene.gltf(filepath=args.obj, merge_vertices=True, guess_original_bind_pose=False, bone_heuristic="TEMPERANCE")
except TimeoutException as e:
print("Timed out finished!")
exit()
# count animated frames
animation_names = []
ending_frame_list = {}
for k in bpy.data.actions.keys():
matched_obj_name = ''
for obj in bpy.context.selected_objects:
if '_'+obj.name in k and len(obj.name) > len(matched_obj_name):
matched_obj_name = obj.name
a_name = k.replace('_'+matched_obj_name, '')
a = bpy.data.actions[k]
frame_start, frame_end = map(int, a.frame_range)
logger.info(f'{k} | frame start: {frame_start}, frame end: {frame_end} | fps: {bpy.context.scene.render.fps}')
if a_name not in animation_names:
animation_names.append(a_name)
ending_frame_list[a_name] = frame_end
else:
ending_frame_list[a_name] = max(frame_end, ending_frame_list[a_name])
selected_a_name = animation_names[args.animation_idx]
max_frame = ending_frame_list[selected_a_name]
for obj in bpy.context.selected_objects:
if obj.animation_data is not None:
obj_a_name = selected_a_name+'_'+obj.name
if obj_a_name in bpy.data.actions:
print('Found ', obj_a_name)
obj.animation_data.action = bpy.data.actions[obj_a_name]
else:
print('Miss ', obj_a_name)
num_frames = args.fixed_animation_length if args.fixed_animation_length != -1 else max_frame
num_frames = num_frames // args.downsample
if num_frames == 0:
print("No animation!")
exit()
# from https://github.com/allenai/objaverse-xl/blob/main/scripts/rendering/blender_script.py
def get_3x4_RT_matrix_from_blender(cam: bpy.types.Object):
"""Returns the 3x4 RT matrix from the given camera.
Taken from Zero123, which in turn was taken from
https://github.com/panmari/stanford-shapenet-renderer/blob/master/render_blender.py
Args:
cam (bpy.types.Object): The camera object.
Returns:
Matrix: The 3x4 RT matrix from the given camera.
"""
# Use matrix_world instead to account for all constraints
location, rotation = cam.matrix_world.decompose()[0:2]
R_world2bcam = rotation.to_matrix().transposed()
# Use location from matrix_world to account for constraints:
T_world2bcam = -1 * R_world2bcam @ location
# put into 3x4 matrix
RT = Matrix(
(
R_world2bcam[0][:] + (T_world2bcam[0],),
R_world2bcam[1][:] + (T_world2bcam[1],),
R_world2bcam[2][:] + (T_world2bcam[2],),
)
)
return RT
def _create_light(
name: str,
light_type: Literal["POINT", "SUN", "SPOT", "AREA"],
location: Tuple[float, float, float],
rotation: Tuple[float, float, float],
energy: float,
use_shadow: bool = False,
specular_factor: float = 1.0,
):
"""Creates a light object.
Args:
name (str): Name of the light object.
light_type (Literal["POINT", "SUN", "SPOT", "AREA"]): Type of the light.
location (Tuple[float, float, float]): Location of the light.
rotation (Tuple[float, float, float]): Rotation of the light.
energy (float): Energy of the light.
use_shadow (bool, optional): Whether to use shadows. Defaults to False.
specular_factor (float, optional): Specular factor of the light. Defaults to 1.0.
Returns:
bpy.types.Object: The light object.
"""
light_data = bpy.data.lights.new(name=name, type=light_type)
light_object = bpy.data.objects.new(name, light_data)
bpy.context.collection.objects.link(light_object)
light_object.location = location
light_object.rotation_euler = rotation
light_data.use_shadow = use_shadow
light_data.specular_factor = specular_factor
light_data.energy = energy
return light_object
def randomize_lighting() -> Dict[str, bpy.types.Object]:
"""Randomizes the lighting in the scene.
Returns:
Dict[str, bpy.types.Object]: Dictionary of the lights in the scene. The keys are
"key_light", "fill_light", "rim_light", and "bottom_light".
"""
# Clear existing lights
bpy.ops.object.select_all(action="DESELECT")
bpy.ops.object.select_by_type(type="LIGHT")
bpy.ops.object.delete()
# Create key light
key_light = _create_light(
name="Key_Light",
light_type="SUN",
location=(0, 0, 0),
rotation=(0.785398, 0, -0.785398),
# energy=random.choice([3, 4, 5]),
energy=4,
)
# Create fill light
fill_light = _create_light(
name="Fill_Light",
light_type="SUN",
location=(0, 0, 0),
rotation=(0.785398, 0, 2.35619),
# energy=random.choice([2, 3, 4]),
energy=3,
)
# Create rim light
rim_light = _create_light(
name="Rim_Light",
light_type="SUN",
location=(0, 0, 0),
rotation=(-0.785398, 0, -3.92699),
# energy=random.choice([3, 4, 5]),
energy=4,
)
# Create bottom light
bottom_light = _create_light(
name="Bottom_Light",
light_type="SUN",
location=(0, 0, 0),
rotation=(3.14159, 0, 0),
# energy=random.choice([1, 2, 3]),
energy=2,
)
return dict(
key_light=key_light,
fill_light=fill_light,
rim_light=rim_light,
bottom_light=bottom_light,
)
def scene_bbox(
single_obj = None, ignore_matrix = False
):
"""Returns the bounding box of the scene.
Taken from Shap-E rendering script
(https://github.com/openai/shap-e/blob/main/shap_e/rendering/blender/blender_script.py#L68-L82)
Args:
single_obj (Optional[bpy.types.Object], optional): If not None, only computes
the bounding box for the given object. Defaults to None.
ignore_matrix (bool, optional): Whether to ignore the object's matrix. Defaults
to False.
Raises:
RuntimeError: If there are no objects in the scene.
Returns:
Tuple[Vector, Vector]: The minimum and maximum coordinates of the bounding box.
"""
bbox_min = (math.inf,) * 3
bbox_max = (-math.inf,) * 3
found = False
for i in range(num_frames):
bpy.context.scene.frame_set(i * args.downsample)
for obj in get_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 get_scene_meshes():
"""Returns all meshes in the scene.
Yields:
Generator[bpy.types.Object, None, None]: Generator of all meshes in the scene.
"""
for obj in bpy.context.scene.objects.values():
if isinstance(obj.data, (bpy.types.Mesh)):
yield obj
def get_scene_root_objects():
"""Returns all root objects in the scene.
Yields:
Generator[bpy.types.Object, None, None]: Generator of all root objects in the
scene.
"""
for obj in bpy.context.scene.objects.values():
if not obj.parent:
yield obj
def normalize_scene():
"""Normalizes the scene by scaling and translating it to fit in a unit cube centered
at the origin.
Mostly taken from the Point-E / Shap-E rendering script
(https://github.com/openai/point-e/blob/main/point_e/evals/scripts/blender_script.py#L97-L112),
but fix for multiple root objects: (see bug report here:
https://github.com/openai/shap-e/pull/60).
Returns:
None
"""
if len(list(get_scene_root_objects())) > 1:
# create an empty object to be used as a parent for all root objects
parent_empty = bpy.data.objects.new("ParentEmpty", None)
bpy.context.scene.collection.objects.link(parent_empty)
# parent all root objects to the empty object
for obj in get_scene_root_objects():
if obj != parent_empty:
obj.parent = parent_empty
bbox_min, bbox_max = scene_bbox()
scale = 1 / max(bbox_max - bbox_min)
logger.info(f"Scale: {scale}")
for obj in get_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 get_scene_root_objects():
obj.matrix_world.translation += offset
bpy.ops.object.select_all(action="DESELECT")
# unparent the camera
bpy.data.objects["Camera"].parent = None
normalize_scene()
randomize_lighting()
# Place camera
cam = scene.objects['Camera']
cam.location = (0, 1.5, 0) # radius equals to 1
cam.data.lens = 35
cam.data.sensor_width = 32
cam_constraint = cam.constraints.new(type='TRACK_TO')
cam_constraint.track_axis = 'TRACK_NEGATIVE_Z'
cam_constraint.up_axis = 'UP_Y'
cam_empty = bpy.data.objects.new("Empty", None)
cam_empty.location = (0, 0, 0)
cam.parent = cam_empty
scene.collection.objects.link(cam_empty)
context.view_layer.objects.active = cam_empty
cam_constraint.target = cam_empty
stepsize = 360.0 / args.views
rotation_mode = 'XYZ'
np.random.seed(args.seed)
if args.camera_option == "fixed":
for scene in bpy.data.scenes:
scene.cycles.device = 'GPU'
elevation_angle = 0.
rotation_angle = 0.
for view_idx in range(args.views):
img_folder = os.path.join(save_root, f'{view_idx:03d}', 'img')
mask_folder = os.path.join(save_root, f'{view_idx:03d}', 'mask')
camera_folder = os.path.join(save_root, f'{view_idx:03d}', 'camera')
os.makedirs(img_folder, exist_ok=True)
os.makedirs(mask_folder, exist_ok=True)
os.makedirs(camera_folder, exist_ok=True)
np.save(os.path.join(camera_folder, 'rotation'), np.array([rotation_angle + view_idx * stepsize for _ in range(num_frames)]))
np.save(os.path.join(camera_folder, 'elevation'), np.array([elevation_angle for _ in range(num_frames)]))
cam_empty.rotation_euler[2] = math.radians(rotation_angle + view_idx * stepsize)
cam_empty.rotation_euler[0] = math.radians(elevation_angle)
# save camera RT matrix
rt_matrix = get_3x4_RT_matrix_from_blender(cam)
rt_matrix_path = os.path.join(camera_folder, "rt_matrix.npy")
np.save(rt_matrix_path, rt_matrix)
for i in range(0, num_frames):
bpy.context.scene.frame_set(i * args.downsample)
render_file_path = os.path.join(img_folder,'%03d.png' % (i))
scene.render.filepath = render_file_path
bpy.ops.render.render(write_still=True)
for i in range(0, num_frames):
img = cv2.imread(os.path.join(img_folder, '%03d.png' % (i)), cv2.IMREAD_UNCHANGED)
mask = img[:, :, 3:4] / 255.0
white_img = img[:, :, :3] * mask + np.ones_like(img[:, :, :3]) * (1 - mask) * 255
white_img = np.clip(white_img, 0, 255)
cv2.imwrite(os.path.join(img_folder, '%03d.jpg' % (i)), white_img)
cv2.imwrite(os.path.join(mask_folder, '%03d.png'%(i)), img[:, :, 3])
os.system('rm %s'%(os.path.join(img_folder, '%03d.png' % (i))))
elif args.camera_option == "random":
for scene in bpy.data.scenes:
scene.cycles.device = 'GPU'
for view_idx in range(args.views):
elevation_angle = np.random.rand(1) * 35 - 5 # [-5, 30]
rotation_angle = np.random.rand(1) * 360
img_folder = os.path.join(save_root, f'{view_idx:03d}', 'img')
mask_folder = os.path.join(save_root, f'{view_idx:03d}', 'mask')
camera_folder = os.path.join(save_root, f'{view_idx:03d}', 'camera')
os.makedirs(img_folder, exist_ok=True)
os.makedirs(mask_folder, exist_ok=True)
os.makedirs(camera_folder, exist_ok=True)
np.save(os.path.join(camera_folder, 'rotation'), np.array([rotation_angle for _ in range(num_frames)]))
np.save(os.path.join(camera_folder, 'elevation'), np.array([elevation_angle for _ in range(num_frames)]))
cam_empty.rotation_euler[2] = math.radians(rotation_angle)
cam_empty.rotation_euler[0] = math.radians(elevation_angle)
# save camera RT matrix
rt_matrix = get_3x4_RT_matrix_from_blender(cam)
rt_matrix_path = os.path.join(camera_folder, "rt_matrix.npy")
np.save(rt_matrix_path, rt_matrix)
for i in range(0, num_frames):
bpy.context.scene.frame_set(i * args.downsample)
render_file_path = os.path.join(img_folder,'%03d.png' % (i))
scene.render.filepath = render_file_path
bpy.ops.render.render(write_still=True)
for i in range(0, num_frames):
img = cv2.imread(os.path.join(img_folder, '%03d.png' % (i)), cv2.IMREAD_UNCHANGED)
mask = img[:, :, 3:4] / 255.0
white_img = img[:, :, :3] * mask + np.ones_like(img[:, :, :3]) * (1 - mask) * 255
white_img = np.clip(white_img, 0, 255)
cv2.imwrite(os.path.join(img_folder, '%03d.jpg' % (i)), white_img)
cv2.imwrite(os.path.join(mask_folder, '%03d.png'%(i)), img[:, :, 3])
os.system('rm %s'%(os.path.join(img_folder, '%03d.png' % (i))))
else:
raise NotImplemented
|