Download export_unordered_cams.py from RoboticImaging/3D-Reconstruction-Asset-Inspection-Benchmark: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/datasets/RoboticImaging/3D-Reconstruction-Asset-Inspection-Benchmark/resolve/main/export_unordered_cams.py
- Command line
-
hf download hf://datasets/RoboticImaging/3D-Reconstruction-Asset-Inspection-Benchmark/export_unordered_cams.py
-
curl -L -o export_unordered_cams.py https://huggingface.co/datasets/RoboticImaging/3D-Reconstruction-Asset-Inspection-Benchmark/resolve/main/export_unordered_cams.py
2.75 kB
| import bpy | |
| import os | |
| import json | |
| import mathutils | |
| import itertools | |
| import tomllib | |
| import math | |
| import sys | |
| traj_name = "proxy_surface" | |
| params_dir = "unordered_cams/" | |
| output_dir = bpy.path.abspath("//") | |
| with open(f"{output_dir}/scene_data.toml", "rb") as f: | |
| data = tomllib.load(f) | |
| scene_data = data["scene_data"] | |
| def main(): | |
| offset_dict = data['traj2offset'] | |
| offsets = offset_dict.values() | |
| scene = bpy.context.scene | |
| os.makedirs(output_dir, exist_ok=True) | |
| for offset in offsets: | |
| if not math.isnan(offset): | |
| scene.splatworks_props.camera_proxy_offset = offset | |
| save_cam_data(offset) | |
| def save_cam_data(offset): | |
| os.makedirs(output_dir, exist_ok=True) | |
| valid_cams = get_valid_cam_names() | |
| n_valid_cams = len(valid_cams) | |
| param_frame_dir = os.path.join( | |
| output_dir, params_dir, f"traj={traj_name}_{offset}" | |
| ) | |
| os.makedirs(param_frame_dir, exist_ok=True) | |
| for frame in range(n_valid_cams): | |
| cam_name = valid_cams[frame] | |
| cam = bpy.data.objects[cam_name] | |
| frame_string = str("%08d" % frame) | |
| cam_data_name = os.path.join( | |
| param_frame_dir, frame_string + ".json") | |
| export_cam_parameters(cam_data_name, cam) | |
| def get_valid_cam_names(): | |
| cam_names = bpy.data.collections['SplatWorks_Cameras'].all_objects.keys() | |
| valid_cam_names = [] | |
| for cam_name in cam_names: | |
| height = get_cam_z_coord(cam_name) | |
| if height > scene_data["min_height"]: | |
| valid_cam_names.append(cam_name) | |
| return valid_cam_names | |
| def get_cam_z_coord(cam_name): | |
| cam_obj = bpy.data.objects[cam_name] | |
| loc = cam_obj.location | |
| return loc[2] | |
| def export_cam_parameters(f_name, cam_obj): | |
| """ | |
| Export the camera parameters for a given camera object to a file | |
| """ | |
| rot_mode = cam_obj.rotation_mode | |
| rot_mode_tuple = tuple(rot_mode) | |
| if rot_mode_tuple in list(itertools.permutations("XYZ", 3)): | |
| eul = mathutils.Euler(cam_obj.rotation_euler, rot_mode) | |
| rot_out = eul.to_quaternion() | |
| elif rot_mode == "AXIS_ANGLE": | |
| raise NotImplementedError() | |
| else: # quaternion case | |
| rot_out = mathutils.Quaternion(cam_obj.rotation_quaternion) | |
| out_dict = { | |
| "name": cam_obj.name, | |
| "loc": tuple(cam_obj.location), | |
| "rot": [rot_out.w, rot_out.x, rot_out.y, rot_out.z], | |
| "focal_length": cam_obj.data.lens, | |
| "sensor_width": cam_obj.data.sensor_width, | |
| "clip_end": cam_obj.data.clip_end, | |
| "clip_start": cam_obj.data.clip_start, | |
| } | |
| # print(cam_obj.name) | |
| # print(f_name, cam_obj.location) | |
| with open(f_name, "w") as f: | |
| json.dump(out_dict, f, indent="\t") | |
| if __name__ == "__main__": | |
| main() | |