|
|
import trimesh |
|
|
import json |
|
|
import numpy as np |
|
|
|
|
|
PI = np.pi |
|
|
|
|
|
|
|
|
|
|
|
MODEL_SCALE = 0.004 |
|
|
|
|
|
GRASP_OFFSET = [0., 0., 0.] |
|
|
GRASP_ROT = [PI, 0.0, PI] |
|
|
|
|
|
def _to_scale_vec(scale): |
|
|
"""将标量/数组统一成 3 维缩放向量 [sx, sy, sz].""" |
|
|
arr = np.asarray(scale, dtype=float) |
|
|
if arr.ndim == 0: |
|
|
return [float(arr)] * 3 |
|
|
if arr.shape == (3,): |
|
|
return arr.tolist() |
|
|
raise ValueError(f"MODEL_SCALE 需为标量或长度为3的向量,收到形状: {arr.shape}") |
|
|
|
|
|
def create_model_data(id="vase"): |
|
|
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 = loaded.to_mesh() if isinstance(loaded, trimesh.Scene) else loaded |
|
|
|
|
|
|
|
|
scale_vec = _to_scale_vec(MODEL_SCALE) |
|
|
mesh.apply_scale(scale_vec) |
|
|
|
|
|
|
|
|
obb = mesh.bounding_box_oriented |
|
|
center = obb.centroid |
|
|
ext = obb.extents |
|
|
diag = np.linalg.norm(ext) |
|
|
|
|
|
|
|
|
rotation_matrix = obb.transform[:3, :3] |
|
|
bottom_center = center - rotation_matrix[:, 2] * (ext[2] / 2.0) |
|
|
mesh.apply_translation(-bottom_center) |
|
|
|
|
|
|
|
|
obb = mesh.bounding_box_oriented |
|
|
center = obb.centroid |
|
|
ext = obb.extents |
|
|
rotation_matrix = obb.transform[:3, :3] |
|
|
|
|
|
|
|
|
top_center = center + rotation_matrix[:, 2] * (ext[2] / 2.0) |
|
|
|
|
|
|
|
|
grasp_translation = trimesh.transformations.translation_matrix(top_center) |
|
|
grasp_rotation = trimesh.transformations.euler_matrix(*GRASP_ROT) |
|
|
grasp_offset = trimesh.transformations.translation_matrix(GRASP_OFFSET) |
|
|
grasp_transform = grasp_translation @ grasp_rotation @ grasp_offset |
|
|
|
|
|
|
|
|
scene = trimesh.Scene(mesh) |
|
|
grasp_sphere = trimesh.creation.icosphere(subdivisions=2, radius=diag * 0.05) |
|
|
grasp_sphere.apply_transform(grasp_transform) |
|
|
grasp_sphere.visual.vertex_colors = np.array([[1.0, 0.0, 0.0, 0.5]] * len(grasp_sphere.vertices)) |
|
|
scene.add_geometry(grasp_sphere) |
|
|
|
|
|
grasp_axis = trimesh.creation.axis(origin_size=0.05, axis_length=diag * 0.2) |
|
|
grasp_axis.apply_transform(grasp_transform) |
|
|
scene.add_geometry(grasp_axis) |
|
|
|
|
|
|
|
|
data = { |
|
|
"center": center.tolist(), |
|
|
"extents": ext.tolist(), |
|
|
"scale": scale_vec, |
|
|
"contact_points_pose": [grasp_transform.tolist()], |
|
|
"transform_matrix": trimesh.transformations.identity_matrix().tolist(), |
|
|
"functional_matrix": [], |
|
|
"orientation_point": grasp_transform.tolist(), |
|
|
"contact_points_group": [], |
|
|
"contact_points_mask": [], |
|
|
"contact_points_discription": [], |
|
|
"target_point_discription": ["Grasp pose at the top edge center."], |
|
|
"functional_point_discription": [""], |
|
|
"orientation_point_discription": ["Grasp pose with adjustable offset and rotation."] |
|
|
} |
|
|
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("vase") |
|
|
|