| import argparse, sys, os, math, re |
| import bpy |
| import bmesh |
| from bpy_extras.image_utils import load_image |
| import math |
| import numpy as np |
| import os |
| import bpy |
| import numpy as np |
| from scipy.signal import correlate2d |
| from scipy.ndimage import shift |
| import json |
| from mathutils import Vector |
|
|
|
|
| def setup_background(): |
| import bpy |
| |
| world = bpy.data.worlds.get("World") |
| if world is None: |
| world = bpy.data.worlds.new("World") |
| bpy.context.scene.world = world |
| world.use_nodes = True |
| |
| nodes = world.node_tree.nodes |
| nodes.clear() |
| |
| output_node = nodes.new(type='ShaderNodeOutputWorld') |
| output_node.location = (200, 0) |
| background_node = nodes.new(type='ShaderNodeBackground') |
| background_node.location = (0, 0) |
| |
| background_node.inputs['Color'].default_value = (1, 1, 1, 1) |
| background_node.inputs['Strength'].default_value = 1.0 |
| |
| world.node_tree.links.new(background_node.outputs['Background'], output_node.inputs['Surface']) |
| print("White background set up.") |
|
|
| def reset_blender(): |
| bpy.ops.wm.read_factory_settings() |
| |
| |
| |
| |
| |
| bpy.ops.outliner.orphans_purge(do_recursive=True) |
| for scene in bpy.data.scenes: |
| if scene.rigidbody_world: |
| scene.rigidbody_world.point_cache.frame_start = 1 |
| bpy.ops.ptcache.free_bake_all() |
|
|
| def clear_render_results(): |
| for scene in bpy.data.scenes: |
| for area in bpy.context.screen.areas: |
| if area.type == 'IMAGE_EDITOR': |
| for space in area.spaces: |
| if space.type == 'IMAGE_EDITOR' and space.image: |
| space.image = None |
| if bpy.data.images.get("Render Result"): |
| bpy.data.images.remove(bpy.data.images["Render Result"], do_unlink=True) |
|
|
| def reset_scene(): |
| |
| |
| |
| |
| for obj in bpy.data.objects: |
| if obj.type not in {"CAMERA", "LIGHT"}: |
| bpy.data.objects.remove(obj, do_unlink=True) |
|
|
| |
| for material in bpy.data.materials: |
| if material.users == 0: |
| bpy.data.materials.remove(material, do_unlink=True) |
|
|
| |
| for texture in bpy.data.textures: |
| if texture.users == 0: |
| bpy.data.textures.remove(texture, do_unlink=True) |
|
|
| |
| for image in bpy.data.images: |
| if image.users == 0: |
| bpy.data.images.remove(image, do_unlink=True) |
| |
|
|
| def import_glb(file_path, location=(0, 0, 0), rotation=(0, 0, 0), scale=(0.01, 0.01, 0.01),centering=True): |
| if not os.path.exists(file_path): |
| return None |
| |
| bpy.ops.import_scene.gltf(filepath=file_path) |
|
|
| |
| imported_object = bpy.context.selected_objects[0] |
|
|
| |
| |
| imported_object.rotation_euler = rotation |
| imported_object.scale = scale |
|
|
| |
| if centering: |
| bpy.context.view_layer.objects.active = imported_object |
| bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS') |
|
|
| |
| |
| imported_object.location = location |
| return imported_object |
|
|
|
|
| def create_wall_mesh(name, vertices): |
| |
| mesh = bpy.data.meshes.new(name) |
| obj = bpy.data.objects.new(name, mesh) |
|
|
| |
| scene = bpy.context.scene |
| scene.collection.objects.link(obj) |
|
|
| |
| bpy.context.view_layer.objects.active = obj |
| obj.select_set(True) |
|
|
| |
| bpy.ops.object.mode_set(mode='EDIT') |
|
|
| |
| bm = bmesh.new() |
|
|
| |
| for v in vertices: |
| bm.verts.new(v) |
|
|
| |
| bm.verts.ensure_lookup_table() |
|
|
|
|
| |
| for i in range(len(vertices)-1): |
| bm.edges.new([bm.verts[i], bm.verts[i+1]]) |
|
|
| |
| bm.faces.new(bm.verts) |
|
|
| bpy.ops.object.mode_set(mode='OBJECT') |
|
|
|
|
| |
| bm.to_mesh(mesh) |
| bm.free() |
| return obj |
|
|
|
|
| def create_cube(name, min_xyz, max_xyz,location,rotate=False): |
| |
| print(min_xyz) |
| print(max_xyz) |
| dimensions = [max_xyz[i] - min_xyz[i] for i in range(3)] |
|
|
| |
| |
|
|
| |
| bpy.ops.mesh.primitive_cube_add(size=1, location=location) |
| cube = bpy.context.active_object |
|
|
| |
| cube.dimensions = dimensions |
|
|
| if rotate: |
| bpy.context.view_layer.objects.active = cube |
| bpy.ops.transform.rotate(value=math.radians(90), orient_axis='Z') |
|
|
|
|
| |
| |
| |
| |
| cube.name = name |
| return cube |
|
|
|
|
| def is_image_loaded(image_filepath): |
| for image in bpy.data.images: |
| if image.filepath == image_filepath: |
| return image |
| return None |
|
|
|
|
| def apply_texture_to_object(obj, texture_path): |
| |
| mat = bpy.data.materials.new(name="TextureMaterial") |
| mat.use_nodes = True |
| bsdf = mat.node_tree.nodes.get("Principled BSDF") |
|
|
| |
| tex_image = mat.node_tree.nodes.new('ShaderNodeTexImage') |
| tex_image.image = bpy.data.images.load(texture_path) |
|
|
| |
| mat.node_tree.links.new(bsdf.inputs['Base Color'], tex_image.outputs['Color']) |
|
|
| |
| if obj.data.materials: |
| obj.data.materials[0] = mat |
| else: |
| obj.data.materials.append(mat) |
|
|
|
|
| def add_material(obj, path_texture, add_uv=False, material_pos=-1, texture_scale=(1.8, 1.8), existing_material_name=None): |
| material_id = os.path.basename(path_texture) |
| if add_uv: |
| bpy.ops.object.select_all(action='DESELECT') |
| bpy.context.view_layer.objects.active = obj |
|
|
| obj.select_set(True) |
|
|
| bpy.ops.object.mode_set(mode='EDIT') |
| bpy.ops.mesh.select_all(action='SELECT') |
|
|
| |
| bpy.ops.uv.smart_project() |
|
|
| |
| bpy.ops.object.mode_set(mode='OBJECT') |
|
|
| bpy.context.view_layer.objects.active = obj |
|
|
| |
| existing_material = bpy.data.materials.get(material_id) |
| if existing_material: |
| new_material = existing_material |
| else: |
| |
| new_material = bpy.data.materials.new(name=material_id) |
| new_material.use_nodes = True |
| node_tree = new_material.node_tree |
|
|
| |
| for node in node_tree.nodes: |
| node_tree.nodes.remove(node) |
|
|
| principled_node = node_tree.nodes.new(type='ShaderNodeBsdfPrincipled') |
| |
| image_texture_node = node_tree.nodes.new(type='ShaderNodeTexImage') |
| |
| resolutions = ["1K", "2K", "12K"] |
| for resolution in resolutions: |
| if os.path.exists(f"{path_texture}/{material_id}_{resolution}-JPG_Color.jpg"): |
| break |
| else: |
| print("No texture found for the object.") |
| return |
|
|
| image = is_image_loaded(f"{path_texture}/{material_id}_{resolution}-JPG_Color.jpg") |
| if image is None: |
| image = load_image(f"{path_texture}/{material_id}_{resolution}-JPG_Color.jpg", new_material) |
| image_texture_node.image = image |
|
|
| |
| tex_coord_node = node_tree.nodes.new(type='ShaderNodeTexCoord') |
| mapping_node = node_tree.nodes.new(type='ShaderNodeMapping') |
| mapping_node.inputs['Scale'].default_value[0] = texture_scale[0] |
| mapping_node.inputs['Scale'].default_value[1] = texture_scale[1] |
|
|
| |
| node_tree.links.new(tex_coord_node.outputs['UV'], mapping_node.inputs['Vector']) |
| node_tree.links.new(mapping_node.outputs['Vector'], image_texture_node.inputs['Vector']) |
|
|
| |
| img_normal = is_image_loaded(f"{path_texture}/{material_id}_{resolution}-JPG_NormalGL.jpg") |
| if img_normal is None: |
| img_normal = load_image(f"{path_texture}/{material_id}_{resolution}-JPG_NormalGL.jpg", new_material) |
| image_texture_node_normal = node_tree.nodes.new(type='ShaderNodeTexImage') |
| image_texture_node_normal.image = img_normal |
| image_texture_node_normal.image.colorspace_settings.name = 'Non-Color' |
|
|
| normal_map_node = node_tree.nodes.new(type='ShaderNodeNormalMap') |
|
|
| node_tree.links.new(normal_map_node.outputs["Normal"], principled_node.inputs["Normal"]) |
| node_tree.links.new(image_texture_node_normal.outputs["Color"], normal_map_node.inputs["Color"]) |
|
|
| |
| if os.path.exists(f"{path_texture}/{material_id}_{resolution}-JPG_Roughness.jpg"): |
| img_rough = is_image_loaded(f"{path_texture}/{material_id}_{resolution}-JPG_Roughness.jpg") |
| if img_rough is None: |
| img_rough = load_image(f"{path_texture}/{material_id}_{resolution}-JPG_Roughness.jpg", new_material) |
|
|
| image_texture_node_rough = node_tree.nodes.new(type='ShaderNodeTexImage') |
| image_texture_node_rough.image = img_rough |
| image_texture_node_rough.image.colorspace_settings.name = 'Non-Color' |
|
|
| node_tree.links.new(image_texture_node_rough.outputs["Color"], principled_node.inputs["Roughness"]) |
|
|
| |
| if os.path.exists(f"{path_texture}/{material_id}_{resolution}-JPG_Metalness.jpg"): |
| img_metal = is_image_loaded(f"{path_texture}/{material_id}_{resolution}-JPG_Metalness.jpg") |
| if img_metal is None: |
| img_metal = load_image(f"{path_texture}/{material_id}_{resolution}-JPG_Metalness.jpg", new_material) |
|
|
| image_texture_node_metal = node_tree.nodes.new(type='ShaderNodeTexImage') |
| image_texture_node_metal.image = img_metal |
| image_texture_node_metal.image.colorspace_settings.name = 'Non-Color' |
|
|
| node_tree.links.new(image_texture_node_metal.outputs["Color"], principled_node.inputs["Metallic"]) |
|
|
| |
| node_tree.links.new(image_texture_node.outputs["Color"], principled_node.inputs["Base Color"]) |
| |
| material_output_node = node_tree.nodes.new(type='ShaderNodeOutputMaterial') |
| node_tree.links.new(principled_node.outputs["BSDF"], material_output_node.inputs["Surface"]) |
|
|
| if existing_material_name is not None: |
| |
| for slot in obj.material_slots: |
| |
| if slot.material and existing_material_name.lower() in slot.material.name.lower(): |
| |
| obj.data.materials.append(new_material) |
| new_slot_index = len(obj.material_slots) - 1 |
| |
| for polygon in obj.data.polygons: |
| if polygon.material_index == obj.material_slots.find(slot.name): |
| polygon.material_index = new_slot_index |
| else: |
| |
| if material_pos == -1 or len(obj.data.materials) == 0: |
| obj.data.materials.clear() |
| obj.data.materials.append(new_material) |
| else: |
| obj.data.materials[material_pos] = new_material |
|
|
|
|
| def load_hdri(hdri_path='./data/HDRIs/studio_small_08_4k.exr', hdri_strength=1, hide=True): |
| |
| |
|
|
| if not os.path.exists(hdri_path): |
| current_file = os.path.abspath(__file__) |
| hdri_path = os.path.join(os.path.dirname(os.path.dirname(current_file)), hdri_path) |
|
|
| |
| if not os.path.exists(hdri_path): |
| print("HDRI file not found:", hdri_path) |
| else: |
| |
| world = bpy.data.worlds['World'] |
|
|
| |
| world.use_nodes = True |
| nodes = world.node_tree.nodes |
| links = world.node_tree.links |
|
|
| |
| |
| |
|
|
| |
| env_texture = nodes.new(type='ShaderNodeTexEnvironment') |
|
|
| texture_coord = nodes.new(type="ShaderNodeTexCoord") |
| |
| mapping_node = nodes.new(type='ShaderNodeMapping') |
|
|
| |
| env_texture.image = bpy.data.images.load(hdri_path) |
|
|
| |
| background = nodes.new(type='ShaderNodeBackground') |
| background.location = (-100, 0) |
|
|
| |
| if 'World Output' not in nodes: |
| world_output = nodes.new(type='ShaderNodeOutputWorld') |
| world_output.location = (100, 0) |
| else: |
| world_output = nodes['World Output'] |
|
|
| if hide: |
| |
| |
| light_path = nodes.new(type='ShaderNodeLightPath') |
| mix_shader = nodes.new(type='ShaderNodeMixShader') |
| bg_transparent = nodes.new(type='ShaderNodeBackground') |
| bg_transparent.inputs['Color'].default_value = (0, 0, 0, 1) |
| |
| links.new(light_path.outputs['Is Camera Ray'], mix_shader.inputs['Fac']) |
| links.new(background.outputs['Background'], mix_shader.inputs[1]) |
| links.new(bg_transparent.outputs['Background'], mix_shader.inputs[2]) |
| links.new(mix_shader.outputs['Shader'], world_output.inputs['Surface']) |
| else: |
| links.new(background.outputs['Background'], world_output.inputs['Surface']) |
|
|
| |
| links.new(texture_coord.outputs['Generated'], mapping_node.inputs['Vector']) |
| links.new(mapping_node.outputs['Vector'], env_texture.inputs['Vector']) |
| links.new(env_texture.outputs['Color'], background.inputs['Color']) |
| background.inputs['Strength'].default_value = hdri_strength |
| print("HDRI background set successfully.") |
|
|
|
|
| def setup_camera(center_x, center_y, width, wall_height=.1, wide_lens=False, fov_multiplier=1.1, use_damped_track=False): |
| |
| if "Camera" not in bpy.data.objects: |
| bpy.ops.object.camera_add() |
| cam = bpy.context.object |
| else: |
| cam = bpy.data.objects["Camera"] |
| bpy.context.scene.camera = cam |
|
|
| if wide_lens: |
| cam.data.lens /= 2 |
| |
|
|
| target_width = abs(fov_multiplier * width) |
| fov = 2 * math.atan((cam.data.sensor_width / (2 * cam.data.lens))) |
| cam.location.x = center_x |
| cam.location.y = center_y |
| cam.location.z = wall_height + (target_width / 2) / math.tan(fov / 2) |
|
|
| |
| cam.constraints.clear() |
| if use_damped_track: |
| cam_constraint = cam.constraints.new(type="DAMPED_TRACK") |
| cam_constraint.track_axis = "TRACK_NEGATIVE_Z" |
| else: |
| cam_constraint = cam.constraints.new(type="TRACK_TO") |
| cam_constraint.track_axis = "TRACK_NEGATIVE_Z" |
| cam_constraint.up_axis = "UP_Y" |
|
|
| empty = bpy.data.objects.new("Empty", None) |
| empty.location = (center_x, center_y, 0) |
| bpy.context.scene.collection.objects.link(empty) |
| cam_constraint.target = empty |
| |
| return cam, cam_constraint |
|
|
|
|
| def world_to_camera_view(scene, camera, coord): |
| """Convert world coordinates to camera view coordinates""" |
| co_local = camera.matrix_world.normalized().inverted() @ coord |
| z = -co_local.z |
|
|
| camera_data = camera.data |
| frame = [-v for v in camera_data.view_frame(scene=scene)[:3]] |
| if camera_data.type != 'ORTHO': |
| frame = [(v / (v.z / z)) for v in frame] |
|
|
| min_x, max_x = frame[1].x, frame[2].x |
| min_y, max_y = frame[0].y, frame[1].y |
| x = (co_local.x - min_x) / (max_x - min_x) |
| y = (co_local.y - min_y) / (max_y - min_y) |
| return Vector((x, y, z)) |
|
|
|
|
| def get_pixel_coordinates(scene, camera, world_coord): |
| """Get pixel coordinates for a given world coordinate""" |
| if isinstance(world_coord, np.ndarray) or isinstance(world_coord, list): |
| world_coord = Vector(world_coord) |
| coord_2d = world_to_camera_view(scene, camera, world_coord) |
| return (coord_2d.x, 1 - coord_2d.y) |
| |
| |
| |
| |
| |
| |
|
|
|
|
| def set_rendering_settings(panorama=False, high_res=False) -> None: |
| render = bpy.context.scene.render |
| render.engine = 'CYCLES' |
| render.image_settings.file_format = "PNG" |
| render.image_settings.color_mode = "RGBA" |
|
|
| |
| try: |
| bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'CUDA' |
| for device in bpy.context.preferences.addons['cycles'].preferences.devices: |
| if device.type == 'CUDA': |
| device.use = True |
| except: |
| print('no CUDA devices found') |
|
|
| render.resolution_x = 512 |
| render.resolution_y = 512 |
| |
| |
| |
| |
| |
|
|
| render.resolution_percentage = 50 |
| if high_res: |
| render.resolution_x = 1080 |
| render.resolution_y = 1080 |
| render.resolution_percentage = 100 |
| bpy.context.scene.cycles.device = "GPU" |
| bpy.context.scene.cycles.samples = 128 |
|
|
| bpy.context.scene.cycles.diffuse_bounces = 3 |
| bpy.context.scene.cycles.glossy_bounces = 3 |
| bpy.context.scene.cycles.transparent_max_bounces = 5 |
| bpy.context.scene.cycles.transmission_bounces = 5 |
| bpy.context.scene.cycles.filter_width = 0.01 |
|
|
| bpy.context.scene.cycles.use_denoising = True |
| indoor_camera = False |
| if indoor_camera: |
| bpy.context.scene.render.film_transparent = False |
| else: |
| bpy.context.scene.render.film_transparent = True |
|
|
|
|
| def display_vertex_color(): |
| |
| bpy.ops.object.mode_set(mode='OBJECT') |
|
|
| |
| obj = bpy.context.active_object |
|
|
| |
| material = bpy.data.materials.new(name="ColorAttributeMaterial") |
| material.use_nodes = True |
| nodes = material.node_tree.nodes |
|
|
| |
| for node in nodes: |
| nodes.remove(node) |
|
|
| |
| bsdf = nodes.new(type='ShaderNodeBsdfPrincipled') |
| output = nodes.new(type='ShaderNodeOutputMaterial') |
|
|
| |
| material.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface']) |
|
|
| |
| bsdf.location = (0, 0) |
| output.location = (200, 0) |
|
|
| |
| if "Col" in obj.data.attributes: |
| |
| attr = nodes.new(type='ShaderNodeAttribute') |
| attr.attribute_name = "Col" |
|
|
| |
| material.node_tree.links.new(attr.outputs['Color'], bsdf.inputs['Base Color']) |
| else: |
| print("Object has no color attribute.") |
|
|
| |
| if obj.data.materials: |
| obj.data.materials[0] = material |
| else: |
| obj.data.materials.append(material) |
|
|
|
|
| if __name__ == '__main__': |
| parser = argparse.ArgumentParser(description='Renders given obj file by rotation a camera around it.') |
|
|
| parser.add_argument( |
| '--output', |
| type=str, |
| default='/viscam/u/sgu33/GenLayout/temp_rendering/', |
| help='The path the output will be dumped to.' |
| ) |
| parser.add_argument( |
| '--json', |
| help='path to the json to load from holodeck' |
| ) |
|
|
| parser.add_argument( |
| '--content', |
| help='path to content for loading windows and doors' |
| ) |
|
|
| parser.add_argument( |
| '--asset_source', |
| help='default should be objaverse' |
| ) |
|
|
| parser.add_argument( |
| '--asset_dir', |
| help='path to the asset respository' |
| ) |
|
|
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
|
|