manim
manim copied to clipboard
Allowing `Swap` to accept `Group` and `VGroup` and add Example
Overview: What does this pull request change?
Fix #4210
This PR modifies CyclicReplace (which Swap is an alias for) to allow it to accept Group or VGroup directly, instead of always creating a new group internally.
Motivation and Explanation: Why and how do your changes improve the library?
Previously, CyclicReplace would create a new group from the given mobjects, making the previously created group unrecognized by self.remove(). Now, if a Group or VGroup is passed, it will be used directly rather than creating a new one.
This ensures that the original group remains in the scene, making it possible to remove it later if needed.
Example:
With this fix, the following code will now correctly remove text_group from the scene:
class SwapExample(Scene):
def construct(self):
text_a = Text("A").move_to(LEFT)
text_b = Text("B").move_to(RIGHT)
text_group = Group(text_a, text_b)
self.play(FadeIn(text_group))
self.play(Swap(text_group)) # passing a group, `Swap` does not create a new group
self.wait()
self.remove(text_group) # Successfully removes the group
self.wait()
Links to added or changed documentation pages
https://manimce--4211.org.readthedocs.build/en/4211/reference/manim.animation.transform.Swap.html
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