ManimCommunity / manim

A community-maintained Python framework for creating mathematical animations.
https://www.manim.community
MIT License
26.14k stars 1.79k forks source link

Updaters not refreshing between animations #3886

Open owl10124 opened 3 months ago

owl10124 commented 3 months ago

Description of bug / unexpected behavior

Updaters are not updated correctly on the first / last frames of animations.

In the below code sample, the red dot is expected to continuously move rightwards every frame: however, after the first animation, a frame is duplicated.

(The updater seems to be run twice between these two frames, but both with dt=0.)

Expected behavior

The updater should be run with dt=1/framerate, exactly once.

How to reproduce the issue

class Minimum(Scene):
    def construct(self):
        d = Dot(color=RED).move_to(UP)
        d.add_updater(lambda d,dt: d.set_x(d.get_x()+dt))
        def updater(d,dt):
            d.set_x(d.get_x()+dt)
            print(dt) # prints 0 at the end of first / beginning of second animation
        d.add_updater(updater)
        e,f = Dot(), Dot(color=BLUE)
        self.play(Create(e))
        self.play(Create(f))

Additional media files

Gif output: Minimum_ManimCE_v0 18 1 Note the repeated frame 15-16.

Comments and workarounds

This issue was found while working on a periodic background animation.

This seems related to self.update_to_time(t) (in Scene.play_internal, line 1259) not performing any update on the first frame t=0.

My current workaround is to modify line 1007 of scene.py:

...
        else:
            step = 1 / config["frame_rate"]
            times = np.arange(0, run_time, step)
        time_progression = tqdm(
            times,
...

to

...
            times = np.arange(step, run_time+step, step)
...

System specifications

System Details - OS (with version, e.g., Windows 10 v2004 or macOS 10.15 (Catalina)): macOS Ventura 13.3.1 (a) - Python version (`python/py/python3 --version`): 3.10 - Manim version: v0.18.1
Plygon commented 2 months ago

This issue has already been noted in https://github.com/ManimCommunity/manim/issues/3005.

I forked Manim and applied behackl's suggestion from that repo, and that has done the trick for me.