Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix TransformMatchingAbstractBase Mobject leaks (#2826)

Simeon Widdis3ae46a4c133f
diff --git a/manim/animation/transform_matching_parts.py b/manim/animation/transform_matching_parts.py
index 7cac27477..85fe5b4aa 100644
--- a/manim/animation/transform_matching_parts.py
+++ b/manim/animation/transform_matching_parts.py
@@ -122,6 +122,7 @@ class TransformMatchingAbstractBase(AnimationGroup):
fade_source.add(source_map[key])
for key in set(target_map).difference(source_map):
fade_target.add(target_map[key])
+ fade_target_copy = fade_target.copy()
if transform_mismatches:
if "replace_mobject_with_target_in_scene" not in kwargs:
@@ -132,12 +133,12 @@ class TransformMatchingAbstractBase(AnimationGroup):
else:
anims.append(FadeOut(fade_source, target_position=fade_target, **kwargs))
anims.append(
- FadeIn(fade_target.copy(), target_position=fade_target, **kwargs),
+ FadeIn(fade_target_copy, target_position=fade_target, **kwargs),
)
super().__init__(*anims)
- self.to_remove = mobject
+ self.to_remove = [mobject, fade_target_copy]
self.to_add = target_mobject
def get_shape_map(self, mobject: Mobject) -> dict:
@@ -153,10 +154,11 @@ class TransformMatchingAbstractBase(AnimationGroup):
return shape_map
def clean_up_from_scene(self, scene: Scene) -> None:
+ # Interpolate all animations back to 0 to ensure source mobjects remain unchanged.
for anim in self.animations:
anim.interpolate(0)
scene.remove(self.mobject)
- scene.remove(self.to_remove)
+ scene.remove(*self.to_remove)
scene.add(self.to_add)
@staticmethod
diff --git a/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingDisplaysCorrect.npz b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingDisplaysCorrect.npz
new file mode 100644
index 000000000..01d1afb31
Binary files /dev/null and b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingDisplaysCorrect.npz differ
diff --git a/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingLeavesOneObject.npz b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingLeavesOneObject.npz
new file mode 100644
index 000000000..52c97e717
Binary files /dev/null and b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingLeavesOneObject.npz differ
diff --git a/tests/test_graphical_units/test_transform_matching_parts.py b/tests/test_graphical_units/test_transform_matching_parts.py
new file mode 100644
index 000000000..c1f01f9f0
--- /dev/null
+++ b/tests/test_graphical_units/test_transform_matching_parts.py
@@ -0,0 +1,27 @@
+from __future__ import annotations
+
+from manim import *
+from manim.utils.testing.frames_comparison import frames_comparison
+
+__module_test__ = "transform_matching_parts"
+
+
+@frames_comparison(last_frame=True)
+def test_TransformMatchingLeavesOneObject(scene):
+ square = Square()
+ circle = Circle().shift(RIGHT)
+ scene.add(square)
+ scene.play(TransformMatchingShapes(square, circle))
+ assert len(scene.mobjects) == 1 and isinstance(scene.mobjects[0], Circle)
+
+
+@frames_comparison(last_frame=False)
+def test_TransformMatchingDisplaysCorrect(scene):
+ square = Square()
+ circle = Circle().shift(RIGHT)
+ scene.add(square)
+ scene.play(TransformMatchingShapes(square, circle))
+ # Wait to make sure object isn't missing in-between animations
+ scene.wait(0.5)
+ # Shift to make sure object isn't duplicated if moved
+ scene.play(circle.animate.shift(DOWN))