Repository navigation
04 Paint Pipeline
English | 中文
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
paint_order.depth_key() mirrors ThreeDCamera.get_mobjects_to_display:
- a mobject with
shade_in_3d=Falsegets depthinf(always above 3D content); - 3D objects use
get_z_index_reference_point()(or the centre of az_index_group) dotted with the third row of the camera rotation matrix; - 2D scenes have no rotation matrix, so every depth is
infand document order wins.
_PaintQueue sorts on a four-part key:
(depth_key, z_index, family_preorder, visit_order)
The stable sort guarantees:
- larger camera depth (closer) draws later;
- equal depth falls back to
z_index; - then CE's flattened family order;
- finally visit order.
A "unit" is not necessarily one mobject:
-
Textmay split into several units per glyph or line; - a container may be split so children of different roots interleave by depth;
-
fixed_in_frame_mobjectsskip depth sorting and stay in screen space; - flat Squares / Polygons use batch emission (
_send_quad_batch/_send_polygon_batch) when eligible.
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.
If none of this frame's roots have updaters, play.py can bake the "never moves" roots into a texture:
- render the static roots into an offscreen target;
- submit the result as a single image quad;
-
syncskips 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.
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 automaticSurfacepath all become mesh. - painter primitives sit on top by default; only units with
shade_in_3d=Truemap 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.
- 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.
-
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