Skip to content

Commit 0843632

Browse files
feat: set_default for Animation class (#3876)
* feat: Animation.set_default Adds set_default class method to Animation class based on existing implementation for Mobject class. Addresses issue #3142 * test: added missing test * fix: docstring formatting --------- Co-authored-by: Benjamin Hackl <[email protected]>
1 parent c8fe4d9 commit 0843632

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

manim/animation/animation.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from collections.abc import Iterable, Sequence
1818
from copy import deepcopy
19+
from functools import partialmethod
1920
from typing import TYPE_CHECKING, Callable
2021

2122
from typing_extensions import Self
@@ -479,6 +480,52 @@ def is_introducer(self) -> bool:
479480
"""
480481
return self.introducer
481482

483+
@classmethod
484+
def __init_subclass__(cls, **kwargs) -> None:
485+
super().__init_subclass__(**kwargs)
486+
487+
cls._original__init__ = cls.__init__
488+
489+
@classmethod
490+
def set_default(cls, **kwargs) -> None:
491+
"""Sets the default values of keyword arguments.
492+
493+
If this method is called without any additional keyword
494+
arguments, the original default values of the initialization
495+
method of this class are restored.
496+
497+
Parameters
498+
----------
499+
500+
kwargs
501+
Passing any keyword argument will update the default
502+
values of the keyword arguments of the initialization
503+
function of this class.
504+
505+
Examples
506+
--------
507+
508+
.. manim:: ChangeDefaultAnimation
509+
510+
class ChangeDefaultAnimation(Scene):
511+
def construct(self):
512+
Rotate.set_default(run_time=2, rate_func=rate_functions.linear)
513+
Indicate.set_default(color=None)
514+
515+
S = Square(color=BLUE, fill_color=BLUE, fill_opacity=0.25)
516+
self.add(S)
517+
self.play(Rotate(S, PI))
518+
self.play(Indicate(S))
519+
520+
Rotate.set_default()
521+
Indicate.set_default()
522+
523+
"""
524+
if kwargs:
525+
cls.__init__ = partialmethod(cls.__init__, **kwargs)
526+
else:
527+
cls.__init__ = cls._original__init__
528+
482529

483530
def prepare_animation(
484531
anim: Animation | mobject._AnimationBuilder,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from manim import *
2+
from manim.animation.animation import DEFAULT_ANIMATION_RUN_TIME
3+
4+
__module_test__ = "animation"
5+
6+
7+
def test_animation_set_default():
8+
s = Square()
9+
Rotate.set_default(run_time=100)
10+
anim = Rotate(s)
11+
assert anim.run_time == 100
12+
anim = Rotate(s, run_time=5)
13+
assert anim.run_time == 5
14+
Rotate.set_default()
15+
anim = Rotate(s)
16+
assert anim.run_time == DEFAULT_ANIMATION_RUN_TIME

0 commit comments

Comments
 (0)