A community-maintained Python framework for creating mathematical animations.
Succession honors individual animations run times (#691)
fhoguinfcd4af01710c
diff --git a/manim/animation/composition.py b/manim/animation/composition.pyindex 21ef0845e..36a1c5488 100644--- a/manim/animation/composition.py+++ b/manim/animation/composition.py@@ -42,10 +42,14 @@ class AnimationGroup(Animation):def get_all_mobjects(self):return self.group+ def get_run_time(self):+ if super().get_run_time() is None:+ self.init_run_time()+ return super().get_run_time()+def begin(self):for anim in self.animations:anim.begin()- # self.init_run_time()def finish(self):for anim in self.animations:@@ -107,23 +111,41 @@ class Succession(AnimationGroup):def begin(self):assert len(self.animations) > 0self.init_run_time()- self.active_animation = self.animations[0]- self.active_animation.begin()+ self.update_active_animation(0)def finish(self):- self.active_animation.finish()+ while self.active_animation is not None:+ self.next_animation()def update_mobjects(self, dt):- self.active_animation.update_mobjects(dt)+ if self.active_animation:+ self.active_animation.update_mobjects(dt)++ def update_active_animation(self, index):+ self.active_index = index+ if index >= len(self.animations):+ self.active_animation = None+ self.active_start_time = None+ self.active_end_time = None+ else:+ self.active_animation = self.animations[index]+ self.active_animation.begin()+ self.active_start_time = self.anims_with_timings[index][1]+ self.active_end_time = self.anims_with_timings[index][2]++ def next_animation(self):+ self.active_animation.finish()+ self.update_active_animation(self.active_index + 1)def interpolate(self, alpha):- index, subalpha = integer_interpolate(0, len(self.animations), alpha)- animation = self.animations[index]- if animation is not self.active_animation:- self.active_animation.finish()- animation.begin()- self.active_animation = animation- animation.interpolate(subalpha)+ current_time = interpolate(0, self.run_time, alpha)+ while self.active_end_time is not None and current_time >= self.active_end_time:+ self.next_animation()+ if self.active_animation:+ elapsed = current_time - self.active_start_time+ active_run_time = self.active_animation.get_run_time()+ subalpha = elapsed / active_run_time if active_run_time != 0.0 else 1.0+ self.active_animation.interpolate(subalpha)class LaggedStart(AnimationGroup):diff --git a/tests/test_composition.py b/tests/test_composition.pynew file mode 100644index 000000000..6b3de0907--- /dev/null+++ b/tests/test_composition.py@@ -0,0 +1,78 @@+import pytest+from manim.animation.composition import Succession+from manim.animation.fading import FadeInFrom, FadeOutAndShift+from manim.constants import DOWN+from manim.mobject.geometry import Line+++def test_succession_timing():+ """Test timing of animations in a succession."""+ line = Line()+ animation_1s = FadeInFrom(line, direction=DOWN, run_time=1.0)+ animation_4s = FadeOutAndShift(line, direction=DOWN, run_time=4.0)+ succession = Succession(animation_1s, animation_4s)+ assert succession.get_run_time() == 5.0+ succession.begin()+ assert succession.active_index == 0+ # The first animation takes 20% of the total run time.+ succession.interpolate(0.199)+ assert succession.active_index == 0+ succession.interpolate(0.2)+ assert succession.active_index == 1+ succession.interpolate(0.8)+ assert succession.active_index == 1+ # At 100% and more, no animation must be active anymore.+ succession.interpolate(1.0)+ assert succession.active_index == 2+ assert succession.active_animation is None+ succession.interpolate(1.2)+ assert succession.active_index == 2+ assert succession.active_animation is None+++def test_succession_in_succession_timing():+ """Test timing of nested successions."""+ line = Line()+ animation_1s = FadeInFrom(line, direction=DOWN, run_time=1.0)+ animation_4s = FadeOutAndShift(line, direction=DOWN, run_time=4.0)+ nested_succession = Succession(animation_1s, animation_4s)+ succession = Succession(+ FadeInFrom(line, direction=DOWN, run_time=4.0),+ nested_succession,+ FadeInFrom(line, direction=DOWN, run_time=1.0),+ )+ assert nested_succession.get_run_time() == 5.0+ assert succession.get_run_time() == 10.0+ succession.begin()+ succession.interpolate(0.1)+ assert succession.active_index == 0+ # The nested succession must not be active yet, and as a result hasn't set active_animation yet.+ assert not hasattr(nested_succession, "active_animation")+ succession.interpolate(0.39)+ assert succession.active_index == 0+ assert not hasattr(nested_succession, "active_animation")+ # The nested succession starts at 40% of total run time+ succession.interpolate(0.4)+ assert succession.active_index == 1+ assert nested_succession.active_index == 0+ # The nested succession second animation starts at 50% of total run time.+ succession.interpolate(0.49)+ assert succession.active_index == 1+ assert nested_succession.active_index == 0+ succession.interpolate(0.5)+ assert succession.active_index == 1+ assert nested_succession.active_index == 1+ # The last animation starts at 90% of total run time. The nested succession must be finished at that time.+ succession.interpolate(0.89)+ assert succession.active_index == 1+ assert nested_succession.active_index == 1+ succession.interpolate(0.9)+ assert succession.active_index == 2+ assert nested_succession.active_index == 2+ assert nested_succession.active_animation is None+ # After 100%, nothing must be playing anymore.+ succession.interpolate(1.0)+ assert succession.active_index == 3+ assert succession.active_animation is None+ assert nested_succession.active_index == 2+ assert nested_succession.active_animation is None