Skip to content

04 Paint Pipeline

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

English | 中文

04 Paint Queue & Ordering

sync() does not simply walk self.mobjects

ManimCE's paint order is not plain document order. MLWindow.sync(scene) first collects "what to draw this frame, and in what order" into a deferred queue, then flushes it:

sync(scene)
  ├─ publish camera / background
  ├─ decide whether the static layer can be reused
  ├─ expand the scene's root mobjects
  ├─ push each paint unit into _PaintQueue using CE rules
  ├─ submit meshes (explicit batches + MeshMobjects in the scene)
  └─ queue.flush() → emitters → native command list

CE ordering rules

paint_order.depth_key() mirrors ThreeDCamera.get_mobjects_to_display:

  • a mobject with shade_in_3d=False gets depth inf (always above 3D content);
  • 3D objects use get_z_index_reference_point() (or the centre of a z_index_group) dotted with the third row of the camera rotation matrix;
  • 2D scenes have no rotation matrix, so every depth is inf and document order wins.

_PaintQueue sorts on a four-part key:

(depth_key, z_index, family_preorder, visit_order)

The stable sort guarantees:

  1. larger camera depth (closer) draws later;
  2. equal depth falls back to z_index;
  3. then CE's flattened family order;
  4. finally visit order.

Paint units

A "unit" is not necessarily one mobject:

  • Text may split into several units per glyph or line;
  • a container may be split so children of different roots interleave by depth;
  • fixed_in_frame_mobjects skip depth sorting and stay in screen space;
  • flat Squares / Polygons use batch emission (_send_quad_batch / _send_polygon_batch) when eligible.

Emitters → native command list

Each unit eventually calls an emitter in render/, appending one native command:

Python emitter Native entry Command
rect / circle / line / ellipse AddRect / AddCircle / ... CMD_*
polygon / bezier fill AddPolygon / AddBezierPath CMD_POLYGON / CMD_BEZIER
line strip / dashed AddLineStripF / AddDashedLine CMD_LINE_STRIP / ...
text glyph usually a polygon / bezier same as above
image AddImageQuad CMD_IMAGE
mesh AddMesh / AddMeshIndexed / ...Lit separate mesh channel

The command list records submission order; native replay builds vertices in that order, preserving the painter rule that later draws cover earlier ones.

The static layer

If none of this frame's roots have updaters, play.py can bake the "never moves" roots into a texture:

  1. render the static roots into an offscreen target;
  2. submit the result as a single image quad;
  3. sync skips those roots' ids and draws only dynamic content.

Rules:

  • any updater in the scene or on a mobject → baking is disabled (updaters can write other objects, so the static decision is unreliable);
  • a background or camera change invalidates the layer and re-bakes it;
  • the static layer is pixels, not geometry: it does not take part in mesh depth and is drawn as a painter overlay.

Meshes vs. painter

Native draws meshes first (depth test + depth write), then the painter:

mesh (depth test + write)
  ↓
painter primitives (depth test, no write)
  ↓
image / text overlays
  • explicit win.set_mesh(batch), MeshMobjects in the scene and the automatic Surface path all become mesh.
  • painter primitives sit on top by default; only units with shade_in_3d=True map their depth into the mesh's [0,1] range and take part in occlusion.
  • 2D text/math/images keep depth 0, preserving the "always above 3D content" semantics.

Transparency and order

  • painter alpha blending depends on submission order, so the queue must sort stably.
  • meshes have no per-pixel transparency sort: translucent faces at the same camera depth still blend in submission order; split the batch or avoid coplanar faces when exact ordering matters.
  • baking a set of roots into pixels freezes their internal order; only use it for deterministic content.

Key files

  • real_time_manim/window/paint_order.py: depth_key, _PaintQueue, batching
  • real_time_manim/window/paint.py: concrete emitters and batch emission
  • real_time_manim/window/core.py: sync, the static layer, mesh submission
  • real_time_manim/window/play.py: static-layer preparation and the play loop

Clone this wiki locally