Make `AnimationGroup` and `Succession` behave more intuituively
Description of bug / unexpected behavior
Currently, AnimationGroup, and thereby Succession, often behaves unintuitively since the setup/clean-up methods of the contained animations are called through the AnimationGroup's correspondibg method, instead of at the actual contained animation's start and end times:
class AnimationGroup(Animation):
def begin(self) -> None:
for anim in self.animations:
anim.begin()
def _setup_scene(self, scene: Scene) -> None:
for anim in self.animations:
anim._setup_scene(scene)
def finish(self) -> None:
for anim in self.animations:
anim.finish()
def clean_up_from_scene(self, scene: Scene) -> None:
for anim in self.animations:
anim.clean_up_from_scene(scene)
My suggestion is to call the setup and clean-up methods of the contained animations at their actual start and end timings.
Prototype
Here is a prototype of my suggestion. It misses some features, but the core logic works:
class AnimationGroup(Animation):
def __init__(self, *animations: Animation, lag_ratio: float = 0.0, run_time: float = 1.0, rate_func=linear) -> None:
super().__init__(Mobject(), run_time=run_time, rate_func=rate_func)
self.animations = animations
self.lag_ratio = lag_ratio
def _setup_scene(self, scene: Scene) -> None:
self._scene = scene
super()._setup_scene(scene)
def begin(self) -> None:
self._started_animations: set[Animation] = set()
self._finished_animations: set[Animation] = set()
super().begin()
def interpolate_mobject(self, alpha: float) -> None:
alpha = self.rate_func(alpha)
timings = self._get_animation_timings()
for (animation_start, animation_end), animation in zip(timings, self.animations, strict=True):
if animation_start <= alpha and animation not in self._started_animations:
animation._setup_scene(self._scene)
animation.begin()
self._started_animations.add(animation)
if animation in self._started_animations and animation not in self._finished_animations:
animation_duration = animation_end - animation_start
if animation_duration > 0:
animation_alpha = (alpha - animation_start) / animation_duration
animation_alpha = max(0, min(animation_alpha, 1))
else:
animation_alpha = 1
animation.interpolate_mobject(animation_alpha)
if animation_end <= alpha and animation not in self._finished_animations:
animation.finish()
animation.clean_up_from_scene(self._scene)
self._finished_animations.add(animation)
def _get_animation_timings(self) -> list[tuple[float, float]]:
start_and_end_times: list[tuple[float, float]] = []
if self.run_time <= 0.0:
return [(0, 0) for _ in self.animations]
time = 0.0
total_run_time = 0.0
for animation in self.animations:
start = time
end = time + animation.run_time
start_and_end_times.append((start, end))
time += animation.run_time * self.lag_ratio
total_run_time = max(total_run_time, end)
if total_run_time <= 0.0:
return start_and_end_times
return [(start / total_run_time, end / total_run_time) for start, end in start_and_end_times]
Remarks
- Doesn't address the problem of animations interfering with each other when modifying the same object.
- I put some thought into ensuring that the setup and clean-up methods cant be skipped.
Example
class TestScene(Scene):
def construct(self) -> None:
self.play(
AnimationGroup(
FadeIn(Square()),
FadeIn(Circle()),
lag_ratio=1.0,
run_time=5.0,
)
)
https://github.com/user-attachments/assets/9c1ffcf1-de02-4fe7-8346-9a627f4a3ca7
I havent't had a chance to look closely at the suggested fix yet - how well does this work with a reversed rate function? That seems to be a recurring theme in the PRs that lead to the current implementation (#3542, 3951).
3. I havent't had a chance to look closely at the suggested fix yet - how well does this work with a reversed rate function? That seems to be a recurring theme in the PRs that lead to the current implementation (#3542, 3951).
My prototype doesn't work (correctly) when the alpha value goes from high to low. E.g. with:
rate_func=lambda t: 1-t
Thanks for checking. That probably isn't a complete dealbreaker, as I suspect the current implementation doesn't work properly with reversed rate funcs either.