A community-maintained Python framework for creating mathematical animations.
Fixed render flow issues with introducer animations (#2594)
Benjamin Hackl294efcd65178
diff --git a/manim/animation/animation.py b/manim/animation/animation.pyindex 039cc93ba..acff189a2 100644--- a/manim/animation/animation.py+++ b/manim/animation/animation.py@@ -233,7 +233,10 @@ class Animation:"""if scene is None:return- if self.is_introducer():+ if (+ self.is_introducer()+ and self.mobject not in scene.get_mobject_family_members()+ ):scene.add(self.mobject)def create_starting_mobject(self) -> Mobject:diff --git a/manim/renderer/cairo_renderer.py b/manim/renderer/cairo_renderer.pyindex 37b827878..c47e17909 100644--- a/manim/renderer/cairo_renderer.py+++ b/manim/renderer/cairo_renderer.py@@ -86,13 +86,14 @@ class CairoRenderer:{"h": str(self.animations_hashes[:5])},)+ self.file_writer.begin_animation(not self.skip_animations)+ scene.begin_animations()+# Save a static image, to avoid rendering non moving objects.self.static_image = self.save_static_frame_data(scene, scene.static_mobjects)- self.file_writer.begin_animation(not self.skip_animations)- scene.begin_animations()if scene.is_current_animation_frozen_frame():- self.update_frame(scene)+ self.update_frame(scene, mobjects=scene.moving_mobjects)# self.duration stands for the total run time of all the animations.# In this case, as there is only a wait, it will be the length of the wait.self.freeze_current_frame(scene.duration)@@ -218,8 +219,8 @@ class CairoRenderer:typing.Iterable[Mobject]the static image computed."""+ self.static_image = Noneif not static_mobjects:- self.static_image = Nonereturn Noneself.update_frame(scene, mobjects=static_mobjects)self.static_image = self.get_frame()@@ -258,8 +259,10 @@ class CairoRenderer:config.save_last_frame = Trueconfig.write_to_movie = Falseelse:+ self.static_image = Noneself.update_frame(scene)if config["save_last_frame"]:+ self.static_image = Noneself.update_frame(scene)self.file_writer.save_final_image(self.camera.get_image())diff --git a/manim/scene/scene.py b/manim/scene/scene.pyindex f751a8b2e..2fc4add87 100644--- a/manim/scene/scene.py+++ b/manim/scene/scene.py@@ -451,7 +451,7 @@ class Scene:mobjects = [*mobjects, *self.foreground_mobjects]self.restructure_mobjects(to_remove=mobjects)self.mobjects += mobjects- if self.moving_mobjects is not None:+ if self.moving_mobjects:self.restructure_mobjects(to_remove=mobjects,mobject_list_name="moving_mobjects",@@ -1074,13 +1074,6 @@ class Scene:# Static image logic when the wait is static is done by the renderer, not here.self.animations[0].is_static_wait = Truereturn None- elif config.renderer != "opengl":- # Paint all non-moving objects onto the screen, so they don't- # have to be rendered every frame- (- self.moving_mobjects,- self.static_mobjects,- ) = self.get_moving_and_static_mobjects(self.animations)self.duration = self.get_run_time(self.animations)return self@@ -1090,6 +1083,14 @@ class Scene:animation._setup_scene(self)animation.begin()+ if config.renderer != "opengl":+ # Paint all non-moving objects onto the screen, so they don't+ # have to be rendered every frame+ (+ self.moving_mobjects,+ self.static_mobjects,+ ) = self.get_moving_and_static_mobjects(self.animations)+def is_current_animation_frozen_frame(self) -> bool:"""Returns whether the current animation produces a static frame (generally a Wait)."""return (diff --git a/tests/opengl/test_composition_opengl.py b/tests/opengl/test_composition_opengl.pyindex 0f4de3cb1..1bedc3edc 100644--- a/tests/opengl/test_composition_opengl.py+++ b/tests/opengl/test_composition_opengl.py@@ -1,6 +1,6 @@from __future__ import annotations-from unittest.mock import Mock+from unittest.mock import MagicMockfrom manim.animation.animation import Animation, Waitfrom manim.animation.composition import AnimationGroup, Succession@@ -18,7 +18,7 @@ def test_succession_timing(using_opengl_renderer):animation_4s = FadeOut(line, shift=DOWN, run_time=4.0)succession = Succession(animation_1s, animation_4s)assert succession.get_run_time() == 5.0- succession._setup_scene(Mock())+ succession._setup_scene(MagicMock())succession.begin()assert succession.active_index == 0# The first animation takes 20% of the total run time.@@ -50,7 +50,7 @@ def test_succession_in_succession_timing(using_opengl_renderer):)assert nested_succession.get_run_time() == 5.0assert succession.get_run_time() == 10.0- succession._setup_scene(Mock())+ succession._setup_scene(MagicMock())succession.begin()succession.interpolate(0.1)assert succession.active_index == 0diff --git a/tests/test_composition.py b/tests/test_composition.pyindex 5660d7717..9f2647455 100644--- a/tests/test_composition.py+++ b/tests/test_composition.py@@ -1,6 +1,6 @@from __future__ import annotations-from unittest.mock import Mock+from unittest.mock import MagicMockimport pytest@@ -22,7 +22,7 @@ def test_succession_timing():animation_4s = FadeOut(line, shift=DOWN, run_time=4.0)succession = Succession(animation_1s, animation_4s)assert succession.get_run_time() == 5.0- succession._setup_scene(Mock())+ succession._setup_scene(MagicMock())succession.begin()assert succession.active_index == 0# The first animation takes 20% of the total run time.@@ -54,7 +54,7 @@ def test_succession_in_succession_timing():)assert nested_succession.get_run_time() == 5.0assert succession.get_run_time() == 10.0- succession._setup_scene(Mock())+ succession._setup_scene(MagicMock())succession.begin()succession.interpolate(0.1)assert succession.active_index == 0diff --git a/tests/test_graphical_units/control_data/creation/bring_to_back_introducer.npz b/tests/test_graphical_units/control_data/creation/bring_to_back_introducer.npznew file mode 100644index 000000000..989a820e4Binary files /dev/null and b/tests/test_graphical_units/control_data/creation/bring_to_back_introducer.npz differdiff --git a/tests/test_graphical_units/control_data/creation/z_index_introducer.npz b/tests/test_graphical_units/control_data/creation/z_index_introducer.npznew file mode 100644index 000000000..cc87472d4Binary files /dev/null and b/tests/test_graphical_units/control_data/creation/z_index_introducer.npz differdiff --git a/tests/test_graphical_units/control_data/vector_scene/vector_to_coords.npz b/tests/test_graphical_units/control_data/vector_scene/vector_to_coords.npzindex d7d6eb905..0ec172436 100644Binary files a/tests/test_graphical_units/control_data/vector_scene/vector_to_coords.npz and b/tests/test_graphical_units/control_data/vector_scene/vector_to_coords.npz differdiff --git a/tests/test_graphical_units/test_creation.py b/tests/test_graphical_units/test_creation.pyindex 87bc6e212..c6ce35ba1 100644--- a/tests/test_graphical_units/test_creation.py+++ b/tests/test_graphical_units/test_creation.py@@ -67,3 +67,23 @@ def test_SpinInFromNothing(scene):def test_ShrinkToCenter(scene):square = Square()scene.play(ShrinkToCenter(square))+++@frames_comparison(last_frame=False)+def test_bring_to_back_introducer(scene):+ a = Square(color=RED, fill_opacity=1)+ b = Square(color=BLUE, fill_opacity=1).shift(RIGHT)+ scene.add(a)+ scene.bring_to_back(b)+ scene.play(FadeIn(b))+ scene.wait()+++@frames_comparison(last_frame=False)+def test_z_index_introducer(scene):+ a = Circle().set_fill(color=RED, opacity=1.0)+ scene.add(a)+ b = Circle(arc_center=(0.5, 0.5, 0.0), color=GREEN, fill_opacity=1)+ b.set_z_index(-1)+ scene.play(Create(b))+ scene.wait()diff --git a/tests/test_graphical_units/test_vector_scene.py b/tests/test_graphical_units/test_vector_scene.pyindex 52d1b473b..59dfda44a 100644--- a/tests/test_graphical_units/test_vector_scene.py+++ b/tests/test_graphical_units/test_vector_scene.py@@ -13,3 +13,4 @@ def test_vector_to_coords(scene):basis = scene.get_basis_vectors()scene.add(basis)scene.vector_to_coords(vector=vector)+ scene.wait()