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

Allow NumPy-like multi-indexing and slicing of Mobjects #4036

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

chopan050
Copy link
Contributor

@chopan050 chopan050 commented Dec 2, 2024

Closes #2916

This allows using multi-indexing, multi-slicing and fancy-indexing of Mobjects, in a similar way to how NumPy arrays can be indexed.

For example:

mobs = [VMobject(name=f"Mob{i}") for i in range(10)]
vgroups = [VGroup(*mobs[1:3]), VGroup(*mobs[3:6]), VGroup(mobs[7])]

base_mob = mobs[0]
base_mob.add(vgroups[0], vgroups[1], mobs[6])
mobs[3].add(vgroups[2], mobs[8])
mobs[8].add(mobs[9])

"""
Structure:

Mob0
 ├──VGroup [0]
 │   ├──Mob1 [0, 0]
 │   └──Mob2 [0, 1]
 ├──VGroup [1]
 │   ├──Mob3 [1, 0]
 │   │   ├──VGroup [1, 0, 0]
 │   │   │   └──Mob7 [1, 0, 0, 0]
 │   │   └──Mob8 [1, 0, 1]
 │   │       └──Mob9 [1, 0, 1, 0]
 │   ├──Mob4 [1, 1]
 │   └──Mob5 [1, 2]
 └──Mob6 [2]
"""
  • base_mob[0, 0] returns Mob1
  • base_mob[1, 0, 0] returns VGroup(Mob7)
  • base_mob[:2, 0] returns VGroup(Mob1, Mob3)
  • base_mob[:2, 1] returns VGroup(Mob2, Mob4)
  • base_mob[1, 0, :, 0] returns VGroup(Mob7, Mob9)
  • base_mob[[2, 0]] returns VGroup(Mob6, VGroup(Mob1, Mob2))
  • base_mob[[True, False, True]] returns VGroup(VGroup(Mob1, Mob2), Mob6)
  • etc.

See the added test for more examples of this.

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

Hope to add multi-dimensional slicing function for VGroup like Numpy.Array
1 participant