Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Add support for negative z-index in AnimationGroup (#4389)

Matvey Merzlikin87cd63549c92
diff --git a/manim/scene/scene.py b/manim/scene/scene.py
index 96d5fd9ab..845fafd0b 100644
--- a/manim/scene/scene.py
+++ b/manim/scene/scene.py
@@ -903,7 +903,24 @@ class Scene:
# as soon as there's one that needs updating of
# some kind per frame, return the list from that
# point forward.
- animation_mobjects = [anim.mobject for anim in animations]
+ # Imported inside the method to avoid cyclic import.
+ from ..animation.composition import AnimationGroup
+
+ def _collect_animation_mobjects(
+ nested_animations: Iterable[Animation],
+ ) -> list[Mobject | OpenGLMobject]:
+ animation_mobjects: list[Mobject | OpenGLMobject] = []
+ for anim in nested_animations:
+ if isinstance(anim, AnimationGroup):
+ animation_mobjects.extend(
+ _collect_animation_mobjects(anim.animations),
+ )
+ else:
+ animation_mobjects.extend(anim.mobject.get_family())
+ return animation_mobjects
+
+ animation_mobjects = _collect_animation_mobjects(animations)
+
mobjects = self.get_mobject_family_members()
for i, mob in enumerate(mobjects):
update_possibilities = [
diff --git a/tests/test_graphical_units/control_data/geometry/negative_z_index_AnimationGroup.npz b/tests/test_graphical_units/control_data/geometry/negative_z_index_AnimationGroup.npz
new file mode 100644
index 000000000..80ba71a0e
Binary files /dev/null and b/tests/test_graphical_units/control_data/geometry/negative_z_index_AnimationGroup.npz differ
diff --git a/tests/test_graphical_units/control_data/geometry/negative_z_index_LaggedStart.npz b/tests/test_graphical_units/control_data/geometry/negative_z_index_LaggedStart.npz
new file mode 100644
index 000000000..dd5e8344d
Binary files /dev/null and b/tests/test_graphical_units/control_data/geometry/negative_z_index_LaggedStart.npz differ
diff --git a/tests/test_graphical_units/control_data/geometry/nested_animation_groups_with_negative_z_index.npz b/tests/test_graphical_units/control_data/geometry/nested_animation_groups_with_negative_z_index.npz
new file mode 100644
index 000000000..65c25c699
Binary files /dev/null and b/tests/test_graphical_units/control_data/geometry/nested_animation_groups_with_negative_z_index.npz differ
diff --git a/tests/test_graphical_units/test_geometry.py b/tests/test_graphical_units/test_geometry.py
index fef2ca095..9f9b6eda5 100644
--- a/tests/test_graphical_units/test_geometry.py
+++ b/tests/test_graphical_units/test_geometry.py
@@ -174,6 +174,27 @@ def test_ZIndex(scene):
scene.play(ApplyMethod(triangle.shift, 2 * UP))
+@frames_comparison(last_frame=False)
+def test_negative_z_index_AnimationGroup(scene):
+ # https://github.com/ManimCommunity/manim/issues/3334
+ s = Square().set_z_index(-1)
+ scene.play(AnimationGroup(GrowFromCenter(s)))
+
+
+@frames_comparison(last_frame=False)
+def test_negative_z_index_LaggedStart(scene):
+ # https://github.com/ManimCommunity/manim/issues/3914
+ line_1 = Line(LEFT, RIGHT, color=BLUE)
+ line_2 = Line(UP + LEFT, UP + RIGHT, color=RED).set_z_index(-1)
+ scene.play(LaggedStart(FadeIn(line_1), FadeIn(line_2), lag_ratio=0.5))
+
+
+@frames_comparison(last_frame=False)
+def test_nested_animation_groups_with_negative_z_index(scene):
+ line = Line(LEFT, RIGHT, color=BLUE).set_z_index(-1)
+ scene.play(AnimationGroup(AnimationGroup(AnimationGroup(FadeIn(line)))))
+
+
@frames_comparison
def test_Angle(scene):
l1 = Line(ORIGIN, RIGHT)