Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Prevent TransformMatchingTex from crashing when there is nothing to fade (#2846)

Kevin Lubickc4f1d202a9c8
diff --git a/manim/animation/transform.py b/manim/animation/transform.py
index bff83be96..cdf44f409 100644
--- a/manim/animation/transform.py
+++ b/manim/animation/transform.py
@@ -823,8 +823,14 @@ class FadeTransform(Transform):
self.ghost_to(m0, m1)
def ghost_to(self, source, target):
- """Replaces the source by the target and sets the opacity to 0."""
- source.replace(target, stretch=self.stretch, dim_to_match=self.dim_to_match)
+ """Replaces the source by the target and sets the opacity to 0.
+
+ If the provided target has no points, and thus a location of [0, 0, 0]
+ the source will simply fade out where it currently is.
+ """
+ # mobject.replace() does not work if the target has no points.
+ if target.get_num_points() or target.submobjects:
+ source.replace(target, stretch=self.stretch, dim_to_match=self.dim_to_match)
source.set_opacity(0)
def get_all_mobjects(self) -> Sequence[Mobject]:
diff --git a/tests/test_graphical_units/control_data/transform/FadeTransform.npz b/tests/test_graphical_units/control_data/transform/FadeTransform.npz
new file mode 100644
index 000000000..674d33fdb
Binary files /dev/null and b/tests/test_graphical_units/control_data/transform/FadeTransform.npz differ
diff --git a/tests/test_graphical_units/control_data/transform/FadeTransformPieces.npz b/tests/test_graphical_units/control_data/transform/FadeTransformPieces.npz
new file mode 100644
index 000000000..be97f3f26
Binary files /dev/null and b/tests/test_graphical_units/control_data/transform/FadeTransformPieces.npz differ
diff --git a/tests/test_graphical_units/control_data/transform/FadeTransform_TargetIsEmpty_FadesOutInPlace.npz b/tests/test_graphical_units/control_data/transform/FadeTransform_TargetIsEmpty_FadesOutInPlace.npz
new file mode 100644
index 000000000..b61b4ae58
Binary files /dev/null and b/tests/test_graphical_units/control_data/transform/FadeTransform_TargetIsEmpty_FadesOutInPlace.npz differ
diff --git a/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex.npz b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex.npz
new file mode 100644
index 000000000..ed183f14c
Binary files /dev/null and b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex.npz differ
diff --git a/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex_FadeTransformMismatches.npz b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex_FadeTransformMismatches.npz
new file mode 100644
index 000000000..dd6aa6ad5
Binary files /dev/null and b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex_FadeTransformMismatches.npz differ
diff --git a/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex_FadeTransformMismatches_NothingToFade.npz b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex_FadeTransformMismatches_NothingToFade.npz
new file mode 100644
index 000000000..df36fde01
Binary files /dev/null and b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex_FadeTransformMismatches_NothingToFade.npz differ
diff --git a/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex_TransformMismatches.npz b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex_TransformMismatches.npz
new file mode 100644
index 000000000..1c2092478
Binary files /dev/null and b/tests/test_graphical_units/control_data/transform_matching_parts/TransformMatchingTex_TransformMismatches.npz differ
diff --git a/tests/test_graphical_units/test_transform.py b/tests/test_graphical_units/test_transform.py
index aa5e74047..6b8fa9523 100644
--- a/tests/test_graphical_units/test_transform.py
+++ b/tests/test_graphical_units/test_transform.py
@@ -199,3 +199,40 @@ def test_TransformWithConflictingPaths(scene):
path_arc_centers=ORIGIN,
)
)
+
+
+@frames_comparison(last_frame=False)
+def test_FadeTransformPieces(scene):
+ src = VGroup(Square(), Circle().shift(LEFT + UP))
+ src.shift(3 * LEFT)
+
+ target = VGroup(Circle(), Triangle().shift(RIGHT + DOWN))
+ target.shift(3 * RIGHT)
+
+ scene.add(src)
+ scene.play(FadeTransformPieces(src, target))
+
+
+@frames_comparison(last_frame=False)
+def test_FadeTransform(scene):
+ src = Square(fill_opacity=1.0)
+ src.shift(3 * LEFT)
+
+ target = Circle(fill_opacity=1.0, color=ORANGE)
+ target.shift(3 * RIGHT)
+
+ scene.add(src)
+ scene.play(FadeTransform(src, target))
+
+
+@frames_comparison(last_frame=False)
+def test_FadeTransform_TargetIsEmpty_FadesOutInPlace(scene):
+ # https://github.com/ManimCommunity/manim/issues/2845
+ src = Square(fill_opacity=1.0)
+ src.shift(3 * LEFT)
+
+ target = VGroup()
+ target.shift(3 * RIGHT)
+
+ scene.add(src)
+ scene.play(FadeTransform(src, target))
diff --git a/tests/test_graphical_units/test_transform_matching_parts.py b/tests/test_graphical_units/test_transform_matching_parts.py
index c1f01f9f0..b5af1fb2a 100644
--- a/tests/test_graphical_units/test_transform_matching_parts.py
+++ b/tests/test_graphical_units/test_transform_matching_parts.py
@@ -25,3 +25,40 @@ def test_TransformMatchingDisplaysCorrect(scene):
scene.wait(0.5)
# Shift to make sure object isn't duplicated if moved
scene.play(circle.animate.shift(DOWN))
+
+
+@frames_comparison(last_frame=False)
+def test_TransformMatchingTex(scene):
+ start = MathTex("A", "+", "B", "=", "C")
+ end = MathTex("C", "=", "B", "-", "A")
+
+ scene.add(start)
+ scene.play(TransformMatchingTex(start, end))
+
+
+@frames_comparison(last_frame=False)
+def test_TransformMatchingTex_FadeTransformMismatches(scene):
+ start = MathTex("A", "+", "B", "=", "C")
+ end = MathTex("C", "=", "B", "-", "A")
+
+ scene.add(start)
+ scene.play(TransformMatchingTex(start, end, fade_transform_mismatches=True))
+
+
+@frames_comparison(last_frame=False)
+def test_TransformMatchingTex_TransformMismatches(scene):
+ start = MathTex("A", "+", "B", "=", "C")
+ end = MathTex("C", "=", "B", "-", "A")
+
+ scene.add(start)
+ scene.play(TransformMatchingTex(start, end, transform_mismatches=True))
+
+
+@frames_comparison(last_frame=False)
+def test_TransformMatchingTex_FadeTransformMismatches_NothingToFade(scene):
+ # https://github.com/ManimCommunity/manim/issues/2845
+ start = MathTex("A", r"\to", "B")
+ end = MathTex("B", r"\to", "A")
+
+ scene.add(start)
+ scene.play(TransformMatchingTex(start, end, fade_transform_mismatches=True))