Skip to content

Load multi-clip motions and compute tracked bodies with forward kinematics - #1368

Draft
qiayuanl wants to merge 1 commit into
mainfrom
qiayuanl/tracking-forward-kinematics
Draft

qiayuanl wants to merge 1 commit into
mainfrom
qiayuanl/tracking-forward-kinematics

Conversation

@qiayuanl

@qiayuanl qiayuanl commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

The tracking task reads one motion clip, and a motion file stores the pose and velocity of every body for every frame. This PR stores only the root and joint states, computes the tracked bodies with forward kinematics, and lets one command track many clips.

A motion file holds root_pos_w, root_quat_w, root_lin_vel_w, root_ang_vel_w, joint_pos, joint_vel, joint_names and fps. For the Unitree G1 that is 71 floats per frame, in place of 448 in the file and 630 on the device. csv_to_npz writes the file from the resampled CSV and builds a scene only for --render.

ForwardKinematics in mjlab.utils.forward_kinematics is built from the MuJoCo model and returns the world poses and twists of the requested bodies for a batch of root and joint states. It needs a free-joint root, joints with one degree of freedom, and hinge joints on the tracked paths, and raises otherwise. MotionCommand keeps the tracked bodies as state and updates them in set_time_steps, which resets, the per-step advance and the viewer scrubber share. On CUDA the command compiles body_state with torch.compile, and CI does not run that path.

MotionCommandCfg.motion_file takes a file, a list, a glob, or a directory written by the new pack_motions script. On the command line, train and play take a file, a glob or such a directory. MotionLoader concatenates the clips, keeps each clip's exclusive end frame in motion_ends and its name in motion_names, and raises when clips differ in fps or a clip has fewer than two frames. The new motion_end termination ends an episode as a time-out when it reaches the last frame of its clip. Without that termination, the command resamples at the clip end, so an episode never runs from one clip into the next. With several clips, sampling_mode="start" resets to the first frame of a random clip, and the exported ONNX file holds the policy alone and maps obs to actions. With one clip the export is unchanged.

Existing motion files do not load and must be regenerated with csv_to_npz. An episode that reaches the end of its clip ends as a time-out, where the command used to resample a new frame and continue. A direct write to time_steps does not move the tracked bodies; set_time_steps does. A reset frame is at most the frame before the last frame of its clip.

The demo motion in src/mjlab/scripts/gcs.py and the registry motions used by scripts/benchmarks need an upload in the new format before this merges. The demo file needs a new URL, because released versions pin the current one by checksum.

🤖 Generated with Claude Code

@qiayuanl
qiayuanl force-pushed the qiayuanl/tracking-forward-kinematics branch 7 times, most recently from 033dfe1 to 8a0603f Compare November 21, 2026 00:07
…atics

Motion files hold the root link's world pose and twist and the joint states: root_pos_w, root_quat_w, root_lin_vel_w, root_ang_vel_w, joint_pos, joint_vel, joint_names and fps. MotionLoader keeps these per-frame tensors and body_state(frames) computes the tracked bodies from them with ForwardKinematics. Loading raises a ValueError that names the file when the file lacks one of these keys, and when its joint_names differ from the robot's joint names in column order.

MotionLoader takes one npz file, a list, a glob or a packed directory written by pack_motions, which stores one npy array per field. resolve_motion_file expands ~, uses an existing path literally even when it contains glob characters, and raises for a glob without a match. train and play both resolve the motion file with it, so play accepts a glob or a packed directory with a zero or random agent. The loader concatenates the clips, records each clip's exclusive end frame in motion_ends and its name in motion_names, and exposes the frame rate as fps. It raises when a clip has fewer than two frames and when clips have different fps. pack_motions raises on mixed fps and writes fps into the packed directory.

The motion_end termination, part of the tracking environment, ends an episode as a time-out on its clip's last frame. A reset frame is at most the frame before its clip's last frame, and the first reference frame of an episode is the reset frame plus one. Reset bins map to frames with integer math that is exact for any bin count. With several clips, sampling_mode="start" resets each env to the first frame of a clip drawn uniformly at random; with one clip it resets to frame 0 and draws no random number.

ForwardKinematics is built from the MuJoCo model and the entity indexing. It composes the steps on the paths from the free-joint root to the requested bodies and returns world-frame poses and twists in the body_link_pose_w / body_link_vel_w convention. It raises for a fixed base, for a slide or ball joint on a tracked path, for a first entity body that is not the free-joint root, and for any joint of the entity with more than one degree of freedom, so joint_pos and joint_vel share columns.

MotionCommand keeps the tracked bodies as state. set_time_steps(env_ids, frames) writes time_steps and computes the bodies of those envs; resets, the per-step advance and the viewer scrubber go through it, and a direct write to time_steps does not update the bodies. On CUDA the command compiles body_state with torch.compile so each call is one fused kernel. The ONNX export reads the bodies of all frames from body_state.

csv_to_npz writes the motion file from the resampled input and builds a scene and a simulation only to render the frames with --render. It raises when the CSV does not have 7 columns plus one per joint.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@qiayuanl
qiayuanl force-pushed the qiayuanl/tracking-forward-kinematics branch from 8a0603f to b2e9c8b Compare November 21, 2026 00:07

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant