Skip to content

feat: set_default for Animation class #3876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading