Skip to content

Commit c4b96ff

Browse files
behacklpre-commit-ci[bot]kolibril13
authored
Implemented :meth:.Mobject.set_default, a mechanism for changing default values of keyword arguments (#2075)
* implement Mobject.change_default * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix doctest * actually fix doctest 🙄 * Update manim/mobject/mobject.py * allow restoring original default arguments * added change_default implementation to OpenGLMobject * change_default -> set_default Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jan-Hendrik Müller <[email protected]>
1 parent 3f27209 commit c4b96ff

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

manim/mobject/mobject.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import sys
1212
import types
1313
import warnings
14-
from functools import reduce
14+
from functools import partialmethod, reduce
1515
from math import ceil
1616
from pathlib import Path
1717
from typing import (
@@ -89,6 +89,7 @@ def __init_subclass__(cls, **kwargs):
8989
Callable[["Mobject"], "Animation"],
9090
] = {}
9191
cls._add_intrinsic_animation_overrides()
92+
cls._original__init__ = cls.__init__
9293

9394
def __init__(self, color=WHITE, name=None, dim=3, target=None, z_index=0):
9495
self.color = Color(color) if color else None
@@ -175,6 +176,51 @@ def add_animation_override(
175176
f"{override_func.__qualname__}.",
176177
)
177178

179+
@classmethod
180+
def set_default(cls, **kwargs):
181+
"""Sets the default values of keyword arguments.
182+
183+
If this method is called without any additional keyword
184+
arguments, the original default values of the initialization
185+
method of this class are restored.
186+
187+
Parameters
188+
----------
189+
190+
kwargs
191+
Passing any keyword argument will update the default
192+
values of the keyword arguments of the initialization
193+
function of this class.
194+
195+
Examples
196+
--------
197+
198+
::
199+
200+
>>> from manim import Square, GREEN
201+
>>> Square.set_default(color=GREEN, fill_opacity=0.25)
202+
>>> s = Square(); s.color, s.fill_opacity
203+
(<Color #83c167>, 0.25)
204+
>>> Square.set_default()
205+
>>> s = Square(); s.color, s.fill_opacity
206+
(<Color white>, 0.0)
207+
208+
.. manim:: ChangedDefaultTextcolor
209+
:save_last_frame:
210+
211+
config.background_color = WHITE
212+
213+
class ChangedDefaultTextcolor(Scene):
214+
def construct(self):
215+
Text.set_default(color=BLACK)
216+
self.add(Text("Changing default values is easy!"))
217+
218+
"""
219+
if kwargs:
220+
cls.__init__ = partialmethod(cls.__init__, **kwargs)
221+
else:
222+
cls.__init__ = cls._original__init__
223+
178224
@property
179225
def animate(self):
180226
"""Used to animate the application of a method.

manim/mobject/opengl_mobject.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import itertools as it
33
import random
44
import sys
5-
from functools import wraps
5+
from functools import partialmethod, wraps
66
from math import ceil
77
from typing import Iterable, Optional, Tuple, Union
88

@@ -121,6 +121,18 @@ def __init__(
121121
if self.depth_test:
122122
self.apply_depth_test()
123123

124+
@classmethod
125+
def __init_subclass__(cls, **kwargs):
126+
super().__init_subclass__(**kwargs)
127+
cls._original__init__ = cls.__init__
128+
129+
@classmethod
130+
def set_default(cls, **kwargs):
131+
if kwargs:
132+
cls.__init__ = partialmethod(cls.__init__, **kwargs)
133+
else:
134+
cls.__init__ = cls._original__init__
135+
124136
def __str__(self):
125137
return self.__class__.__name__
126138

0 commit comments

Comments
 (0)