add normal

This commit is contained in:
hofee 2024-10-02 16:07:33 +08:00
parent e6fd317bac
commit a712ec063c
2 changed files with 46 additions and 17 deletions

View File

@ -69,13 +69,10 @@ class BlenderUtils:
@staticmethod @staticmethod
def setup_scene(init_light_and_camera_config, table_model_path, binocular_vision): def setup_scene(init_light_and_camera_config, table_model_path, binocular_vision):
bpy.context.scene.render.engine = 'BLENDER_WORKBENCH' bpy.context.scene.render.engine = 'BLENDER_WORKBENCH'
bpy.context.scene.display.shading.light = 'FLAT'
bpy.context.scene.display.shading.color_type = 'MATERIAL'
bpy.context.scene.display.shading.show_xray = False bpy.context.scene.display.shading.show_xray = False
bpy.context.scene.display.shading.use_dof = False bpy.context.scene.display.shading.use_dof = False
bpy.context.scene.display.render_aa = 'OFF' bpy.context.scene.display.render_aa = 'OFF'
bpy.context.scene.view_settings.view_transform = 'Standard' bpy.context.scene.view_settings.view_transform = 'Standard'
BlenderUtils.init_light_and_camera(init_light_and_camera_config, binocular_vision) BlenderUtils.init_light_and_camera(init_light_and_camera_config, binocular_vision)
BlenderUtils.add_plane("plane_floor", location=(0,0,0), orientation=(0,0,0)) BlenderUtils.add_plane("plane_floor", location=(0,0,0), orientation=(0,0,0))
@ -170,14 +167,24 @@ class BlenderUtils:
min_z = min([v.z for v in vertices_world]) min_z = min([v.z for v in vertices_world])
return min_z return min_z
@staticmethod
def setup_render_mask():
bpy.context.scene.display.shading.light = 'FLAT'
bpy.context.scene.display.shading.color_type = 'MATERIAL'
@staticmethod
def setup_render_normal():
bpy.context.scene.display.shading.light = 'MATCAP'
bpy.context.scene.display.shading.studio_light = 'check_normal+y.exr'
@staticmethod @staticmethod
def render_and_save(output_dir, file_name, binocular_vision=False): def render_and_save(output_dir, file_name, binocular_vision=False):
target_cameras = [BlenderUtils.CAMERA_NAME] target_cameras = [BlenderUtils.CAMERA_NAME]
if binocular_vision: if binocular_vision:
target_cameras.append(BlenderUtils.CAMERA_RIGHT_NAME) target_cameras.append(BlenderUtils.CAMERA_RIGHT_NAME)
BlenderUtils.setup_render_mask()
for cam_name in target_cameras: for cam_name in target_cameras:
# Set the current camera
bpy.context.scene.camera = BlenderUtils.get_obj(cam_name) bpy.context.scene.camera = BlenderUtils.get_obj(cam_name)
bpy.context.scene.view_layers["ViewLayer"].use_pass_z = True bpy.context.scene.view_layers["ViewLayer"].use_pass_z = True
cam_suffix = "L" if cam_name == BlenderUtils.CAMERA_NAME else "R" cam_suffix = "L" if cam_name == BlenderUtils.CAMERA_NAME else "R"
@ -188,8 +195,6 @@ class BlenderUtils:
if not os.path.exists(mask_dir): if not os.path.exists(mask_dir):
os.makedirs(mask_dir) os.makedirs(mask_dir)
# Modify the file name based on the camera
scene.render.filepath = os.path.join(output_dir, mask_dir, f"{file_name}_{cam_suffix}.png") scene.render.filepath = os.path.join(output_dir, mask_dir, f"{file_name}_{cam_suffix}.png")
scene.render.image_settings.color_depth = '8' scene.render.image_settings.color_depth = '8'
scene.render.resolution_percentage = 100 scene.render.resolution_percentage = 100
@ -226,6 +231,28 @@ class BlenderUtils:
tree.links.new(map_range.outputs[0], output_depth.inputs[0]) tree.links.new(map_range.outputs[0], output_depth.inputs[0])
bpy.ops.render.render(write_still=True) bpy.ops.render.render(write_still=True)
BlenderUtils.setup_render_normal()
for cam_name in target_cameras:
bpy.context.scene.camera = BlenderUtils.get_obj(cam_name)
bpy.context.scene.view_layers["ViewLayer"].use_pass_normal = True
cam_suffix = "L" if cam_name == BlenderUtils.CAMERA_NAME else "R"
scene = bpy.context.scene
scene.render.filepath = ""
normal_dir = os.path.join(output_dir, "normal")
if not os.path.exists(normal_dir):
os.makedirs(normal_dir)
scene.render.filepath = os.path.join(output_dir, normal_dir, f"{file_name}_{cam_suffix}.png")
scene.render.image_settings.color_depth = '8'
scene.render.resolution_percentage = 100
scene.render.use_overwrite = False
scene.render.use_file_extension = False
scene.render.use_placeholder = False
bpy.ops.render.render(write_still=True)
msg = "success" msg = "success"
return msg return msg

View File

@ -40,7 +40,7 @@ class ViewSampleUtil:
return np.array(downsampled_points), downsampled_indices return np.array(downsampled_points), downsampled_indices
@staticmethod @staticmethod
def sample_view_data(obj, distance_range:tuple = (0.2,0.4), voxel_size:float = 0.005, max_views: int = 1) -> dict: def sample_view_data(obj, distance_range:tuple = (0.2,0.4), voxel_size:float = 0.005, max_views: int = 1, pertube_repeat:int = 1) -> dict:
view_data = { view_data = {
"look_at_points": [], "look_at_points": [],
"cam_positions": [], "cam_positions": [],
@ -69,6 +69,8 @@ class ViewSampleUtil:
if np.dot(normal, look_at_point) < 0: if np.dot(normal, look_at_point) < 0:
normal = -normal normal = -normal
normals.append(normal) normals.append(normal)
for _ in range(pertube_repeat):
perturb_angle = np.radians(np.random.uniform(0, 30)) perturb_angle = np.radians(np.random.uniform(0, 30))
perturb_axis = np.random.normal(size=3) perturb_axis = np.random.normal(size=3)
perturb_axis /= np.linalg.norm(perturb_axis) perturb_axis /= np.linalg.norm(perturb_axis)