Skip to content

03 Timeline

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

English | 中文

03 Timeline & Frame Addressing

Why a timeline

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, directly

This requires interpolate(alpha) to be a pure function of alpha: begin() captures start/end copies, and any alpha can then recompute the state.

Data structure

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

finalize() runs once, and the order matters:

  1. 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 of scene.mobjects first.
  2. 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.
  3. Record each animation's start snapshot, so a seek can restore the "not started yet" state.
  4. Resolve Wait frozen/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)

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_t keeps advancing and the first frame after the window gets the previous dt.
  • updaters: drive_animation_updaters advances the animation's own copy first, then interpolates mobjects, matching CE's Scene.update_to_time.
  • finish hooks: with finish_hooks=True, an entry runs finish() once after it ends and then holds its final state, so things like Blink are not interpolated back.

render_at(t)

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.

frame_times(fps)

Timeline.frame_times builds the time list from each play's local frame grid:

  • entries with the same start form a group;
  • each group uses CE's frame-count rule (ce_frame_count);
  • frozen waits count frames with int(), so round(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.

FrameServer

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.

Stateful animations

The following are not pure functions of alpha:

  • accumulative animations (Rotate, Rotating, SpeedModifier, ChangeSpeed, ...);
  • animations with their own updaters;
  • custom interpolate_mobject that 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.

Key files

  • 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 live play loop

Clone this wiki locally