| | import argparse |
| | import json |
| |
|
| | from isaaclab.app import AppLauncher |
| |
|
| | |
| | parser = argparse.ArgumentParser(description="Load many GLB objects from a JSON file.") |
| | parser.add_argument("--json_path", type=str, default="layouts/desk_4.json", |
| | help="Path to object list JSON file.") |
| | parser.add_argument("--num_envs", type=int, default=1) |
| | AppLauncher.add_app_launcher_args(parser) |
| | args_cli = parser.parse_args() |
| |
|
| | |
| | app_launcher = AppLauncher(args_cli) |
| | simulation_app = app_launcher.app |
| |
|
| | import torch |
| | import isaaclab.sim as sim_utils |
| | from isaaclab.scene import InteractiveScene, InteractiveSceneCfg |
| | from isaaclab.assets import AssetBaseCfg |
| | from isaaclab.utils.math import quat_from_euler_xyz |
| |
|
| | |
| | import omni.usd |
| | from pxr import Usd, UsdGeom, Gf |
| |
|
| |
|
| | |
| | |
| | |
| | class SimpleSceneCfg(InteractiveSceneCfg): |
| | ground = AssetBaseCfg( |
| | prim_path="/World/defaultGroundPlane", |
| | spawn=sim_utils.GroundPlaneCfg(), |
| | ) |
| | dome_light = AssetBaseCfg( |
| | prim_path="/World/Light", |
| | spawn=sim_utils.DomeLightCfg(intensity=3000.0), |
| | ) |
| |
|
| |
|
| | |
| | |
| | |
| | def compute_bbox_center_and_size(prim_path: str): |
| | """ |
| | 计算 prim_path 对应 prim(及其所有子节点)的整体包围盒几何中心和尺寸。 |
| | 注意:这里用的是 WorldBound,但如果父节点是 identity,那么 world/local 一致。 |
| | """ |
| | ctx = omni.usd.get_context() |
| | stage = ctx.get_stage() |
| | prim = stage.GetPrimAtPath(prim_path) |
| |
|
| | if not prim.IsValid(): |
| | raise RuntimeError(f"[compute_bbox_center_and_size] Invalid prim path: {prim_path}") |
| |
|
| | bbox_cache = UsdGeom.BBoxCache( |
| | Usd.TimeCode.Default(), |
| | includedPurposes=[ |
| | UsdGeom.Tokens.default_, |
| | UsdGeom.Tokens.render, |
| | UsdGeom.Tokens.proxy, |
| | ], |
| | useExtentsHint=False, |
| | ) |
| |
|
| | world_bbox = bbox_cache.ComputeWorldBound(prim) |
| | aligned_range = world_bbox.ComputeAlignedRange() |
| | mn = aligned_range.GetMin() |
| | mx = aligned_range.GetMax() |
| |
|
| | size = mx - mn |
| | center = (mn + mx) * 0.5 |
| | return center, size |
| |
|
| |
|
| |
|
| | def print_final_bbox(prim_path: str, app, label=""): |
| | """ |
| | 计算 prim_path 对应的完整物体(含所有子 Mesh)在世界坐标系下的最终包围盒尺寸。 |
| | """ |
| | |
| | app.update() |
| | app.update() |
| |
|
| | stage = omni.usd.get_context().get_stage() |
| | prim = stage.GetPrimAtPath(prim_path) |
| | if not prim.IsValid(): |
| | print(f"[WARN] invalid prim for bbox: {prim_path}") |
| | return |
| |
|
| | bbox_cache = UsdGeom.BBoxCache( |
| | Usd.TimeCode.Default(), |
| | includedPurposes=[ |
| | UsdGeom.Tokens.default_, |
| | UsdGeom.Tokens.render, |
| | UsdGeom.Tokens.proxy, |
| | ], |
| | useExtentsHint=False, |
| | ) |
| |
|
| | world_bbox = bbox_cache.ComputeWorldBound(prim) |
| | aligned = world_bbox.ComputeAlignedRange() |
| |
|
| | mn = aligned.GetMin() |
| | mx = aligned.GetMax() |
| | size = mx - mn |
| |
|
| | print(f"\n====== Final BBox for {label or prim_path} ======") |
| | print(f" Min = ({mn[0]:.4f}, {mn[1]:.4f}, {mn[2]:.4f})") |
| | print(f" Max = ({mx[0]:.4f}, {mx[1]:.4f}, {mx[2]:.4f})") |
| | print(f" Size = ({size[0]:.4f}, {size[1]:.4f}, {size[2]:.4f})") |
| | print("==========================================\n") |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | def spawn_glb_from_dict(obj: dict, app, pivot_mode: str = "bottom"): |
| | """ |
| | 期望 JSON 字段: |
| | - object_name: str |
| | - glb_path: str |
| | - position: [x, y, z] |
| | * pivot_mode="bottom": 表示 bbox 底面中心的世界坐标 |
| | * pivot_mode="center": 表示 bbox 几何中心的世界坐标 |
| | - rotation_deg: [rx, ry, rz] (绕“pivot 点”的欧拉角, XYZ, 度) |
| | - size: [sx, sy, sz] (目标 bbox 尺寸, 世界单位) |
| | """ |
| | if pivot_mode not in ("bottom", "center"): |
| | raise ValueError(f"Unsupported pivot_mode: {pivot_mode}. Use 'bottom' or 'center'.") |
| |
|
| | obj_name = obj["object_name"] |
| | glb_path = obj["glb_path"] |
| | pos = obj.get("position", [0.0, 0.0, 0.0]) |
| | rot_deg = obj.get("rotation_deg", [0.0, 0.0, 0.0]) |
| | target_size = obj.get("size", None) |
| | if target_size is None: |
| | raise RuntimeError(f"[{obj_name}] `size` must be provided because position is defined on bbox.") |
| |
|
| | pos = [float(pos[0]), float(pos[1]), float(pos[2])] |
| | target_size = [float(target_size[0]), float(target_size[1]), float(target_size[2])] |
| |
|
| | stage = omni.usd.get_context().get_stage() |
| |
|
| | |
| | parent_path = f"/World/{obj_name}" |
| | child_path = f"{parent_path}/Mesh" |
| |
|
| | |
| | stage.DefinePrim(parent_path, "Xform") |
| |
|
| | |
| | glb_cfg = sim_utils.UsdFileCfg( |
| | usd_path=glb_path, |
| | scale=[1.0, 1.0, 1.0], |
| | ) |
| | glb_cfg.func( |
| | prim_path=child_path, |
| | cfg=glb_cfg, |
| | translation=None, |
| | orientation=None, |
| | ) |
| |
|
| | |
| | app.update() |
| | app.update() |
| |
|
| | |
| | orig_center, orig_size = compute_bbox_center_and_size(child_path) |
| |
|
| | eps = 1e-6 |
| | sx = target_size[0] / (orig_size[0] if abs(orig_size[0]) > eps else 1.0) |
| | sy = target_size[1] / (orig_size[1] if abs(orig_size[1]) > eps else 1.0) |
| | sz = target_size[2] / (orig_size[2] if abs(orig_size[2]) > eps else 1.0) |
| | scale_vec = Gf.Vec3d(sx, sy, sz) |
| |
|
| | |
| | if pivot_mode == "center": |
| | |
| | pivot_local_before_scale = Gf.Vec3d( |
| | float(orig_center[0]), |
| | float(orig_center[1]), |
| | float(orig_center[2]), |
| | ) |
| | else: |
| | |
| | pivot_local_before_scale = Gf.Vec3d( |
| | float(orig_center[0]), |
| | float(orig_center[1]), |
| | float(orig_center[2] - orig_size[2] * 0.5), |
| | ) |
| |
|
| | |
| | pivot_local_after_scale = Gf.Vec3d( |
| | pivot_local_before_scale[0] * sx, |
| | pivot_local_before_scale[1] * sy, |
| | pivot_local_before_scale[2] * sz, |
| | ) |
| |
|
| | |
| | |
| | |
| | child_prim = stage.GetPrimAtPath(child_path) |
| | child_xform = UsdGeom.Xformable(child_prim) |
| | child_xform.ClearXformOpOrder() |
| | s_child = child_xform.AddScaleOp() |
| | s_child.Set(scale_vec) |
| | t_child = child_xform.AddTranslateOp() |
| | t_child.Set(-pivot_local_after_scale) |
| |
|
| | |
| | |
| | rot_deg_t = torch.tensor(rot_deg, dtype=torch.float32) |
| | rot_rad = torch.deg2rad(rot_deg_t) |
| | quat = quat_from_euler_xyz( |
| | rot_rad[0:1], |
| | rot_rad[1:2], |
| | rot_rad[2:3], |
| | )[0].tolist() |
| |
|
| | w, x, y, z = quat |
| | parent_prim = stage.GetPrimAtPath(parent_path) |
| | parent_xform = UsdGeom.Xformable(parent_prim) |
| | parent_xform.ClearXformOpOrder() |
| |
|
| | |
| | |
| |
|
| | t_op = parent_xform.AddTranslateOp() |
| | t_op.Set(Gf.Vec3d(pos[0], pos[1], pos[2])) |
| |
|
| | |
| | mode_desc = "bbox center" if pivot_mode == "center" else "bbox bottom-center" |
| | print(f"[Spawned] {obj_name}") |
| | print(f" glb_path = {glb_path}") |
| | print(f" pivot_mode = {pivot_mode} ({mode_desc})") |
| | print(f" json position = {pos} (pivot in world)") |
| | print(f" orig_size = ({orig_size[0]:.4f}, {orig_size[1]:.4f}, {orig_size[2]:.4f})") |
| | print(f" target_size = ({target_size[0]:.4f}, {target_size[1]:.4f}, {target_size[2]:.4f})") |
| | print(f" scale_vec = ({sx:.4f}, {sy:.4f}, {sz:.4f})") |
| |
|
| | print_final_bbox(parent_path, app, label=obj_name) |
| | |
| |
|
| | print("\n") |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | def main(): |
| | |
| | with open(args_cli.json_path, "r") as f: |
| | objects = json.load(f) |
| |
|
| | print(f"Loaded {len(objects)} objects from JSON.") |
| |
|
| | |
| | sim_cfg = sim_utils.SimulationCfg(device=args_cli.device) |
| | sim = sim_utils.SimulationContext(sim_cfg) |
| | sim.set_camera_view([2.5, 2.5, 3.0], [0.0, 0.0, 0.0]) |
| |
|
| | |
| | for obj in objects: |
| | |
| | |
| | spawn_glb_from_dict(obj, simulation_app, pivot_mode="bottom") |
| |
|
| | scene_cfg = SimpleSceneCfg(args_cli.num_envs, env_spacing=2.0) |
| | scene = InteractiveScene(scene_cfg) |
| |
|
| | sim.reset() |
| | print("[INFO] Simulation started...") |
| |
|
| | sim_dt = sim.get_physics_dt() |
| | while simulation_app.is_running(): |
| | sim.step() |
| | scene.update(sim_dt) |
| |
|
| |
|
| | if __name__ == "__main__": |
| | main() |
| | simulation_app.close() |