""" Huhb3D Blender Import Script ============================= Imports Huhb3D dataset depth maps and 6DoF poses into Blender for visualization and Sim2Real domain adaptation. COORDINATE CONVERSION (Huhb3D/OpenCV -> Blender): Huhb3D uses OpenCV convention: Y-down, Z-forward Blender uses OpenGL convention: Y-up, Z-backward Conversion: R_blender = R_opencv * diag(1, -1, -1) t_blender = [t_x, -t_y, -t_z] Example: R_opencv = [[1, 0, 0], [0, -1, 0], [0, 0, -1]] (180 deg X rotation) R_blender = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] (identity) Usage (in Blender Scripting workspace): exec(open("/path/to/huhb3d_blender_import.py").read()) import_huhb3d("/path/to/sell_Huhb3D-Test-Precision-v4", "flange", frame=1) """ import bpy import bmesh import json import mathutils import numpy as np from pathlib import Path def load_depth_as_mesh(depth_path, K, depth_scale=1.0, scale=0.001): depth = cv2_imread_unchanged(depth_path) if depth is None: raise FileNotFoundError(f"Cannot read: {depth_path}") h, w = depth.shape fx, fy = K[0, 0], K[1, 1] cx, cy = K[0, 2], K[1, 2] verts = [] faces = [] vi = 0 for v in range(h): for u in range(w): z_mm = depth[v, u] * depth_scale if z_mm <= 0: vi += 1 continue z_m = z_mm * scale x_m = (u - cx) * z_m / fx y_m = (v - cy) * z_m / fy verts.append((x_m, -y_m, -z_m)) vi += 1 mesh = bpy.data.meshes.new("Huhb3D_DepthMesh") mesh.from_pydata(verts, [], faces) obj = bpy.data.objects.new("Huhb3D_DepthMesh", mesh) bpy.context.collection.objects.link(obj) return obj def opencv_to_blender_rotation(R_opencv): flip = np.diag([1.0, -1.0, -1.0]) R_blender = R_opencv @ flip return R_blender def opencv_to_blender_translation(t_opencv): return np.array([t_opencv[0], -t_opencv[1], -t_opencv[2]]) def set_camera_from_intrinsics(K, image_width=800, image_height=600): if 'Huhb3D_Camera' in bpy.data.cameras: cam_data = bpy.data.cameras['Huhb3D_Camera'] else: cam_data = bpy.data.cameras.new('Huhb3D_Camera') if 'Huhb3D_Camera' in bpy.data.objects: cam_obj = bpy.data.objects['Huhb3D_Camera'] else: cam_obj = bpy.data.objects.new('Huhb3D_Camera', cam_data) bpy.context.collection.objects.link(cam_obj) fx = K[0, 0] sensor_width_mm = 36.0 focal_length_mm = fx * sensor_width_mm / image_width cam_data.lens = focal_length_mm cam_data.sensor_width = sensor_width_mm cam_data.sensor_height = sensor_width_mm * image_height / image_width cam_obj.rotation_euler = (0, 0, 0) cam_obj.location = (0, 0, 0) return cam_obj def set_object_pose_from_gt(obj_name, R_m2c, t_m2c): if obj_name not in bpy.data.objects: print(f"Object '{obj_name}' not found in Blender scene") return obj = bpy.data.objects[obj_name] R_blender = opencv_to_blender_rotation(R_m2c) t_blender = opencv_to_blender_translation(t_m2c) rot_mat = mathutils.Matrix(R_blender.tolist()).to_4x4() rot_mat.translation = mathutils.Vector(t_blender * 0.001) obj.matrix_world = rot_mat print(f"Set pose for '{obj_name}': t_blender={t_blender * 0.001} m") def cv2_imread_unchanged(path): try: import cv2 return cv2.imread(str(path), cv2.IMREAD_UNCHANGED) except ImportError: from PIL import Image import numpy as np img = Image.open(str(path)) return np.array(img) def import_huhb3d(dataset_dir, object_name, frame=1): dataset_dir = Path(dataset_dir) obj_dir = dataset_dir / object_name if not obj_dir.exists(): raise FileNotFoundError(f"Object directory not found: {obj_dir}") with open(obj_dir / 'scene_camera.json') as f: scene_cam = json.load(f) cam = scene_cam[str(frame)] K = np.array(cam['cam_K']).reshape(3, 3) depth_scale = cam.get('depth_scale', 1.0) with open(obj_dir / 'scene_gt.json') as f: scene_gt = json.load(f) gt = scene_gt[str(frame)][0] R_m2c = np.array(gt['cam_R_m2c']).reshape(3, 3) t_m2c = np.array(gt['cam_t_m2c']) cam_obj = set_camera_from_intrinsics(K) depth_path = obj_dir / 'depth' / f'depth_{frame:04d}.png' if depth_path.exists(): depth_mesh = load_depth_as_mesh(str(depth_path), K, depth_scale) print(f"Imported depth mesh: {depth_path}") set_object_pose_from_gt(object_name, R_m2c, t_m2c) print(f"Camera focal length: {cam_obj.data.lens:.2f} mm") print(f"R_m2c (OpenCV):\n{R_m2c}") print(f"t_m2c (OpenCV): {t_m2c} mm") print(f"R_blender:\n{opencv_to_blender_rotation(R_m2c)}") print(f"t_blender: {opencv_to_blender_translation(t_m2c)} mm") return { 'K': K, 'R_m2c': R_m2c, 't_m2c': t_m2c, 'depth_scale': depth_scale, } if __name__ == '__main__': print("Run this script inside Blender's Scripting workspace:") print(" exec(open('huhb3d_blender_import.py').read())") print(" import_huhb3d('/path/to/dataset', 'flange', frame=1)")