manim
manim copied to clipboard
Fix duplicate references in `scene.mobjects` after `ReplacementTransform` with existing target mobject
Overview: What does this pull request change?
Fix https://github.com/ManimCommunity/manim/issues/4238
Fix duplicate mobject reference issue when using ReplacementTransform if the target mobject is already in the scene.
Motivation and Explanation: Why and how do your changes improve the library?
Previously, if the target mobject was already in the scene, ReplacementTransform would replace the source mobject in the scene.mobjects list without removing the existing reference to the target mobject. This caused scene.mobjects to contain two references to the same object (i.e., the target mobject), leading to unexpected behavior in #4238.
Before this fix:
from manim import Circle, ReplacementTransform, Scene, Square
scene = Scene()
c = Circle()
sq = Square()
scene.add(c, sq)
print(scene.mobjects) # [Circle, Square]
scene.play(ReplacementTransform(c, sq))
print(scene.mobjects) # [Square, Square] <- Duplicate reference to Square
mob = scene.mobjects
print(mob[0] is mob[1]) # True
After this fix:
from manim import Circle, ReplacementTransform, Scene, Square
scene = Scene()
c = Circle()
sq = Square()
scene.add(c, sq)
print(scene.mobjects) # [Circle, Square]
scene.play(ReplacementTransform(c, sq))
print(scene.mobjects) # [Square] <- Only one reference remains
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