benchmarks-viz / outputs /logs /spider_record_frames.py
ckwolfe's picture
Upload folder using huggingface_hub
73a3036 verified
"""Offscreen render the Spider Allegro bimanual hand scene to mp4.
Used by the end-to-end evaluation to prove:
1. MuJoCo (the Spider sim backend) loads the hand assets baked into the image.
2. shared.bench.VideoRecorder encodes rendered frames into a valid mp4.
Runs inside the `benchmarking/spider:latest` image with MUJOCO_GL=egl.
"""
from __future__ import annotations
import sys
from pathlib import Path
import mujoco
import numpy as np
# shared/bench is on the default PYTHONPATH of the spider container.
from bench.video_recorder import VideoRecorder
XML = "/opt/spider/spider/assets/robots/allegro/bimanual.xml"
OUT = Path("/workspace/outputs/videos/spider_allegro_oakink_lift_board_bimanual_data0.mp4")
W, H = 640, 480
FPS = 30
N_FRAMES = 60 # 2s of video
def main() -> int:
OUT.parent.mkdir(parents=True, exist_ok=True)
model = mujoco.MjModel.from_xml_path(XML)
data = mujoco.MjData(model)
print(f"model loaded: nq={model.nq} nv={model.nv} nu={model.nu} nbody={model.nbody}")
# Basic sweep: oscillate each hinge around its midrange to prove the scene
# is alive and frames differ. We don't have a retargeted qpos trajectory
# (preprocessed npz isn't in the image), so this is a synthetic wave.
jnt_range = model.jnt_range.copy()
mid = 0.5 * (jnt_range[:, 0] + jnt_range[:, 1])
amp = 0.5 * (jnt_range[:, 1] - jnt_range[:, 0])
limited = model.jnt_limited.astype(bool)
mid = np.where(limited, mid, 0.0)
amp = np.where(limited, amp, 0.2)
renderer = mujoco.Renderer(model, height=H, width=W)
with VideoRecorder(OUT, fps=FPS, size=(W, H)) as rec:
for t in range(N_FRAMES):
phase = 2.0 * np.pi * t / N_FRAMES
qpos_joint = mid + amp * 0.3 * np.sin(phase + np.arange(model.njnt) * 0.1)
# Set per-joint qpos via qpos_addr to avoid size mismatches when
# some joints (e.g. free joints) span multiple qpos entries.
data.qpos[:] = 0.0
for j in range(model.njnt):
addr = model.jnt_qposadr[j]
jtype = model.jnt_type[j]
if jtype == mujoco.mjtJoint.mjJNT_HINGE or jtype == mujoco.mjtJoint.mjJNT_SLIDE:
data.qpos[addr] = qpos_joint[j]
elif jtype == mujoco.mjtJoint.mjJNT_FREE:
# leave pose at identity, just set position z=0
data.qpos[addr : addr + 3] = 0.0
data.qpos[addr + 3] = 1.0
data.qpos[addr + 4 : addr + 7] = 0.0
mujoco.mj_forward(model, data)
renderer.update_scene(data)
frame = renderer.render() # H x W x 3 uint8
assert frame.shape == (H, W, 3), frame.shape
assert frame.dtype == np.uint8, frame.dtype
rec.log_frame(frame)
out_path = rec.close()
size_bytes = out_path.stat().st_size if out_path.exists() else 0
print(f"wrote {out_path} bytes={size_bytes} frames={N_FRAMES} res={W}x{H}")
if size_bytes < 1024:
print("ERROR: mp4 too small", file=sys.stderr)
return 2
return 0
if __name__ == "__main__":
sys.exit(main())