Skip to content

Commit

Permalink
feat: set_default for Animation class (ManimCommunity#3876)
Browse files Browse the repository at this point in the history
* feat: Animation.set_default

Adds set_default class method to Animation class based on existing implementation for Mobject class.

Addresses issue ManimCommunity#3142

* test: added missing test

* fix: docstring formatting

---------

Co-authored-by: Benjamin Hackl <[email protected]>
  • Loading branch information
christopher-hampson and behackl authored Jul 22, 2024
1 parent c8fe4d9 commit 0843632
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
47 changes: 47 additions & 0 deletions manim/animation/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_graphical_units/test_animation.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0843632

Please sign in to comment.