ManimCommunity / ManimCommunity/manim

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

Open Beginner friendly
#4,183 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

good first issue new feature
Dominant language
Python
Stars
40.9k
Forks
3.1k
Avg merge
3d 12h
Merged PRs (30d)
25

Description

## 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:

https://github.com/user-attachments/assets/abd9c835-6a4e-4182-be2c-b71372e7c2a9

### Why

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

https://github.com/ManimCommunity/manim/blob/2ab99b53d3e8f43b1cd7245208d78c5dd6f9414a/manim/mobject/mobject.py#L2368-L2372

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()`:

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

## Additional comments

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

```python
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:

```python
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:

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

https://github.com/user-attachments/assets/62a1595f-1fff-4405-a313-1fdd9396296b

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in manim/mobject/mobject.py at Mobject.arrange and compare the proposed last-object behavior with the existing direction and center handling. Done means arrange can use the final submobject as the alignment target while preserving the current default behavior and supporting the demonstrated reverse-arrange usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-graphics
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.