Skip to content

02 Runtime Model

Tagin T edited this page Nov 24, 2026 · 2 revisions

English | 中文

02 Runtime Model

Scene lifecycle

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:

  1. create an MLWindow;
  2. bind the scene with win.scene = self;
  3. put static content in with self.add(...);
  4. animate or wait with win.play(...);
  5. 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.

Window initialisation

MLWindow.__init__ roughly does this:

  • locate and load vulkan_core.dll / vulkan_core.dylib with ctypes.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.

The two modes of play()

MLWindow has a class-level _schedule_mode:

Live mode (default)

win.play(*animations, run_time=...):

  1. resolve animation durations and whether Wait freezes;
  2. build a Timeline for this group of animations;
  3. loop over frames: timeline.evaluate(t) → sync(scene) → tick();
  4. run each animation's finish() / clean_up_from_scene() at the end;
  5. advance _cursor.

Scheduling mode (FrameServer / workers)

FrameServer temporarily sets _schedule_mode = True and then runs construct():

  • win.play(...) does not draw; it appends the animation to win._sched;
  • _cursor accumulates, forming a global timeline;
  • after construct() returns, window.build_timeline() calls finalize() once;
  • from then on, render_frame(t) only does evaluate + 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)

sync / tick per frame

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).

Camera and 2D/3D

  • 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_mobjects ignore rotation and stay in screen space (subtitles, titles).

close() ordering

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.

Key files

  • 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 and FrameServer

Clone this wiki locally