scene.recorder.FrameRecorder

scene.recorder.FrameRecorder(
    canvas,
    path=None,
    fps=None,
    render_scale=100,
    frames_dir=None,
    overwrite=True,
)

Collect manually rendered frames into an animation.

Where animation renders an animation by playing back the Blender timeline, a recorder gives the loop to you: change anything about the scene between frames - the timeline, the camera, styles, colors - and call :meth:render to capture the scene as it stands as the next frame. :meth:finalize assembles the captured frames into an MP4 or GIF.

Created via record rather than directly. Frames are rendered as PNGs into a temporary directory that lives (and is cleaned up) with the recorder, or into frames_dir when one is given, where they stay after the recorder is gone. With overwrite=False, frames already in frames_dir are reused rather than re-rendered, resuming an interrupted recording.

A recorder is also a context manager: when a path was given to record, leaving the with block without an exception finalizes to that path automatically.

Examples

Explicit finalize, which returns the animation for notebook display::

movie = canvas.record(fps=24)
for i in range(120):
    traj.frame = i
    canvas.look_at(traj)
    movie.render()
movie.finalize("wobble.mp4")

As a context manager, finalizing on exit::

with canvas.record("wobble.mp4", fps=24) as movie:
    for i in range(120):
        traj.frame = i
        movie.render()

See Also

molecularnodes.Canvas.record : Create a recorder bound to a canvas. molecularnodes.Canvas.animation : Render an animation from the timeline.

Attributes

Name Description
frames Get the frames captured so far.

Methods

Name Description
finalize Assemble the captured frames into an animation.
render Render the scene as it stands and store it as the next frame.

finalize

scene.recorder.FrameRecorder.finalize(path=None, fps=None, format=None)

Assemble the captured frames into an animation.

The recorder is left intact: more frames can be rendered and finalize called again, e.g. to write both an MP4 and a GIF of the same recording.

Parameters

Name Type Description Default
path str | Path | None File path to write the animation to, defaulting to the one given to record. The animation is returned for display regardless of whether a path is given. None
fps float Frame rate of the animation. Defaults to the rate given to record, or failing that the scene’s fps. None
format str Output format, either "MP4" or "GIF" (case-insensitive). When not specified, inferred from the suffix of path, defaulting to MP4. GIF output requires the pillow package. None

Returns

Name Type Description
IPython.display.Video | IPython.display.Image | None The assembled animation (a Video for MP4, an Image for GIF), which displays automatically as the result of a notebook cell. None if IPython is not installed.

Raises

Name Type Description
RuntimeError If no frames have been rendered yet.

render

scene.recorder.FrameRecorder.render(render_scale=None)

Render the scene as it stands and store it as the next frame.

Parameters

Name Type Description Default
render_scale int Scale of the rendered frame with respect to the resolution, overriding the recorder’s default for this frame. None

Returns

Name Type Description
pathlib.Path Path of the rendered PNG frame.

Raises

Name Type Description
ValueError If the frame’s pixel size differs from the frames already captured - all frames of an animation must share one resolution.

Notes

With overwrite=False (and a frames_dir), a frame whose numbered file already exists is reused instead of re-rendered, so re-running the same loop after a crash only renders the frames that are missing.