Download scene_layout_reference.py from UCBProject/eval_objects_ge30_sim_bundle: direct link, hf CLI and curl.
- Browser
- Download file 4.54 kB
-
https://huggingface.co/datasets/UCBProject/eval_objects_ge30_sim_bundle/resolve/main/scene_layout_reference.py
- Command line
-
hf download hf://datasets/UCBProject/eval_objects_ge30_sim_bundle/scene_layout_reference.py
-
curl -L -o scene_layout_reference.py https://huggingface.co/datasets/UCBProject/eval_objects_ge30_sim_bundle/resolve/main/scene_layout_reference.py
4.54 kB
| #!/usr/bin/env python3 | |
| """ | |
| 场景布置与 grasp 位姿变换(本 bundle 配套,纯 numpy)。 | |
| 示例: | |
| python scene_layout_reference.py --obj-id A01006 | |
| python scene_layout_reference.py --obj-id A01006 --candidate-index 0 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| ROBOT_POSITION = np.array([0.2, -0.05, 0.8], dtype=np.float64) | |
| ROBOT_ORIENTATION_EULER_DEG = np.array([0.0, 0.0, 90.0], dtype=np.float64) | |
| TABLE_POSITION = np.array([0.0, 1.0, 0.75], dtype=np.float64) | |
| TABLE_SCALE = np.array([2.0, 2.0, 0.1], dtype=np.float64) | |
| TABLE_TOP_Z = 0.80 | |
| OBJECT_XY = np.array([0.0, 0.55], dtype=np.float64) | |
| TCP_OFFSET = 0.105 | |
| R_ADAPT = np.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1]], dtype=np.float64) | |
| def euler_xyz_deg_to_matrix(euler_deg: np.ndarray) -> np.ndarray: | |
| rx, ry, rz = np.radians(np.asarray(euler_deg, dtype=np.float64)) | |
| cx, sx = np.cos(rx), np.sin(rx) | |
| cy, sy = np.cos(ry), np.sin(ry) | |
| cz, sz = np.cos(rz), np.sin(rz) | |
| Rx = np.array([[1, 0, 0], [0, cx, -sx], [0, sx, cx]], dtype=np.float64) | |
| Ry = np.array([[cy, 0, sy], [0, 1, 0], [-sy, 0, cy]], dtype=np.float64) | |
| Rz = np.array([[cz, -sz, 0], [sz, cz, 0], [0, 0, 1]], dtype=np.float64) | |
| return Rz @ Ry @ Rx | |
| def load_z_offset_m(bundle_dir: Path, obj_id: str) -> float: | |
| meta_path = bundle_dir / "obj_usd" / f"{obj_id}_meta.json" | |
| return float(json.loads(meta_path.read_text(encoding="utf-8"))["z_offset_m"]) | |
| def resolve_object_world_placement( | |
| obj_id: str, | |
| bundle_dir: Path, | |
| *, | |
| sim_z_yaw_deg: float = 0.0, | |
| ) -> dict: | |
| z_offset = load_z_offset_m(bundle_dir, obj_id) | |
| euler = np.array([0.0, 0.0, float(sim_z_yaw_deg)], dtype=np.float64) | |
| pos = np.array([OBJECT_XY[0], OBJECT_XY[1], TABLE_TOP_Z + z_offset], dtype=np.float64) | |
| return { | |
| "obj_id": obj_id, | |
| "position_world": pos.tolist(), | |
| "orientation_euler_deg": euler.tolist(), | |
| "z_offset_m": z_offset, | |
| "table_top_z": TABLE_TOP_Z, | |
| "sim_z_yaw_deg": float(sim_z_yaw_deg), | |
| } | |
| def grasp_mesh_to_panda_hand_target( | |
| position_mesh: np.ndarray, | |
| rotation_mesh: np.ndarray, | |
| placement: dict, | |
| ) -> tuple[np.ndarray, np.ndarray]: | |
| T = np.eye(4, dtype=np.float64) | |
| T[:3, :3] = euler_xyz_deg_to_matrix( | |
| np.array(placement["orientation_euler_deg"], dtype=np.float64) | |
| ) | |
| T[:3, 3] = np.asarray(placement["position_world"], dtype=np.float64) | |
| p = np.asarray(position_mesh, dtype=np.float64).reshape(3) | |
| R = np.asarray(rotation_mesh, dtype=np.float64).reshape(3, 3) | |
| pos_w = (T @ np.append(p, 1.0))[:3] | |
| rot_w = T[:3, :3] @ R | |
| rot_w = rot_w @ R_ADAPT | |
| pos_w = pos_w - rot_w[:, 2] * TCP_OFFSET | |
| min_z = TABLE_TOP_Z + 0.02 | |
| if pos_w[2] < min_z: | |
| pos_w = pos_w.copy() | |
| pos_w[2] = min_z | |
| return pos_w, rot_w | |
| def load_candidate(bundle_dir: Path, obj_id: str, index: int = 0) -> dict: | |
| doc = json.loads((bundle_dir / "candidates" / f"{obj_id}.json").read_text(encoding="utf-8")) | |
| c = doc["candidates"][f"candidate_{index}"] | |
| return { | |
| "position": np.array(c["position"], dtype=np.float64), | |
| "rotation": np.array(c["rotation"], dtype=np.float64), | |
| "name": c.get("name", f"candidate_{index}"), | |
| } | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--bundle-dir", type=Path, default=Path(__file__).resolve().parent) | |
| parser.add_argument("--obj-id", required=True) | |
| parser.add_argument("--candidate-index", type=int, default=None) | |
| args = parser.parse_args() | |
| placement = resolve_object_world_placement(args.obj_id, args.bundle_dir) | |
| out = { | |
| "table": {"position": TABLE_POSITION.tolist(), "top_z": TABLE_TOP_Z}, | |
| "franka": { | |
| "position": ROBOT_POSITION.tolist(), | |
| "orientation_euler_deg": ROBOT_ORIENTATION_EULER_DEG.tolist(), | |
| }, | |
| "object": placement, | |
| } | |
| if args.candidate_index is not None: | |
| cand = load_candidate(args.bundle_dir, args.obj_id, args.candidate_index) | |
| pos_w, rot_w = grasp_mesh_to_panda_hand_target( | |
| cand["position"], cand["rotation"], placement | |
| ) | |
| out["example"] = { | |
| "candidate_index": args.candidate_index, | |
| "name": cand["name"], | |
| "position_mesh": cand["position"].tolist(), | |
| "panda_hand_target_world": {"position": pos_w.tolist()}, | |
| } | |
| print(json.dumps(out, indent=2, ensure_ascii=False)) | |
| if __name__ == "__main__": | |
| main() | |