Skip to content

Add support for negative z-index in AnimationGroup #4389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion manim/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,33 @@ def clear(self) -> Self:
self.foreground_mobjects = []
return self

def recursively_unpack_animation_groups(
self, *animations: Animation
) -> list[Mobject | OpenGLMobject]:
"""
Unpacks animations
Parameters
----------
*animations
The animations to unpack
Returns
------
list
The list of mobjects in animations
"""
# Imported inside the method to avoid cyclic import
from ..animation.composition import AnimationGroup

unpacked_mobjects = []
for anim in animations:
if isinstance(anim, AnimationGroup):
for sub in anim.animations:
unpacked = self.recursively_unpack_animation_groups(sub)
unpacked_mobjects.extend(unpacked)
else:
unpacked_mobjects.append(anim.mobject)
return unpacked_mobjects

def get_moving_mobjects(self, *animations: Animation) -> list[Mobject]:
"""
Gets all moving mobjects in the passed animation(s).
Expand All @@ -904,7 +931,7 @@ def get_moving_mobjects(self, *animations: Animation) -> list[Mobject]:
# 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]
animation_mobjects = self.recursively_unpack_animation_groups(*animations)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it works, I'd prefer a simpler solution not requiring a new method; something along the lines of the suggestion from my previous review:

Suggested change
animation_mobjects = self.recursively_unpack_animation_groups(*animations)
animation_mobjects = []
for anim in animations:
animation_mobject.extend(anim.mob.get_family())

mobjects = self.get_mobject_family_members()
for i, mob in enumerate(mobjects):
update_possibilities = [
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
21 changes: 21 additions & 0 deletions tests/test_graphical_units/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading