Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Allow for chaining _AnimationBuilder (#889)

Devin Neal2faa89e3d367
diff --git a/docs/source/examples.rst b/docs/source/examples.rst
index 5a7eebb8e..41e1e3c91 100644
--- a/docs/source/examples.rst
+++ b/docs/source/examples.rst
@@ -396,7 +396,7 @@ Special Camera Settings
dot_at_start_graph = Dot().move_to(graph.points[0])
dot_at_end_graph = Dot().move_to(graph.points[-1])
self.add(graph, dot_at_end_graph, dot_at_start_graph, moving_dot)
- self.play(self.camera_frame.animate.scale(0.5),self.camera_frame.animate.move_to(moving_dot))
+ self.play(self.camera_frame.animate.scale(0.5).move_to(moving_dot))
def update_curve(mob):
mob.move_to(moving_dot.get_center())
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index 6273dbb9a..2ab908649 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -59,12 +59,46 @@ class Mobject(Container):
@property
def animate(self):
- """
- Used to animate the application of a method.
+ """Used to animate the application of a method.
+
+ .. warning::
+
+ Passing multiple animations for the same :class:`~.Mobject` in one
+ call to :meth:`~.Scene.play` is discouraged and will most likely
+ not work properly. Instead of writing an animation like
+
+ ::
+
+ self.play(my_mobject.animate.shift(RIGHT), my_mobject.animate.rotate(PI))
+
+ make use of method chaining for ``animate``, meaning::
+
+ self.play(my_mobject.animate.shift(RIGHT).rotate(PI))
Examples
--------
- self.play(mobject.animate.shift(RIGHT))
+
+ .. manim:: AnimateExample
+
+ class AnimateExample(Scene):
+ def construct(self):
+ s = Square()
+ self.play(ShowCreation(s))
+ self.play(s.animate.shift(RIGHT))
+ self.play(s.animate.scale(2))
+ self.play(s.animate.rotate(PI / 2))
+ self.play(Uncreate(s))
+
+
+ .. manim:: AnimateChainExample
+
+ class AnimateChainExample(Scene):
+ def construct(self):
+ s = Square()
+ self.play(ShowCreation(s))
+ self.play(s.animate.shift(RIGHT).scale(2).rotate(PI / 2))
+ self.play(Uncreate(s))
+
"""
return _AnimationBuilder(self)
@@ -1309,16 +1343,21 @@ class Group(Mobject):
class _AnimationBuilder:
- def __init__(self, mobject):
+ def __init__(self, mobject, generate_target=True):
self.mobject = mobject
+ if generate_target:
+ self.mobject.generate_target()
def __getattr__(self, method_name):
- from ..animation.transform import _MethodAnimation
+ method = getattr(self.mobject.target, method_name)
- self.method = getattr(self.mobject.generate_target(), method_name)
+ def update_target(*method_args, **method_kwargs):
+ method(*method_args, **method_kwargs)
+ return _AnimationBuilder(self.mobject, generate_target=False)
- def build(*method_args, **method_kwargs):
- self.method(*method_args, **method_kwargs)
- return _MethodAnimation(self.mobject)
+ return update_target
+
+ def build(self):
+ from ..animation.transform import _MethodAnimation
- return build
+ return _MethodAnimation(self.mobject)
diff --git a/manim/scene/moving_camera_scene.py b/manim/scene/moving_camera_scene.py
index 8714663d1..218a745df 100644
--- a/manim/scene/moving_camera_scene.py
+++ b/manim/scene/moving_camera_scene.py
@@ -40,14 +40,11 @@ Examples
s = Square(color=BLUE, fill_opacity=0.5).move_to(2 * LEFT)
t = Triangle(color=YELLOW, fill_opacity=0.5).move_to(2 * RIGHT)
self.add(s, t)
- self.play(self.camera_frame.animate.move_to(s),
- self.camera_frame.animate.set_width(s.get_width()*2))
+ self.play(self.camera_frame.animate.move_to(s).set_width(s.get_width()*2))
self.wait(0.3)
- self.play(self.camera_frame.animate.move_to(t),
- self.camera_frame.animate.set_width(t.get_width()*2))
+ self.play(self.camera_frame.animate.move_to(t).set_width(t.get_width()*2))
- self.play(self.camera_frame.animate.move_to(ORIGIN),
- self.camera_frame.animate.set_width(14))
+ self.play(self.camera_frame.animate.move_to(ORIGIN).set_width(14))
.. manim:: MovingCameraOnGraph
@@ -66,7 +63,7 @@ Examples
dot_at_start_graph = Dot().move_to(graph.points[0])
dot_at_end_graph = Dot().move_to(graph.points[-1])
self.add(graph, dot_at_end_graph, dot_at_start_graph)
- self.play(self.camera_frame.animate.scale(0.5), self.camera_frame.animate.move_to(dot_at_start_graph))
+ self.play(self.camera_frame.animate.scale(0.5).move_to(dot_at_start_graph))
self.play(self.camera_frame.animate.move_to(dot_at_end_graph))
self.play(Restore(self.camera_frame))
self.wait()
diff --git a/manim/scene/scene.py b/manim/scene/scene.py
index 3a0217448..010b67db0 100644
--- a/manim/scene/scene.py
+++ b/manim/scene/scene.py
@@ -601,32 +601,39 @@ class Scene(Container):
)
return all_moving_mobject_families, static_mobjects
- def compile_animations(self, *animations, **play_kwargs):
+ def compile_animations(self, *args, **kwargs):
"""
Creates _MethodAnimations from any _AnimationBuilders and updates animation
kwargs with kwargs passed to play().
-
Parameters
----------
*animations : Tuple[:class:`Animation`]
Animations to be played.
-
**play_kwargs
Configuration for the call to play().
-
Returns
-------
Tuple[:class:`Animation`]
Animations to be played.
"""
- for animation in animations:
- if inspect.ismethod(animation):
+ animations = []
+ for arg in args:
+ if isinstance(arg, _AnimationBuilder):
+ animations.append(arg.build())
+ elif isinstance(arg, Animation):
+ animations.append(arg)
+ elif inspect.ismethod(arg):
raise TypeError(
- "Passing mobject methods to Scene.play is no longer supported. Use "
+ "Passing Mobject methods to Scene.play is no longer supported. Use "
"Mobject.animate instead."
)
- for k, v in play_kwargs.items():
+ else:
+ raise TypeError(f"Unexpected argument {arg} passed to Scene.play().")
+
+ for animation in animations:
+ for k, v in kwargs.items():
setattr(animation, k, v)
+
return animations
def _get_animation_time_progression(self, animations, duration):
diff --git a/tests/control_data/graphical_units_data/transform/AnimationBuilderTest.npz b/tests/control_data/graphical_units_data/transform/AnimationBuilderTest.npz
new file mode 100644
index 000000000..04880d7cc
Binary files /dev/null and b/tests/control_data/graphical_units_data/transform/AnimationBuilderTest.npz differ
diff --git a/tests/test_graphical_units/test_transform.py b/tests/test_graphical_units/test_transform.py
index 89ef46baa..2e058d975 100644
--- a/tests/test_graphical_units/test_transform.py
+++ b/tests/test_graphical_units/test_transform.py
@@ -131,6 +131,11 @@ class FadeInAndOutTest(Scene):
self.play(FadeOut(square))
+class AnimationBuilderTest(Scene):
+ def construct(self):
+ self.play(Square().animate.shift(RIGHT).rotate(PI / 4))
+
+
MODULE_NAME = "transform"