rendering
FrameClock
¶
Decides when the next video frame is due, given the simulation time.
Shared by renderers and trajectory recorders so that both sample the simulation at exactly the same timepoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
playback_speed
|
float
|
Video playback speed relative to real time. |
required |
output_fps
|
int
|
Output video frame rate. |
required |
timestep
|
float
|
Simulation timestep in seconds. Half of it is used as a tolerance when comparing times, to absorb floating-point drift. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
interval |
Simulation time between consecutive frames, in seconds. |
Source code in src/flygym/rendering.py
Renderer
¶
Renders MuJoCo scenes to video frames.
Frames are rendered from a private copy of the simulation state, in which the
geometry is recomputed from the current qpos and mocap poses (see
update_render_geometry). Rendered frames are therefore consistent with the
current qpos, rather than with the geometry computed at the start of the last
physics step. The simulation data is not modified. Quantities that do not depend
on positions alone (e.g., contact forces, if enabled in scene_option) are not
rendered correctly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
Compiled MuJoCo model. |
required |
cameras
|
str | MjsCamera | list[str | MjsCamera]
|
Camera(s) to render. Can be a camera name, MJCF camera element, or a sequence of either. |
required |
camera_res
|
tuple[int, int]
|
|
(240, 320)
|
playback_speed
|
float
|
Video playback speed relative to real time. |
0.2
|
output_fps
|
int
|
Output video frame rate. |
25
|
buffer_frames
|
bool
|
If True, store rendered frames on the renderer. |
True
|
scene_option
|
MjvOption | None
|
MuJoCo scene options. Uses defaults if None. |
None
|
render_rgb
|
bool
|
If True, render RGB frames (stored in |
True
|
render_depth
|
bool
|
If True, render depth maps (raw float arrays in
|
False
|
render_segmentation
|
bool
|
If True, render segmentation masks (raw int arrays in
|
False
|
**kwargs
|
Any
|
Passed to |
{}
|
render_rgb, render_depth, and render_segmentation are independent;
enable any combination. At least one must be True.
Attributes:
| Name | Type | Description |
|---|---|---|
frames |
Dict mapping camera name to list of RGB frames (uint8), or None if
|
|
depth_frames |
Like |
|
segmentation_frames |
Like |
Source code in src/flygym/rendering.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
close()
¶
get_camera_matrix(camera, mj_data, mj_model)
¶
Get the 3x4 camera projection matrix from the current MjData.
The returned matrix maps homogeneous world coordinates to homogeneous
image (pixel) coordinates, following the standard MuJoCo
image @ focal @ rotation @ translation composition.
Source code in src/flygym/rendering.py
render_as_needed(mj_data)
¶
Render frames for all cameras if enough time has elapsed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_data
|
MjData
|
Current MuJoCo data. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if frames were rendered, False otherwise. |
Source code in src/flygym/rendering.py
reset()
¶
Clear buffered frames and reset the render timer.
Source code in src/flygym/rendering.py
save_video(output_path, **kwargs)
¶
Save recorded frames as video files.
Only RGB frames are saved. Depth/segmentation renders are kept as raw
arrays in self.depth_frames / self.segmentation_frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
dict[str | MjsCamera, PathLike] | PathLike
|
Either a dict mapping camera specs to file paths, or: - If single camera: a file path to save to - If multiple cameras: a directory path to save all videos to |
required |
**kwargs
|
Any
|
Additional arguments passed to imageio.imwrite |
{}
|
Source code in src/flygym/rendering.py
show_in_notebook(camera=None, **kwargs)
¶
Display recorded frames in a Jupyter notebook.
Only RGB frames are displayed. Depth/segmentation renders are kept as raw
arrays in self.depth_frames / self.segmentation_frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera
|
str | MjsCamera | list[str | MjsCamera] | None
|
Camera(s) to display. If None, displays all cameras. |
None
|
**kwargs
|
Any
|
Additional arguments passed to mediapy.show_video |
{}
|
Source code in src/flygym/rendering.py
launch_interactive_viewer(mj_model, mj_data, run_async=False, init_keyframe='neutral')
¶
Launch MuJoCo's built-in interactive viewer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
Compiled MuJoCo model. |
required |
mj_data
|
MjData
|
MuJoCo data. |
required |
run_async
|
bool
|
If True, launch the viewer in a separate process and return immediately. Use this when calling from a Jupyter notebook. |
False
|
init_keyframe
|
str | None
|
Keyframe name to reset to before launching. Uses the current state if None. |
'neutral'
|
Source code in src/flygym/rendering.py
preview_model(mj_model, mj_data, camera, *, init_keyframe='neutral', duration=0.1, camera_res=(240, 320), playback_speed=0.1, output_fps=25, show_in_notebook=False, output_path=None, **kwargs)
¶
Run a short simulation and render a preview.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
Compiled MuJoCo model. |
required |
mj_data
|
MjData
|
MuJoCo data. |
required |
camera
|
MjsCamera | str
|
Camera name or MJCF element to use for rendering. |
required |
init_keyframe
|
str | None
|
Keyframe name to reset to before rendering. Uses the current state if None. |
'neutral'
|
duration
|
float
|
Duration to simulate in seconds. |
0.1
|
camera_res
|
tuple[int, int]
|
|
(240, 320)
|
playback_speed
|
float
|
Video playback speed relative to real time. |
0.1
|
output_fps
|
int
|
Output video frame rate. |
25
|
show_in_notebook
|
bool
|
If True, display the video in a Jupyter notebook. |
False
|
output_path
|
PathLike | None
|
Path to save the video. Not saved if None. |
None
|
**kwargs
|
Any
|
Passed to |
{}
|
Source code in src/flygym/rendering.py
update_render_geometry(mj_model, mj_data)
¶
Recompute everything that rendering depends on from qpos and mocap poses.
This runs the stages of mj_forward that determine rendered geometry (kinematics,
center-of-mass positions, camera and light poses, and tendon paths). It skips
collision detection and all velocity- and force-dependent stages, which do not
affect rendering with default scene options.
Why renderers call this before rendering
MuJoCo's scene update does not read qpos. It reads derived positions such as
xpos/xmat (bodies), geom_xpos/geom_xmat (geoms), and cam_xpos
(cameras). mj_step computes these derived positions first and integrates
afterwards, so right after a step, qpos is the new state while the derived
positions still reflect the state at the start of the step. Rendering
mj_data directly would therefore show the fly one timestep in the past.
FlyGym renderers instead copy qpos and mocap poses into a private MjData
and call this function on it, so that frames are consistent with the current
qpos and time. Trajectory recorders (flygym.recording) store only qpos
and mocap poses, and replay calls this function in the same way. Live and
replayed frames are therefore identical.
Recording the derived positions instead would also reproduce live frames, but it would take far more space (about 20 times more for NeuroMechFly), tie the recording format to the renderer's internals, and lose the ability to restore or analyze the simulation state from the recording. Recomputing the geometry is cheap compared to rendering itself.