File size: 4,513 Bytes
69f15ae |
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 104 105 106 107 108 109 110 111 112 113 114 |
# model_card_scaled.py
import trimesh
import json
import numpy as np
PI = np.pi
# === 可调参数 ===
MODEL_SCALE = 1.0 # 几何整体缩放
MARKER_FRAC = 0.02 # 标注物相对尺寸(基于 OBB 对角线)
CONTACT_POS = (-0.035, 0.045, -0.5) # 目标点位置
CONTACT_RPY = (-PI/2, 0.0, PI/2) # 目标点姿态 (XYZ/弧度)
CONTACT_RADIUS_SCALE = 1.2 # 目标标记球半径系数
TARGET_POS = (0., 0.045, -0.45) # 抓取/吸附点位置
TARGET_RPY = (-PI/2, 0.0, -PI/2) # 抓取点姿态 (XYZ/弧度)
TARGET_RADIUS_SCALE = 1.0 # 抓取标记球半径系数
# 可视化小坐标轴大小(两者共用)
AXIS_ORIGIN_SIZE_SCALE = 1.0
def create_model_data(id="barrel"):
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
origin_sz = diag * MARKER_FRAC * 0.6
r_target = diag * MARKER_FRAC * float(TARGET_RADIUS_SCALE)
r_contact = diag * MARKER_FRAC * float(CONTACT_RADIUS_SCALE)
red = [1.0, 0.0, 0.0, 0.5]
green = [0.0, 1.0, 0.0, 0.5]
# 4) target 点
target_sphere = trimesh.creation.icosphere(subdivisions=2, radius=r_target)
T_target = (
trimesh.transformations.translation_matrix(np.asarray(TARGET_POS)) @
trimesh.transformations.euler_matrix(*TARGET_RPY)
)
target_sphere.apply_transform(T_target)
target_sphere.visual.vertex_colors = np.array([red] * len(target_sphere.vertices))
axis_t = trimesh.creation.axis(origin_size=origin_sz * float(AXIS_ORIGIN_SIZE_SCALE))
axis_t.apply_transform(T_target)
scene.add_geometry(axis_t)
scene.add_geometry(target_sphere)
target_points_list = [T_target.tolist()]
# 5) contact 点(新增)
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_c = trimesh.creation.axis(origin_size=origin_sz * float(AXIS_ORIGIN_SIZE_SCALE))
axis_c.apply_transform(T_contact)
scene.add_geometry(axis_c)
scene.add_geometry(contact_sphere)
contact_points_list = [T_contact.tolist()]
# 6) Orientation 参考点(OBB +Y 顶端)
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, # 一个 target
"contact_points_pose": contact_points_list, # 一个 contact
"transform_matrix": transform_matrix,
"functional_matrix": [],
"orientation_point": T_orient.tolist(),
"contact_points_group": [[0]], # 单组:仅索引0
"contact_points_mask": [True],
"contact_points_discription": ["Single contact at CONTACT_POS."],
"target_point_discription": ["User-defined target at TARGET_POS."],
"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("barrel")
|