import bpy import os import json import math import numpy as np import random from math import radians import struct import mathutils # --- Config Parameters --- CONFIG = { "RESULTS_PATH": os.environ.get("RESULTS_PATH"), "EXPOSURES": [0.125, 0.25, 1.0, 2.0, 8.0], # "EXPOSURES": [0.666, 0.333, 0.166, 0.1, 0.05], # "EXPOSURES": [0.333, 0.125, 0.066, 0.0333, 0.0166], # "EXPOSURES": [0.333, 0.166, 0.1, 0.05, 0.0222], # "EXPOSURES": [2.0, 1.0, 0.5, 0.25, 0.125], # "EXPOSURES": [0.03125, 0.125, 0.5, 2.0, 8], # "EXPOSURES": [0.0625, 0.25, 1, 4.0, 16], # "EXPOSURES": [0.125, 0.5, 2.0, 8.0, 32.0], # "EXPOSURES": [8, 32, 128, 512, 2048], # ================ Upload From Camera ================ "ROWS": 5, "COLS": 7, "ROT_STEP": 2.5, # "ROT_STEP": 5.0, "RESOLUTION": 448, "SAMPLES": 2048, "TONEMAP_POOL": ['AgX', 'Filmic', 'Standard'], } chosen_tonemap = None import math def update_config_from_camera(): cam = bpy.data.objects.get('Camera') if not cam: print("Error: No object named 'Camera' found in the scene") return loc = cam.location CONFIG["INIT_LOC"] = np.array([[loc.x], [loc.z], [-loc.y]]) CONFIG["RADIUS"] = loc.length CONFIG["MAX_DISTANCE"] = loc.length * 1.5 rot = cam.rotation_euler pitch = math.degrees(rot.x) - 90 yaw = math.degrees(rot.z) roll = math.degrees(rot.y) CONFIG["INIT_ROT"] = [pitch, yaw, roll] print(f"--- CONFIG updated automatically ---") print(f"INIT_LOC: {CONFIG['INIT_LOC'].flatten()}") print(f"INIT_ROT: {CONFIG['INIT_ROT']}") print(f"RADIUS: {CONFIG['RADIUS']:.4f}") print(f"MAX_DISTANCE: {CONFIG['MAX_DISTANCE']:.4f}") def setup_environment(): global chosen_tonemap scene = bpy.context.scene scene.render.engine = 'CYCLES' scene.cycles.samples = CONFIG["SAMPLES"] scene.render.resolution_x = CONFIG["RESOLUTION"] scene.render.resolution_y = CONFIG["RESOLUTION"] scene.render.resolution_percentage = 100 scene.render.dither_intensity = 0.0 scene.render.film_transparent = True scene.render.use_persistent_data = True # Default HDR output settings scene.render.image_settings.file_format = 'OPEN_EXR' scene.render.image_settings.color_depth = '32' # Randomly select Tonemapping chosen_tonemap = random.choice(CONFIG["TONEMAP_POOL"]) if chosen_tonemap is None else chosen_tonemap bpy.context.scene.view_settings.view_transform = chosen_tonemap # GPU acceleration try: cprefs = bpy.context.preferences.addons['cycles'].preferences cprefs.compute_device_type = 'OPTIX' cprefs.get_devices() for d in cprefs.devices: d.use = True scene.cycles.device = 'GPU' except: print("Using CPU Rendering...") def setup_nodes(save_root): scene = bpy.context.scene scene.use_nodes = True tree = scene.node_tree tree.nodes.clear() rl = tree.nodes.new('CompositorNodeRLayers') scene.view_layers["ViewLayer"].use_pass_normal = True scene.view_layers["ViewLayer"].use_pass_z = True # --- 1. Depth and normal output (PNG) --- aux_out = tree.nodes.new('CompositorNodeOutputFile') aux_out.format.file_format = 'PNG' map_node = tree.nodes.new('CompositorNodeMapRange') map_node.inputs['From Max'].default_value = CONFIG["MAX_DISTANCE"] map_node.inputs['To Min'].default_value = 1 map_node.inputs['To Max'].default_value = 0 tree.links.new(rl.outputs['Depth'], map_node.inputs[0]) aux_out.file_slots[0].path = "depth_" tree.links.new(map_node.outputs[0], aux_out.inputs[0]) aux_out.file_slots.new("normal_") tree.links.new(rl.outputs['Normal'], aux_out.inputs[1]) # --- 2. Exposure bracket LDR output --- ldr_out = tree.nodes.new('CompositorNodeOutputFile') ldr_out.format.file_format = 'PNG' ldr_out.base_path = "" ldr_out.file_slots.clear() for i, exp_val in enumerate(CONFIG["EXPOSURES"]): mix_node = tree.nodes.new('CompositorNodeMixRGB') mix_node.blend_type = 'MULTIPLY' mix_node.inputs[2].default_value = (exp_val, exp_val, exp_val, 1) mix_node.inputs[0].default_value = 1.0 slot_name = f"ldr_exp_{str(i).replace('.', '_')}_" ldr_out.file_slots.new(slot_name) tree.links.new(rl.outputs['Image'], mix_node.inputs[1]) tree.links.new(mix_node.outputs[0], ldr_out.inputs[i]) return aux_out, ldr_out def get_pose_matrix(pitch_deg, yaw_deg, radius): phi, theta = radians(pitch_deg), radians(yaw_deg) trans_t = np.array([[1,0,0,0],[0,1,0,0],[0,0,1,radius],[0,0,0,1]], dtype=float) rot_phi = np.array([[1,0,0,0],[0,np.cos(phi),-np.sin(phi),0],[0,np.sin(phi),np.cos(phi),0],[0,0,0,1]], dtype=float) rot_theta = np.array([[np.cos(theta),0,np.sin(theta),0],[0,1,0,0],[-np.sin(theta),0,np.cos(theta),0],[0,0,0,1]], dtype=float) return rot_theta @ (rot_phi @ trans_t) def save_as_cameras_bin(data, file_path): if os.path.exists(file_path): print(f"Already have {file_path}. Skipping...") return w = int(data["w"]) # Width h = int(data["h"]) # Height fx = data["fl_x"] # Horizontal focal length fy = data["fl_y"] # Vertical focal length cx = data["cx"] # Principal point X cy = data["cy"] # Principal point Y with open(file_path, "wb") as f: # A. First write the camera count: 1 camera (format Q represents uint64) f.write(struct.pack(" 64 bytes f.write(struct.pack( "