recording
Record kinematic trajectories during simulation and render them to video later.
Rendering frames during simulation does not scale to many parallel worlds: buffering
(n_worlds, n_cams, height, width, 3) RGB tensors quickly becomes infeasible. Instead,
a trajectory recorder stores only the generalized coordinates (qpos, plus mocap poses
if the model has any) at the video frame rate. The resulting RecordedTrajectory can
be saved, sub-selected, and rendered to video afterwards with render_trajectories
(CPU) or flygym.warp.recording.render_trajectories_gpu (GPU).
A trajectory does not store the model or any rendering settings. To render it, pass a
compiled mujoco.MjModel with the same qpos layout as the one it was recorded with.
Persist the model separately if needed (e.g. with
BaseCompositionElement.save_xml_with_assets). Material, texture, and lighting edits
(such as flygym.warp.rendering.modify_world_for_batch_rendering) do not change the
qpos layout, so a trajectory remains compatible with the edited model.
Trajectories recorded on CPU and GPU have the same format, and either can be rendered on either backend.
RecordedTrajectory
dataclass
¶
Kinematic trajectory of a single world, sampled at the video frame rate.
Attributes:
| Name | Type | Description |
|---|---|---|
time |
ndarray
|
|
qpos |
ndarray
|
|
output_fps |
int
|
Frame rate at which the frames should be played back. |
playback_speed |
float
|
Playback speed relative to real time. |
world_id |
int
|
Index of the parallel world this trajectory came from (always 0 for CPU simulations). |
mocap_pos |
ndarray | None
|
|
mocap_quat |
ndarray | None
|
|
Source code in src/flygym/recording.py
from_file(path)
classmethod
¶
Load a trajectory from a .npz file written by save.
Source code in src/flygym/recording.py
save(path)
¶
Save the trajectory to a .npz file. Load it back with from_file.
Source code in src/flygym/recording.py
TrajectoryRecorder
¶
Records the kinematic state of a CPU simulation at the video frame rate.
Frames are sampled at the same timepoints as a flygym.rendering.Renderer with the
same playback_speed and output_fps would render them. Usually created via
Simulation.set_trajectory_recorder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
Compiled MuJoCo model. |
required |
playback_speed
|
float
|
Video playback speed relative to real time. |
0.2
|
output_fps
|
int
|
Output video frame rate. |
25
|
Source code in src/flygym/recording.py
trajectory
property
¶
The trajectory recorded so far.
record_as_needed(mj_data)
¶
Record the current state if a frame is due.
Returns:
| Type | Description |
|---|---|
bool
|
True if a frame was recorded, False otherwise. |
Source code in src/flygym/recording.py
reset()
¶
Discard recorded frames and reset the frame clock.
Source code in src/flygym/recording.py
as_trajectory_list(trajectories)
¶
Wrap a single trajectory in a list; raise if no trajectories are given.
Source code in src/flygym/recording.py
check_trajectories_match_model(mj_model, trajectories)
¶
Raise a ValueError if a trajectory's state layout does not match the model.
Source code in src/flygym/recording.py
render_trajectories(mj_model, trajectories, cameras, output_path=None, *, camera_res=(240, 320), scene_option=None)
¶
Render recorded trajectories on the CPU.
For GPU batch rendering, see flygym.warp.recording.render_trajectories_gpu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
Compiled model with the same |
required |
trajectories
|
RecordedTrajectory | list[RecordedTrajectory]
|
One trajectory or a list of them. |
required |
cameras
|
str | MjsCamera | list[str | MjsCamera]
|
Camera(s) to render. |
required |
output_path
|
PathLike | None
|
If given, also save the frames as videos (see
|
None
|
camera_res
|
tuple[int, int]
|
|
(240, 320)
|
scene_option
|
MjvOption | None
|
MuJoCo scene options. Uses defaults if None. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, ndarray]]
|
A list, parallel to |
list[dict[str, ndarray]]
|
|
Source code in src/flygym/recording.py
resolve_camera_ids(mj_model, cameras)
¶
Map camera specs to {camera_name: internal_camera_id}.
Source code in src/flygym/recording.py
save_trajectory_videos(trajectories, frames, output_path)
¶
Save frames rendered from trajectories as videos.
If there is a single trajectory and a single camera and output_path ends with
.mp4, the video is written to output_path. Otherwise, output_path is a
directory: {output_path}/{camera}.mp4 for a single trajectory, or
{output_path}/traj{i:04d}_world{world_id}/{camera}.mp4 for multiple ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trajectories
|
list[RecordedTrajectory]
|
The rendered trajectories, used for the frame rate and the world IDs. |
required |
frames
|
list[dict[str, ndarray]]
|
Rendered frames, as returned by |
required |
output_path
|
PathLike
|
Output video file or directory (see above). |
required |