def add_desk(self, scale, loc, euler, color): names = [] for i in (-1, 1): for j in (-1, 1): new_loc = (loc[0] + i * (scale[0] - scale[2]), loc[1] + j * (scale[1] - scale[2]), loc[2] + scale[2]) names.append(self.add_object("cube", (scale[2], scale[2], scale[2]), new_loc, convert_euler(euler), color)) names.append(self.add_object("cube", (.04, scale[0], scale[1]), ( loc[0], loc[1], loc[2] + scale[2] * 2 + OCCLUDER_HALF_WIDTH), convert_euler(euler), color)) return names
def log(self, id, motion, mask): """log the motion of object id into log""" bpy.context.view_layer.objects.active = bpy.data.objects[self.obj_names[id]] obj_mask = np.asarray(mask, dtype=np.uint8, order="F") mask_code = mask_util.encode(obj_mask) mask_code["counts"] = mask_code["counts"].decode("ASCII") scale = [x * y for x, y in zip(self.scales[id], SHAPE_DIMENSIONS[self.shapes[id]])] shape = "Occluder" if self.scales[id][0] == OCCLUDER_HALF_WIDTH else TYPES[self.shapes[id]] return dict(mask=mask_code, name=self.obj_names[id], type=shape, scale=scale, location=motion["location"], rotation=convert_euler(motion["orientation"]), velocity=motion["velocity"], angular_velocity=convert_euler(motion["angular_velocity"]), color=self.colors[id])
def __init__(self, config, shape_dir, material_dir, back_wall): self.shape_dir = shape_dir self.material_dir = material_dir self.obj_names = [] self.mask_nodes = [] self.shapes = [] self.scales = [] self.colors = [] self.load_materials() self.color_name_to_rgba = {} for name, rgb in COLORS.items(): rgba = [float(c) / 255.0 for c in rgb] + [1.0] self.color_name_to_rgba[name] = rgba self.config = config for obj_params in self.config["objects"]: shape = obj_params['shape'] scale, loc = obj_params['scale'], obj_params['init_pos'] euler = convert_euler(deg2rad(obj_params['init_orn'])) color = obj_params['color'] name = self.add_object(shape, scale, loc, euler, color) material = obj_params['material'] split = obj_params['split'] if "split" in obj_params else (0, 0, 0) self.add_material(name, material, color, split) if "occluders" in self.config: for obj_params in self.config["occluders"]: shape = obj_params['shape'] scale, loc = obj_params['scale'], obj_params['init_pos'] euler = convert_euler(deg2rad(obj_params['init_orn'])) color = obj_params['color'] name = self.add_object(shape, scale, loc, euler, color) material = obj_params['material'] self.add_material(name, material, color) if "desks" in self.config: for obj_params in self.config["desks"]: scale, loc = obj_params['scale'], obj_params['init_pos'] euler = convert_euler(deg2rad(obj_params['init_orn'])) color = obj_params['color'] names = self.add_desk(scale, loc, euler, color) material = obj_params['material'] for name in names: self.add_material(name, material, color) if back_wall: desk_name = self.add_back_wall("wall_color") material = "rubber" color = "wall" self.add_material(desk_name, material, color)
def render_intro(om, rendering, m): render_args = bpy.context.scene.render time_step = 1 / rendering.fps phi_s, theta_s = get_intro_camera( rendering, int(rendering.intro_time * rendering.fps)) for n in range(int(rendering.intro_time * rendering.fps)): if "ABORT" in globals(): if globals()["ABORT"]: print("Aborted") raise KeyboardInterrupt set_camera(rendering.camera_rho, theta_s[n], phi_s[n], look_at=rendering.camera_look_at) # objects are before occluders for i, obj_motion in enumerate(m["objects"] + m["occluders"]): loc = obj_motion['location'] euler = convert_euler(obj_motion['orientation']) om.set_position(om.obj_names[i], loc, euler) i = len(m["objects"]) + len(m["occluders"]) for desk_motion in m["desks"]: for obj_motion in desk_motion: loc = obj_motion['location'] euler = convert_euler(obj_motion['orientation']) om.set_position(om.obj_names[i], loc, euler) i += 1 image_path = os.path.join( rendering.output_dir, 'imgs', '%s_-%05.2fs.png' % (rendering.image_prefix, n * time_step)) render_args.filepath = image_path bpy.ops.render.render(write_still=True) set_camera(rendering.camera_rho, rendering.camera_theta, rendering.camera_phi, look_at=rendering.camera_look_at)
def main(config): # main script rendering = config.rendering mkdir(rendering.output_dir) mkdir(os.path.join(rendering.output_dir, 'imgs')) mkdir(os.path.join(rendering.output_dir, 'masks')) mkdir(os.path.join(rendering.output_dir, 'depths')) mkdir(os.path.join(rendering.output_dir, 'flows')) clr_dir(os.path.join(rendering.output_dir, 'imgs')) clr_dir(os.path.join(rendering.output_dir, 'masks')) clr_dir(os.path.join(rendering.output_dir, 'depths')) clr_dir(os.path.join(rendering.output_dir, 'flows')) add_ground(rendering) # set up render parameters render_args = bpy.context.scene.render render_args.engine = 'CYCLES' render_args.resolution_x = rendering.width render_args.resolution_y = rendering.height render_args.resolution_percentage = 100 render_args.tile_x = rendering.render_tile_size render_args.tile_y = rendering.render_tile_size if rendering.use_gpu: if True: enable_gpus() render_args.tile_x = rendering.render_tile_size_gpu render_args.tile_y = rendering.render_tile_size_gpu else: # blender changed the API for enabling CUDA at some point pref = bpy.context.user_preferences.addons["cycles"].preferences pref.compute_device_type = "CUDA" for device in pref.devices: device.use = True # bpy.context.user_preferences.system.compute_device_type = 'CUDA' # bpy.context.user_preferences.system.compute_device = 'CUDA_0' render_args.tile_x = rendering.render_tile_size_gpu render_args.tile_y = rendering.render_tile_size_gpu # some CYCLES-specific stuff bpy.data.worlds['World'].cycles.sample_as_light = True bpy.context.scene.cycles.blur_glossy = 2.0 bpy.context.scene.cycles.samples = rendering.render_num_samples bpy.context.scene.cycles.transparent_min_bounces = rendering.render_min_bounces bpy.context.scene.cycles.transparent_max_bounces = rendering.render_max_bounces if rendering.use_gpu: bpy.context.scene.cycles.device = 'GPU' bpy.context.scene.use_nodes = True bpy.context.scene.view_layers['RenderLayer'].use_pass_object_index = True # set up camera set_camera(rendering.camera_rho, rendering.camera_theta, rendering.camera_phi, look_at=rendering.camera_look_at) # apply jitter to lamp positions if rendering.key_light_jitter > 0: for i in range(3): bpy.data.objects['Lamp_Key'].location[i] += rand_jitter( rendering.key_light_jitter) if rendering.back_light_jitter > 0: for i in range(3): bpy.data.objects['Lamp_Back'].location[i] += rand_jitter( rendering.back_light_jitter) if rendering.fill_light_jitter > 0: for i in range(3): bpy.data.objects['Lamp_Fill'].location[i] += rand_jitter( rendering.fill_light_jitter) # set up objects om = ObjectManager(config, rendering.shape_dir, rendering.material_dir, rendering.back_wall) mask_node = set_mask(os.path.join(rendering.output_dir, "masks")) depth_node = set_depth(os.path.join(rendering.output_dir, "depths")) flow_node = set_flow(os.path.join(rendering.output_dir, "flows")) # load motion with open(rendering.motion_file, 'r') as f: input_file = json.load(f) time_step = float(input_file['timestep']) motion = input_file['motion'] # render it render_every = int(1 / time_step / rendering.fps) if render_every == 0: render_every = 1 camera = dict(camera_rho=rendering.camera_rho, camera_theta=rendering.camera_theta, camera_phi=rendering.camera_phi, camera_look_at=rendering.camera_look_at) scene_anns = dict(case_name=config.case_name, camera=camera, scene=[]) if rendering.intro_time > 0: render_intro(om, rendering, motion[0]) for n, m in enumerate(motion): if n % render_every == 0: bpy.context.scene.frame_set(n) # objects are before occluders for i, obj_motion in enumerate(m["objects"] + m["occluders"]): loc = obj_motion['location'] euler = convert_euler(obj_motion['orientation']) om.set_position(om.obj_names[i], loc, euler, key_frame=True) i = len(m["objects"]) + len(m["occluders"]) for desk_motion in m["desks"]: for obj_motion in desk_motion: loc = obj_motion['location'] euler = convert_euler(obj_motion['orientation']) om.set_position(om.obj_names[i], loc, euler, key_frame=True) i += 1 for n, m in enumerate(motion): if "ABORT" in globals(): if globals()["ABORT"]: print("Aborted") raise KeyboardInterrupt if n % render_every == 0: bpy.context.scene.frame_set(n) image_path = os.path.join( rendering.output_dir, 'imgs', '%s_%06.2fs.png' % (rendering.image_prefix, n * time_step)) render_args.filepath = image_path mask_base_name = '####_%s_%06.2fs.png' % (rendering.image_prefix, n * time_step) mask_node.file_slots[0].path = mask_base_name depth_base_name = '####_%s_%06.2fs.png' % (rendering.image_prefix, n * time_step) depth_node.file_slots[0].path = depth_base_name for ch in "RGBA": flow_base_name = '%s_####_%s_%06.2fs.png' % ( ch, rendering.image_prefix, n * time_step) flow_node[ch].file_slots[0].path = flow_base_name bpy.ops.render.render(write_still=True) frame_anns = dict(image_path=image_path, objects=[]) mask_file_path = os.path.join( rendering.output_dir, "masks", "{:04d}".format(n) + mask_base_name[4:]) for i, obj_motion in enumerate(m["objects"] + m["occluders"]): mask = imread(mask_file_path)[:, :, 0] == i + 1 frame_anns["objects"].append(om.log(i, obj_motion, mask)) scene_anns["scene"].append(frame_anns) bpy.ops.wm.save_as_mainfile(filepath=os.path.join(rendering.output_dir, "scene.blend"), compress=True) write_serialized( scene_anns, os.path.join(rendering.output_dir, "{:s}_ann.yaml".format(rendering.image_prefix)))