Skip to content

Fix duplicate references in scene.mobjects after ReplacementTransform with existing target mobject #4242

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

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

Conversation

irvanalhaq9
Copy link
Contributor

@irvanalhaq9 irvanalhaq9 commented May 9, 2025

Overview: What does this pull request change?

Fix #4238

Fix duplicate mobject reference issue when using ReplacementTransform

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

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.

ReplacementTransform leaves behind a “ghost” mobject when the target is already on‑screen
1 participant