Download objects/badminton_annotated/models.py from martelzhang/RoboMonster_assets: direct link, hf CLI and curl.
- Browser
- Download file 4.47 kB
-
https://huggingface.co/datasets/martelzhang/RoboMonster_assets/resolve/main/objects/badminton_annotated/models.py
- Command line
-
hf download hf://datasets/martelzhang/RoboMonster_assets/objects/badminton_annotated/models.py
-
curl -L -o models.py https://huggingface.co/datasets/martelzhang/RoboMonster_assets/resolve/main/objects/badminton_annotated/models.py
4.47 kB
| # model_card_scaled.py | |
| import trimesh | |
| import json | |
| import numpy as np | |
| PI = np.pi | |
| # === 你可以调这些参数(contact在世界坐标系) === | |
| MODEL_SCALE = 1.0 # 几何整体缩放 | |
| MARKER_FRAC = 0.02 # 标注物相对尺寸(基于OBB对角线) | |
| CONTACT_POS = (0.0, 0.09, 0.035) # contact 世界坐标位置 —— 默认原点 | |
| CONTACT_RPY = (0.0, 0.0, 0.0) # contact 欧拉角(XYZ顺序), 单位: 弧度 | |
| CONTACT_RADIUS_SCALE = 1.0 # contact 球半径系数(相对于默认 r = diag*MARKER_FRAC*1.0) | |
| AXIS_ORIGIN_SIZE_SCALE = 1.0 # contact处坐标轴的“轴心球”尺寸缩放 | |
| def create_model_data(id="badminton"): | |
| file_path = f"./{id}.glb" | |
| save_path = f"./model_data.json" | |
| # 1) 读模型并烘焙为单一 Trimesh | |
| 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 | |
| # 2) 缩放 | |
| mesh.apply_scale(MODEL_SCALE) | |
| scene = trimesh.Scene(mesh) | |
| # 3) 尺寸 & 标注尺度 | |
| obb = mesh.bounding_box_oriented | |
| center = np.asarray(obb.centroid) | |
| ext = np.asarray(obb.extents) | |
| diag = float(np.linalg.norm(ext)) + 1e-12 | |
| axis_len = diag * MARKER_FRAC * 6.0 | |
| origin_sz = diag * MARKER_FRAC * 0.6 | |
| r_target = diag * MARKER_FRAC * 1.2 | |
| r_contact = diag * MARKER_FRAC * 1.0 * float(CONTACT_RADIUS_SCALE) | |
| red = [1.0, 0.0, 0.0, 0.5] | |
| green = [0.0, 1.0, 0.0, 0.5] | |
| # 4) Target point:OBB 质心 | |
| target_sphere = trimesh.creation.icosphere(subdivisions=2, radius=r_target) | |
| T_target = trimesh.transformations.translation_matrix(center) | |
| target_sphere.apply_transform(T_target) | |
| target_sphere.visual.vertex_colors = np.array([red] * len(target_sphere.vertices)) | |
| scene.add_geometry(target_sphere) | |
| target_points_list = [T_target.tolist()] | |
| # 5) 仅一个 contact:固定在世界坐标 CONTACT_POS(默认原点) | |
| contact_sphere = trimesh.creation.icosphere(subdivisions=2, radius=r_contact) | |
| T_contact = ( | |
| trimesh.transformations.translation_matrix(np.asarray(CONTACT_POS)) @ | |
| trimesh.transformations.euler_matrix(*CONTACT_RPY) | |
| ) | |
| contact_sphere.apply_transform(T_contact) | |
| contact_sphere.visual.vertex_colors = np.array([red] * len(contact_sphere.vertices)) | |
| # 可视化局部坐标轴 | |
| axis = trimesh.creation.axis(origin_size=origin_sz * float(AXIS_ORIGIN_SIZE_SCALE)) | |
| axis.apply_transform(T_contact) | |
| scene.add_geometry(axis) | |
| scene.add_geometry(contact_sphere) | |
| contact_points_list = [T_contact.tolist()] | |
| contact_desc = ["Single contact at world origin (editable at top)."] | |
| # 6) Orientation 参考点(仍放 OBB +Y 顶端,便于对齐/校验) | |
| # 如不需要,注释掉这段亦可 | |
| # 从 OBB primitive 拿旋转矩阵 R_obb(若不可用则回退单位阵) | |
| if hasattr(obb, "primitive") and hasattr(obb.primitive, "transform"): | |
| R_obb = np.asarray(obb.primitive.transform)[:3, :3] | |
| else: | |
| R_obb = np.eye(3) | |
| orient = trimesh.creation.icosphere(subdivisions=2, radius=origin_sz) | |
| T_orient = trimesh.transformations.translation_matrix(center + R_obb[:, 1] * (ext[1] / 2.0)) | |
| orient.apply_transform(T_orient) | |
| orient.visual.vertex_colors = np.array([green] * len(orient.vertices)) | |
| scene.add_geometry(orient) | |
| # 7) 写 JSON | |
| 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": target_points_list, | |
| "contact_points_pose": contact_points_list, # 只有1个 | |
| "transform_matrix": transform_matrix, | |
| "functional_matrix": [], | |
| "orientation_point": T_orient.tolist(), | |
| "contact_points_group": [[0]], # 单组:仅索引0 | |
| "contact_points_mask": [True], # 单个有效 | |
| "contact_points_discription": contact_desc, | |
| "target_point_discription": ["Object centroid."], | |
| "functional_point_discription": [""], | |
| "orientation_point_discription": ["OBB +Y reference."] | |
| } | |
| with open(save_path, "w") as f: | |
| json.dump(data, f, indent=4, separators=(",", ": ")) | |
| scene.show() | |
| if __name__ == "__main__": | |
| create_model_data("badminton") | |