diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index f76655362c..f9fac8a115 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -11,7 +11,7 @@ import sys import types import warnings -from functools import reduce +from functools import partialmethod, reduce from math import ceil from pathlib import Path from typing import ( @@ -89,6 +89,7 @@ def __init_subclass__(cls, **kwargs): Callable[["Mobject"], "Animation"], ] = {} cls._add_intrinsic_animation_overrides() + cls._original__init__ = cls.__init__ def __init__(self, color=WHITE, name=None, dim=3, target=None, z_index=0): self.color = Color(color) if color else None @@ -175,6 +176,51 @@ def add_animation_override( f"{override_func.__qualname__}.", ) + @classmethod + def set_default(cls, **kwargs): + """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 + -------- + + :: + + >>> from manim import Square, GREEN + >>> Square.set_default(color=GREEN, fill_opacity=0.25) + >>> s = Square(); s.color, s.fill_opacity + (, 0.25) + >>> Square.set_default() + >>> s = Square(); s.color, s.fill_opacity + (, 0.0) + + .. manim:: ChangedDefaultTextcolor + :save_last_frame: + + config.background_color = WHITE + + class ChangedDefaultTextcolor(Scene): + def construct(self): + Text.set_default(color=BLACK) + self.add(Text("Changing default values is easy!")) + + """ + if kwargs: + cls.__init__ = partialmethod(cls.__init__, **kwargs) + else: + cls.__init__ = cls._original__init__ + @property def animate(self): """Used to animate the application of a method. diff --git a/manim/mobject/opengl_mobject.py b/manim/mobject/opengl_mobject.py index e981e051e2..bc29d78a9d 100644 --- a/manim/mobject/opengl_mobject.py +++ b/manim/mobject/opengl_mobject.py @@ -2,7 +2,7 @@ import itertools as it import random import sys -from functools import wraps +from functools import partialmethod, wraps from math import ceil from typing import Iterable, Optional, Tuple, Union @@ -121,6 +121,18 @@ def __init__( if self.depth_test: self.apply_depth_test() + @classmethod + def __init_subclass__(cls, **kwargs): + super().__init_subclass__(**kwargs) + cls._original__init__ = cls.__init__ + + @classmethod + def set_default(cls, **kwargs): + if kwargs: + cls.__init__ = partialmethod(cls.__init__, **kwargs) + else: + cls.__init__ = cls._original__init__ + def __str__(self): return self.__class__.__name__