diff --git a/manim/animation/animation.py b/manim/animation/animation.py index 042054b7ba..8bdf236b3f 100644 --- a/manim/animation/animation.py +++ b/manim/animation/animation.py @@ -16,6 +16,7 @@ from collections.abc import Iterable, Sequence from copy import deepcopy +from functools import partialmethod from typing import TYPE_CHECKING, Callable from typing_extensions import Self @@ -479,6 +480,52 @@ def is_introducer(self) -> bool: """ return self.introducer + @classmethod + def __init_subclass__(cls, **kwargs) -> None: + super().__init_subclass__(**kwargs) + + cls._original__init__ = cls.__init__ + + @classmethod + def set_default(cls, **kwargs) -> None: + """Sets the default values of keyword arguments. + + If this method is called without any additional keyword + arguments, the original default values of the initialization + method of this class are restored. + + Parameters + ---------- + + kwargs + Passing any keyword argument will update the default + values of the keyword arguments of the initialization + function of this class. + + Examples + -------- + + .. manim:: ChangeDefaultAnimation + + class ChangeDefaultAnimation(Scene): + def construct(self): + Rotate.set_default(run_time=2, rate_func=rate_functions.linear) + Indicate.set_default(color=None) + + S = Square(color=BLUE, fill_color=BLUE, fill_opacity=0.25) + self.add(S) + self.play(Rotate(S, PI)) + self.play(Indicate(S)) + + Rotate.set_default() + Indicate.set_default() + + """ + if kwargs: + cls.__init__ = partialmethod(cls.__init__, **kwargs) + else: + cls.__init__ = cls._original__init__ + def prepare_animation( anim: Animation | mobject._AnimationBuilder, diff --git a/tests/test_graphical_units/test_animation.py b/tests/test_graphical_units/test_animation.py new file mode 100644 index 0000000000..3758e4985b --- /dev/null +++ b/tests/test_graphical_units/test_animation.py @@ -0,0 +1,16 @@ +from manim import * +from manim.animation.animation import DEFAULT_ANIMATION_RUN_TIME + +__module_test__ = "animation" + + +def test_animation_set_default(): + s = Square() + Rotate.set_default(run_time=100) + anim = Rotate(s) + assert anim.run_time == 100 + anim = Rotate(s, run_time=5) + assert anim.run_time == 5 + Rotate.set_default() + anim = Rotate(s) + assert anim.run_time == DEFAULT_ANIMATION_RUN_TIME