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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions manim/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ def replace(self, old_mobject: Mobject, new_mobject: Mobject) -> None:
def replace_in_list(
mobj_list: list[Mobject], old_m: Mobject, new_m: Mobject
) -> bool:
# Avoid duplicate references to the same object in self.mobjects
if new_m in mobj_list:
mobj_list.remove(new_m)

# We use breadth-first search because some Mobjects get very deep and
# we expect top-level elements to be the most common targets for replace.
for i in range(0, len(mobj_list)):
Expand Down
27 changes: 27 additions & 0 deletions tests/module/animation/test_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from __future__ import annotations

from manim import Circle, ReplacementTransform, Scene, Square, VGroup


def test_no_duplicate_references():
scene = Scene()
c = Circle()
sq = Square()
scene.add(c, sq)

scene.play(ReplacementTransform(c, sq))
assert len(scene.mobjects) == 1
assert scene.mobjects[0] is sq


def test_duplicate_references_in_group():
scene = Scene()
c = Circle()
sq = Square()
vg = VGroup(c, sq)
scene.add(vg)

scene.play(ReplacementTransform(c, sq))
submobs = vg.submobjects
assert len(submobs) == 1
assert submobs[0] is sq