Files

28 lines
1.3 KiB
Python

"""MuJoCo 实时查看器:加载场景并把双臂放到指定姿态跑物理。
用法:MUJOCO_GL=glfw .venv/bin/python scripts/view_scene.py [--pose ready|home]
"""
import argparse, json, time
import os
from pathlib import Path
import mujoco, mujoco.viewer
ROOT = Path(__file__).resolve().parents[1]
SCENE = os.environ.get("A7L20_SCENE", "a7l20_scene")
ap = argparse.ArgumentParser(); ap.add_argument("--pose", default="ready", choices=["ready", "home"]); args = ap.parse_args()
m = mujoco.MjModel.from_xml_path(str(ROOT / f"scene/{SCENE}.xml")); d = mujoco.MjData(m)
info = json.loads((ROOT / f"scene/{SCENE}.json").read_text())
pose = info[args.pose]
for side in ("left", "right"):
for jn, an, q in zip(info["arms"][side]["joints"], info["arms"][side]["actuators"], pose["arm_qpos"][side]):
d.qpos[m.jnt_qposadr[m.joint(jn).id]] = q; d.ctrl[m.actuator(an).id] = q
for an in info["hands"][side]["actuators"]:
d.ctrl[m.actuator(an).id] = pose.get("hand_ctrl", 0.0)
mujoco.mj_forward(m, d)
with mujoco.viewer.launch_passive(m, d) as v:
v.cam.lookat[:] = (0.35, 0, 0.9); v.cam.distance = 2.2; v.cam.azimuth = 150; v.cam.elevation = -20
while v.is_running():
t0 = time.time(); mujoco.mj_step(m, d); v.sync()
dt = m.opt.timestep - (time.time() - t0)
if dt > 0: time.sleep(dt)