File size: 2,745 Bytes
7093965 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 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()
|