Skip to content
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

Select the last object of the group as the alignment target when using Mobject.arrange() #4183

Open
zhanbao2000 opened this issue Mar 7, 2025 · 0 comments
Labels
new feature Enhancement specifically adding a new feature (feature request should be used for issues instead)

Comments

@zhanbao2000
Copy link

Description of proposed feature

My motivation

Hello, I want to be able to select the last object in a group as the target for alignment when using Mobject.arrange(), instead of the default first object.

Like this:

ReversedArrange.mp4

Why

Mobject.arrange() always uses the first object as the alignment target:

manim/manim/mobject/mobject.py

Lines 2368 to 2372 in 2ab99b5

for m1, m2 in zip(self.submobjects, self.submobjects[1:]):
m2.next_to(m1, direction, buff, **kwargs)
if center:
self.center()
return self

If I want to implement reverse-arrange, I must use the Mobject.next_to() method one by one (in reverse order) for the objects within the group, as implemented in above code.

How can the new feature be used?

By adding a new param last in Mobject.arrange():

group.animate.arrange(center=False, last=True, direction=LEFT)

Additional comments

Here is my change of Mobject.arrange() to implement this feature:

def arrange(
    self,
    direction: Vector3D = RIGHT,
    buff: float = DEFAULT_MOBJECT_TO_MOBJECT_BUFFER,
    center: bool = True,
    last: bool = False,
    **kwargs,
) -> Self:
    if last:
        submobjects = list(reversed(self.submobjects))
    else:
        submobjects = self.submobjects
    
    for m1, m2 in zip(submobjects, submobjects[1:]):
        m2.next_to(m1, direction, buff, **kwargs)
    if center:
        self.center()
    return self

Here is my code to test this feature:

from manim import *

class ReversedArrange(Scene):

    def construct(self):
        square_r = Square(fill_color=RED, fill_opacity=1).move_to(ORIGIN + LEFT * 5)
        square_g = Square(fill_color=GREEN, fill_opacity=1).move_to(ORIGIN)
        square_b = Square(fill_color=BLUE, fill_opacity=1).move_to(ORIGIN + RIGHT * 5)

        group = Group(square_r, square_g, square_b)

        self.play(FadeIn(group))
        self.wait(1)

        self.play(group.animate.arrange(center=False, last=True, direction=LEFT))

If modify Mobject.arrange() as I did, you have to specify the direction param as LEFT when call this method, otherwise the other object in the group will move to the right of the target object (which is assumed to be the rightmost one). Like this:

self.play(group.animate.arrange(center=False, last=True))  # without direction=LEFT
ReversedArrange.mp4
@zhanbao2000 zhanbao2000 added the new feature Enhancement specifically adding a new feature (feature request should be used for issues instead) label Mar 7, 2025
@github-project-automation github-project-automation bot moved this to 🆕 New in Dev Board Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature Enhancement specifically adding a new feature (feature request should be used for issues instead)
Projects
Status: 🆕 New
Development

No branches or pull requests

1 participant