Mobject moved by another mobject's updater stays frozen if it was added first
An updater that moves another mobject renders frozen, depending on add order
Description
When an updater attached to one mobject moves a different mobject that was added to the scene earlier, the moved mobject never moves on screen, with no error or warning.
Minimal example
Bad: driver added after the square, which stays still
from manim import *
class Repro(Scene):
def construct(self):
square = Square()
self.add(square)
driver = Mobject() # draws nothing; its updater rotates the square
driver.add_updater(lambda m, dt: square.rotate(dt))
self.add(driver)
self.wait(2) # expected: the square rotates. actual: it stays still
https://github.com/user-attachments/assets/7c721703-23fb-4b43-88e0-4a83346235ef
Good: driver added before the square, which rotates
from manim import *
class Control(Scene):
def construct(self):
driver = Mobject()
self.add(driver) # the only difference: driver added before the square
square = Square()
self.add(square)
driver.add_updater(lambda m, dt: square.rotate(dt))
self.wait(2) # rotates as expected
https://github.com/user-attachments/assets/6f29518a-0b8f-45f9-9b53-84a3543ccf98
Why
The updater does run every frame and the square's points do rotate: only the
drawing is skipped. Scene.get_moving_mobjects walks the mobjects in the order
they were added and treats the first one that is animated or has updaters, and
everything after it, as moving. Everything before it is painted once into the
static background (CairoRenderer.save_static_frame_data) and never redrawn
during the play. The square comes before driver and owns no updater, so it is
considered static, although driver's updater changes it.
Possible fix
Check the assumption instead of relying on it. When the static background is painted, keep a cheap fingerprint of each static mobject (its points and its fill and stroke colors). After the updaters run each frame, compare: if a static mobject changed, re-split the scene from the first changed mobject onward, with the same "this one and everything after it" rule so the draw order is kept, and repaint the background once.
# CairoRenderer.save_static_frame_data: remember what was painted
scene._static_prints = [(m, fingerprint(m)) for m in static_mobjects]
# Scene.update_to_time, after the updaters ran
changed = [m for m, p in self._static_prints if fingerprint(m) != p]
if changed:
order = self.get_mobject_family_members()
first = min(order.index(m) for m in changed)
self.moving_mobjects = order[first:]
self.renderer.save_static_frame_data(self, order[:first])
A scene whose updaters only change their own mobject behaves exactly as today, at the cost of fingerprinting the static mobjects each frame, which is far cheaper than drawing them. I prototyped this and the bad example above then rotates exactly like the good one.
If this is confirmed as a bug, should I open a PR with this approach?
Environment
- Manim Community v0.21.0 (PyPI), Cairo renderer
- Python 3.12.3, Linux (WSL2)
Yeah this is an old issue and your diagnosis is right! I do however question whether this is the right solution to the problem, at least on its own. Here are some of my thoughts, I'd be curious to hear your opinion.
- I think I'd need to see a benchmark to really buy that fingerprinting mobjects every frame this way is "cheap". It would obviously be a particularly annoying performance hit in scenes where there are no updaters which affect another mobject.
- Is the issue maybe actually with the way Manim is determining whether an object is moving or not? Is there maybe a way to handle "staticness" more intelligently or explicitly that would be useful for a larger class of mobjects?
- Memoized points lists/bounding boxes and "has changed" events have been on the docket for a good while, I think there are some open PRs in here somewhere. Maybe that would be a useful thing to look at? Then we wouldn't need to look at all static mobjects each frame and could instead, say, subscribe to the "has changed" event of each currently static mobject.
I tested the implementation in Hamdi's fork against its immediate parent on Apple M5, using the Cairo renderer. In the square-first example the parent produced a 122 px wide square in all 30 frames. With the fix, its width ranged from 122 to 172 px. Its thresholded outline matched the working driver-first control in every frame. Tested commit · Parent
For the performance question, I rendered a moving circle in front of static squares. Each number is the median wall time in milliseconds from scene construction through writing the MP4. Parentheses show the middle 50% of seven runs per checkout. I discarded one warm-up per case and alternated which checkout went first. Each clip has 30 frames at 854 × 480, with caching disabled. The decoded RGB frames of each ordinary scene matched between checkouts.
| Circle motion | Static squares | Parent, ms | Fix, ms |
|---|---|---|---|
Built-in .animate.shift |
32 | 152 (134–174) | 160 (136–197) |
Built-in .animate.shift |
256 | 277 (252–302) | 287 (271–308) |
| Updater moves only the circle | 32 | 128 (127–150) | 142 (132–191) |
| Updater moves only the circle | 256 | 265 (252–283) | 283 (265–300) |
The whole-render times overlap. To isolate the cost, I timed _StaticFrameWatch.changed() in four separate renders per case on the fixed commit. The built-in animation made zero calls at both sizes. The self-updater made one call per frame, with median call times of 62 µs for 32 squares and 222 µs for 256 squares (120 calls at each size). This measures the per-frame check. Watch setup, rendering and encoding are included only in the wall times above. The scenes use simple vector shapes, so image-heavy scenes are still an open measurement.
Reproduce the renders
I used Python 3.12.14 and Cairo 1.18.6. Save the script as bench.py outside the checkouts and run it against each source tree in the same environment, for example:
PYTHONPATH=/path/to/checkout python bench.py own-updater 256 /tmp/manim-bench
"""Run with PYTHONPATH pointing to the desired Manim source checkout."""
import argparse
import json
from pathlib import Path
from time import perf_counter
import manim
from manim import Circle, Mobject, RIGHT, Scene, Square, tempconfig
parser = argparse.ArgumentParser()
parser.add_argument("case", choices=("ordinary", "own-updater", "square-first", "driver-first"))
parser.add_argument("count", type=int)
parser.add_argument("out", type=Path)
args = parser.parse_args()
class Probe(Scene):
def construct(self):
if args.case in ("ordinary", "own-updater"):
for i in range(args.count):
square = Square(side_length=0.12)
square.move_to(((i % 24 - 11.5) * 0.28, (i // 24 - 5) * 0.28, 0))
self.add(square)
dot = Circle(radius=0.2).shift(2 * RIGHT)
self.add(dot)
if args.case == "ordinary":
self.play(dot.animate.shift(RIGHT), run_time=2)
else:
dot.add_updater(lambda mob, dt: mob.shift(0.5 * dt * RIGHT))
self.wait(2)
else:
square = Square()
driver = Mobject()
driver.add_updater(lambda mob, dt: square.rotate(dt))
if args.case == "square-first":
self.add(square, driver)
else:
self.add(driver, square)
self.wait(2)
args.out.mkdir(parents=True, exist_ok=True)
with tempconfig(dict(renderer="cairo", media_dir=str(args.out), format="mp4",
pixel_width=854, pixel_height=480, frame_rate=15,
disable_caching=True, progress_bar="none", verbosity="ERROR")):
start = perf_counter()
scene = Probe()
scene.render()
seconds = perf_counter() - start
video = Path(scene.renderer.file_writer.movie_file_path)
assert video.is_file(), video
print(json.dumps(dict(case=args.case, count=args.count, seconds=seconds,
video=str(video), source=manim.__file__)))