|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import trimesh |
|
|
import json |
|
|
import numpy as np |
|
|
|
|
|
PI = np.pi |
|
|
|
|
|
|
|
|
MODEL_SCALE = 1.0 |
|
|
MARKER_FRAC = 0.02 |
|
|
OFFSET_FRAC_Z = 0.746 |
|
|
ROT_Z_DEG = -90.0 |
|
|
OFFSET_FRAC_Y = 0.5 |
|
|
|
|
|
def _to_fused_mesh(obj): |
|
|
if isinstance(obj, trimesh.Trimesh): |
|
|
return obj |
|
|
if isinstance(obj, trimesh.Scene): |
|
|
try: |
|
|
return obj.to_mesh() |
|
|
except Exception: |
|
|
geoms = list(obj.dump().geometry.values()) |
|
|
return trimesh.util.concatenate(geoms) |
|
|
raise TypeError(f"Unsupported type: {type(obj)}") |
|
|
|
|
|
def create_model_data(id="terminal"): |
|
|
file_path = f"./{id}.glb" |
|
|
save_path = f"./model_data.json" |
|
|
|
|
|
|
|
|
with open(file_path, "rb") as f: |
|
|
loaded = trimesh.load(f, file_type="glb") |
|
|
mesh = _to_fused_mesh(loaded) |
|
|
|
|
|
|
|
|
mesh.apply_scale(MODEL_SCALE) |
|
|
scene = trimesh.Scene(mesh) |
|
|
|
|
|
|
|
|
obb = mesh.bounding_box_oriented |
|
|
center = obb.centroid |
|
|
ext = obb.extents |
|
|
diag = float(np.linalg.norm(ext)) + 1e-12 |
|
|
|
|
|
axis_len = diag * MARKER_FRAC * 12.0 |
|
|
origin_sz = diag * MARKER_FRAC * 0.6 |
|
|
r_target = diag * MARKER_FRAC * 1.2 |
|
|
|
|
|
red = [1.0, 0.0, 0.0, 0.5] |
|
|
green = [0.0, 1.0, 0.0, 0.5] |
|
|
blue = [0.0, 0.0, 1.0, 0.5] |
|
|
|
|
|
|
|
|
T_obb = obb.primitive.transform |
|
|
R_obb = T_obb[:3, :3] |
|
|
top_center = center + R_obb[:, 2] * (ext[2] / 2.0) |
|
|
|
|
|
T_target = np.eye(4, dtype=float) |
|
|
T_target[:3, :3] = R_obb |
|
|
T_target[:3, 3] = top_center |
|
|
|
|
|
|
|
|
offset_dist_z = ext[2] * OFFSET_FRAC_Z |
|
|
T_offset_z = trimesh.transformations.translation_matrix([0.0, 0.0, -offset_dist_z]) |
|
|
Rz_local = trimesh.transformations.rotation_matrix(np.deg2rad(ROT_Z_DEG), [0.0, 0.0, 1.0]) |
|
|
offset_dist_y = ext[1] * OFFSET_FRAC_Y |
|
|
T_offset_y = trimesh.transformations.translation_matrix([0.0, offset_dist_y, 0.0]) |
|
|
|
|
|
|
|
|
T_final = T_target @ T_offset_z @ Rz_local @ T_offset_y |
|
|
|
|
|
|
|
|
target_sphere = trimesh.creation.icosphere(subdivisions=2, radius=r_target) |
|
|
target_sphere.apply_transform(T_final) |
|
|
target_sphere.visual.vertex_colors = np.array([red] * len(target_sphere.vertices)) |
|
|
scene.add_geometry(target_sphere) |
|
|
|
|
|
axis = trimesh.creation.axis( |
|
|
origin_size=origin_sz, |
|
|
axis_length=axis_len, |
|
|
axis_radius=origin_sz * 0.3, |
|
|
) |
|
|
axis.apply_transform(T_final) |
|
|
scene.add_geometry(axis) |
|
|
|
|
|
print("extents =", ext) |
|
|
print("center =", center) |
|
|
print("target pose (before offset/rot) =\n", T_target) |
|
|
print("final target pose (after z-offset, z-rot, y-offset) =\n", T_final) |
|
|
|
|
|
|
|
|
transform_matrix = trimesh.transformations.euler_matrix(0, 0, 0).tolist() |
|
|
data = { |
|
|
"center": center.tolist(), |
|
|
"extents": ext.tolist(), |
|
|
"scale": [MODEL_SCALE, MODEL_SCALE, MODEL_SCALE], |
|
|
"target_pose": [T_final.tolist()], |
|
|
"contact_points_pose": [], |
|
|
"transform_matrix": transform_matrix, |
|
|
"functional_matrix": [], |
|
|
"orientation_point": T_final.tolist(), |
|
|
"contact_points_group": [], |
|
|
"contact_points_mask": [], |
|
|
"contact_points_discription": [], |
|
|
"target_point_discription": [ |
|
|
"OBB +Z face center → local -Z offset → local Z rot → local +Y offset." |
|
|
], |
|
|
"functional_point_discription": [""], |
|
|
"orientation_point_discription": ["Same as target for schema compatibility."] |
|
|
} |
|
|
with open(save_path, "w", encoding="utf-8") as f: |
|
|
json.dump(data, f, indent=4, ensure_ascii=False, separators=(",", ": ")) |
|
|
|
|
|
scene.show() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
create_model_data("terminal") |
|
|
|