Repository navigation
02 Runtime Model
English | 中文
RTM never drives ManimCE's Scene.render(); the recorders run an explicit lifecycle instead:
scene.setup()
scene.construct()
scene.tear_down()
The implementation is record.scene_lifecycle(). You only write construct(), and inside it:
- create an
MLWindow; - bind the scene with
win.scene = self; - put static content in with
self.add(...); - animate or wait with
win.play(...); - finish with
win.close()(which also ends encoding when recording).
MLWindow keeps a registry that allows exactly one live window per process; a second one raises immediately, because the native Vulkan state is process-global.
MLWindow.__init__ roughly does this:
- locate and load
vulkan_core.dll/vulkan_core.dylibwithctypes.CDLL; - bind the exported functions (
Vulkan_Init,AddRect,AddImageQuad,AddMesh...); - call
Vulkan_InitEx(w, h, hidden)to create the instance/surface/device/swapchain; - on failure, call
Vulkan_Shutdown()before raising, so no half-initialised state is left behind.
with MLWindow(...) as win: calls close() on block exit; there is also an atexit fallback, but an explicit close() is what finishes a manual fast record and surfaces encoder errors.
MLWindow has a class-level _schedule_mode:
win.play(*animations, run_time=...):
- resolve animation durations and whether
Waitfreezes; - build a
Timelinefor this group of animations; - loop over frames:
timeline.evaluate(t)→sync(scene)→tick(); - run each animation's
finish()/clean_up_from_scene()at the end; - advance
_cursor.
FrameServer temporarily sets _schedule_mode = True and then runs construct():
-
win.play(...)does not draw; it appends the animation towin._sched; -
_cursoraccumulates, forming a global timeline; - after
construct()returns,window.build_timeline()callsfinalize()once; - from then on,
render_frame(t)only doesevaluate+ draw.
That is where "construct once, render any frame" comes from.
live: play → Timeline → per frame evaluate + sync + tick
scheduled: play → _sched (record only) → build_timeline() → FrameServer.render_frame(t)
MLWindow.sync(scene) is the per-frame entry point on the Python side. It:
- publishes the camera (
camera_state: frame centre, height, rotation, focal distance, light source); - publishes the background colour;
- decides whether the static layer can be reused;
- collects scene roots and computes paint order;
- submits explicit meshes /
MeshMobjects found in the scene; - hands painter primitives to the native command list.
tick() triggers the native Vulkan_Tick: record the command buffer, submit, present (or read back).
- 2D scenes have no rotation matrix: projection is affine and paint order falls back to document order +
z_index. - 3D scenes (
ThreeDCamera/set_camera_orientation) publish a rotation matrix: emitters project into camera space and paint order sorts by camera depth. -
fixed_in_frame_mobjectsignore rotation and stay in screen space (subtitles, titles).
close() stops the recording thread / fast record first, then calls Vulkan_Shutdown(). The order is not optional: shutting Vulkan down while the recording thread is still reading the swapchain crashes.
record._AutoRecord injects _finish_fast_record() / stop_record() before the real close(), so a plain win.close() inside a recorded scene finishes encoding for you.
-
real_time_manim/window/core.py: window construction, DLL bindings,sync -
real_time_manim/window/play.py: play loop, timeline rendering -
real_time_manim/window/recording.py:close, recording lifecycle -
real_time_manim/record.py:scene_lifecycle -
real_time_manim/frames.py: scheduling mode andFrameServer