Repository navigation
03 Timeline
English | 中文
Traditional Manim advances Scene.play frame by frame against the wall clock while Mobjects mutate in place. Jumping to frame N means replaying from 0 — which makes recording, previewing and parallel work slow.
RTM's Timeline represents a scene as animation entries on one global time axis:
tl = Timeline(window, scene)
tl.add(FadeIn(sq), start=0.0)
tl.add(Transform(sq, circle), start=1.0)
tl.finalize()
tl.render_at(1.5) # the frame at 1.5s, directlyThis requires interpolate(alpha) to be a pure function of alpha: begin() captures start/end copies, and any alpha can then recompute the state.
Timeline
_entries: [{anim, start, run_time, stateful}, ...]
duration: last entry's start + run_time
_prepared: True after finalize()
_baseline: snapshot of the scene's authored state (only needed for staggered schedules)
_start_snapshots: state when each animation began
_frozen_windows: [start, end) ranges from CE's freeze_current_frame
finalize() runs once, and the order matters:
-
Decide whether a baseline is needed: if the entries do not all start at 0 (the global timeline of
FrameServer/jobs=N), snapshot the full state ofscene.mobjectsfirst. -
begin()entries in ascending start order; before each one, advance earlier entries to its start so chained animations (A ends, B begins) see A's end state rather than the scene's initial state. - Record each animation's start snapshot, so a seek can restore the "not started yet" state.
- Resolve
Waitfrozen/stepped semantics and build_frozen_windows.
The baseline is the fix from #122: begin() may call interpolate(0) and write mobjects it does not own; without a baseline those writes contaminate every earlier frame.
evaluate(t) changes scene state only; it never draws:
- first call / forward: the scene already carries the previous frame's state, so just advance started entries in start order.
- backward seek / first staggered step: restore the baseline, then restore "not yet started" entries to their start snapshots (in reverse start order, so the earliest one writes last).
-
frozen window: do not drive updaters; the picture holds, but
_last_tkeeps advancing and the first frame after the window gets the previous dt. -
updaters:
drive_animation_updatersadvances the animation's own copy first, then interpolates mobjects, matching CE'sScene.update_to_time. -
finish hooks: with
finish_hooks=True, an entry runsfinish()once after it ends and then holds its final state, so things likeBlinkare not interpolated back.
render_at(t, readback=False)
├─ evaluate(t) # change scene state
├─ sync(scene) # queue that state as native commands
├─ tick() # record command buffer, submit, present
└─ request_readback() # optional: copy this frame to staging before present
The order cannot be reversed: sync must queue the state for t before tick draws. An early version had it backwards, so the readback after render_at(t) returned the previous frame.
Timeline.frame_times builds the time list from each play's local frame grid:
- entries with the same
startform a group; - each group uses CE's frame-count rule (
ce_frame_count); - frozen waits count frames with
int(), soround(duration*fps)cannot disagree with single-frame playback.
This makes a frame index mean the same thing in FramePreview, FrameServer, record_mp4 and parallel workers.
fs = FrameServer(MyScene) # one construct + build_timeline
fs.render_frame(1.5) # O(1)
fs.grab(1.5) # render + read back
fs.save_frame(1.5, "f.png")
fs.close()FrameServer hides the window by default; show_frame(t) reveals it. save_frame_async moves PNG encoding/disk writes to a thread pool, leaving only GPU readback on the main thread.
The following are not pure functions of alpha:
- accumulative animations (
Rotate,Rotating,SpeedModifier,ChangeSpeed, ...); - animations with their own updaters;
- custom
interpolate_mobjectthat depends on outside state (module-level counters,Story.state(t), ...).
Timeline.add marks them stateful; strict=True rejects them outright. FrameServer still runs, but the semantics of an arbitrary seek are the animation's responsibility. Custom animations should keep state on a mobject copy or make interpolate(alpha) side-effect free.
-
real_time_manim/timeline.py:Timeline,finalize,evaluate,render_at,frame_times -
real_time_manim/frames.py:FrameServer,grab,save_frame -
real_time_manim/window/play.py: the liveplayloop