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 stillhttps://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 expectedhttps://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)
Source: ManimCommunity/manim