Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

New Feature: Allow passing animation arguments with .animate syntax (#1118)

friedkeenan903253b243ca
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index da3b0d67f..b9ac7a0c5 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -136,6 +136,19 @@ class Mobject(Container):
self.play(my_mobject.animate.shift(RIGHT).rotate(PI))
+ Keyword arguments that can be passed to :meth:`.Scene.play` can be passed
+ directly after accessing ``.animate``, like so::
+
+ self.play(my_mobject.animate(rate_func=linear).shift(RIGHT))
+
+ This is especially useful when animating simultaneous ``.animate`` calls that
+ you want to behave differently::
+
+ self.play(
+ mobject1.animate(run_time=2).rotate(PI),
+ mobject2.animate(rate_func=there_and_back).shift(RIGHT),
+ )
+
.. seealso::
:func:`override_animate`
@@ -165,6 +178,21 @@ class Mobject(Container):
self.play(s.animate.shift(RIGHT).scale(2).rotate(PI / 2))
self.play(Uncreate(s))
+ .. manim:: AnimateWithArgsExample
+
+ class AnimateWithArgsExample(Scene):
+ def construct(self):
+ s = Square()
+ c = Circle()
+
+ VGroup(s, c).arrange(RIGHT, buff=2)
+ self.add(s, c)
+
+ self.play(
+ s.animate(run_time=2).rotate(PI / 2),
+ c.animate(rate_func=there_and_back).shift(RIGHT),
+ )
+
"""
return _AnimationBuilder(self)
@@ -2100,11 +2128,27 @@ class Group(Mobject):
class _AnimationBuilder:
def __init__(self, mobject):
self.mobject = mobject
- self.overridden_animation = None
self.mobject.generate_target()
+
+ self.overridden_animation = None
self.is_chaining = False
self.methods = []
+ # Whether animation args can be passed
+ self.cannot_pass_args = False
+ self.anim_args = {}
+
+ def __call__(self, **kwargs):
+ if self.cannot_pass_args:
+ raise ValueError(
+ "Animation arguments must be passed before accessing methods and can only be passed once"
+ )
+
+ self.anim_args = kwargs
+ self.cannot_pass_args = True
+
+ return self
+
def __getattr__(self, method_name):
method = getattr(self.mobject.target, method_name)
self.methods.append(method)
@@ -2126,15 +2170,22 @@ class _AnimationBuilder:
return self
self.is_chaining = True
+ self.cannot_pass_args = True
+
return update_target
def build(self):
from ..animation.transform import _MethodAnimation
if self.overridden_animation:
- return self.overridden_animation
+ anim = self.overridden_animation
+ else:
+ anim = _MethodAnimation(self.mobject, self.methods)
+
+ for attr, value in self.anim_args.items():
+ setattr(anim, attr, value)
- return _MethodAnimation(self.mobject, self.methods)
+ return anim
def override_animate(method):
diff --git a/tests/test_animate.py b/tests/test_animate.py
index 31d1d675c..a2bea0e38 100644
--- a/tests/test_animate.py
+++ b/tests/test_animate.py
@@ -74,3 +74,39 @@ def test_chaining_overridden_animate():
NotImplementedError, match="not supported for overridden animations"
):
DotsWithLine().animate.remove_line().shift((1, 0, 0))
+
+
+def test_animate_with_args():
+ s = Square()
+ scale_factor = 2
+ run_time = 2
+
+ anim = s.animate(run_time=run_time).scale(scale_factor).build()
+ assert anim.mobject.target.width == scale_factor * s.width
+ assert anim.run_time == run_time
+
+
+def test_chained_animate_with_args():
+ s = Square()
+ scale_factor = 2
+ direction = np.array((1, 1, 0))
+ run_time = 2
+
+ anim = s.animate(run_time=run_time).scale(scale_factor).shift(direction).build()
+ assert (
+ anim.mobject.target.width == scale_factor * s.width
+ and (anim.mobject.target.get_center() == direction).all()
+ )
+ assert anim.run_time == run_time
+
+
+def test_animate_with_args_misplaced():
+ s = Square()
+ scale_factor = 2
+ run_time = 2
+
+ with pytest.raises(ValueError, match="must be passed before"):
+ s.animate.scale(scale_factor)(run_time=run_time)
+
+ with pytest.raises(ValueError, match="must be passed before"):
+ s.animate(run_time=run_time)(run_time=run_time).scale(scale_factor)