From c61b74b178a1cf7eb36301d48eae991ba7b09520 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Fri, 24 Sep 2021 18:52:59 +0200 Subject: [PATCH 1/8] implement Mobject.change_default --- manim/mobject/mobject.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index c949793eb2..6b0794252d 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 ( @@ -179,6 +179,40 @@ def add_animation_override( f"{cls.animation_overrides[animation_class].__qualname__} and " f"{override_func.__qualname__}.", ) + + @classmethod + def change_default(cls, **kwargs): + """Changes the default values of keyword arguments. + + Parameters + ---------- + + kwargs + Passing any keyword argument will update the default + values of the keyword arguments of the initialization + function of this class. + + Examples + -------- + + :: + + >>> Square.change_default(color=GREEN, fill_opacity=0.25) + >>> s = Square(); s.color, s.fill_opacity + (, 0.25) + + .. manim:: ChangedDefaultTextcolor + :save_last_frame: + + config.background_color = WHITE + + class ChangedDefaultTextcolor(Scene): + def construct(self): + Text.change_default(color=BLACK) + self.add(Text("Changing default values is easy!")) + + """ + cls.__init__ = partialmethod(cls.__init__, **kwargs) @property def animate(self): From e3fbcda064deb11f730abbcb1440e40e8a8a13eb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Sep 2021 17:00:50 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/mobject/mobject.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index 6b0794252d..72dbc545cf 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -179,7 +179,7 @@ def add_animation_override( f"{cls.animation_overrides[animation_class].__qualname__} and " f"{override_func.__qualname__}.", ) - + @classmethod def change_default(cls, **kwargs): """Changes the default values of keyword arguments. @@ -191,7 +191,7 @@ def change_default(cls, **kwargs): Passing any keyword argument will update the default values of the keyword arguments of the initialization function of this class. - + Examples -------- From 43fe744258b952112d516d1093ecf365436650eb Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Fri, 24 Sep 2021 19:13:14 +0200 Subject: [PATCH 3/8] fix doctest --- manim/mobject/mobject.py | 1 + 1 file changed, 1 insertion(+) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index 72dbc545cf..2bec6450b6 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -197,6 +197,7 @@ def change_default(cls, **kwargs): :: + >>> from manim import Square >>> Square.change_default(color=GREEN, fill_opacity=0.25) >>> s = Square(); s.color, s.fill_opacity (, 0.25) From 7a2f0ff18a208df92c1527291ac510c707169056 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Fri, 24 Sep 2021 19:25:03 +0200 Subject: [PATCH 4/8] =?UTF-8?q?actually=20fix=20doctest=20=F0=9F=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manim/mobject/mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index 2bec6450b6..afeed5af84 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -197,7 +197,7 @@ def change_default(cls, **kwargs): :: - >>> from manim import Square + >>> from manim import Square, GREEN >>> Square.change_default(color=GREEN, fill_opacity=0.25) >>> s = Square(); s.color, s.fill_opacity (, 0.25) From 6839b11131693dc643ade549a97949660b7e2e2f Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Fri, 24 Sep 2021 20:17:38 +0200 Subject: [PATCH 5/8] Update manim/mobject/mobject.py --- manim/mobject/mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index afeed5af84..0e94414330 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -200,7 +200,7 @@ def change_default(cls, **kwargs): >>> from manim import Square, GREEN >>> Square.change_default(color=GREEN, fill_opacity=0.25) >>> s = Square(); s.color, s.fill_opacity - (, 0.25) + (, 0.25) .. manim:: ChangedDefaultTextcolor :save_last_frame: From b34766def602053940369eb84df5f1ed549c6a05 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Sat, 25 Sep 2021 14:20:42 +0200 Subject: [PATCH 6/8] allow restoring original default arguments --- manim/mobject/mobject.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index afeed5af84..5e122caad3 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -94,6 +94,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 @@ -184,6 +185,10 @@ def add_animation_override( def change_default(cls, **kwargs): """Changes 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 ---------- @@ -201,6 +206,9 @@ def change_default(cls, **kwargs): >>> Square.change_default(color=GREEN, fill_opacity=0.25) >>> s = Square(); s.color, s.fill_opacity (, 0.25) + >>> Square.change_default() + >>> s = Square(); s.color, s.fill_opacity + (, 0.0) .. manim:: ChangedDefaultTextcolor :save_last_frame: @@ -213,7 +221,10 @@ def construct(self): self.add(Text("Changing default values is easy!")) """ - cls.__init__ = partialmethod(cls.__init__, **kwargs) + if kwargs: + cls.__init__ = partialmethod(cls.__init__, **kwargs) + else: + cls.__init__ = cls._original__init__ @property def animate(self): From e638eaac0aacf953b99122ac1d3b7280f4b23ff2 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Sat, 25 Sep 2021 14:24:11 +0200 Subject: [PATCH 7/8] added change_default implementation to OpenGLMobject --- manim/mobject/opengl_mobject.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/manim/mobject/opengl_mobject.py b/manim/mobject/opengl_mobject.py index 30f59d9e59..d6eded7434 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 change_default(cls, **kwargs): + if kwargs: + cls.__init__ = partialmethod(cls.__init__, **kwargs) + else: + cls.__init__ = cls._original__init__ + def __str__(self): return self.__class__.__name__ From dce47446a54c7f7723d97a68e3eac1abbe4460a7 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Sun, 26 Sep 2021 01:58:34 +0200 Subject: [PATCH 8/8] change_default -> set_default --- manim/mobject/mobject.py | 10 +++++----- manim/mobject/opengl_mobject.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index 6392504f2a..5af908ed82 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -182,8 +182,8 @@ def add_animation_override( ) @classmethod - def change_default(cls, **kwargs): - """Changes the default values of keyword arguments. + 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 @@ -203,10 +203,10 @@ def change_default(cls, **kwargs): :: >>> from manim import Square, GREEN - >>> Square.change_default(color=GREEN, fill_opacity=0.25) + >>> Square.set_default(color=GREEN, fill_opacity=0.25) >>> s = Square(); s.color, s.fill_opacity (, 0.25) - >>> Square.change_default() + >>> Square.set_default() >>> s = Square(); s.color, s.fill_opacity (, 0.0) @@ -217,7 +217,7 @@ def change_default(cls, **kwargs): class ChangedDefaultTextcolor(Scene): def construct(self): - Text.change_default(color=BLACK) + Text.set_default(color=BLACK) self.add(Text("Changing default values is easy!")) """ diff --git a/manim/mobject/opengl_mobject.py b/manim/mobject/opengl_mobject.py index d6eded7434..65517a2c15 100644 --- a/manim/mobject/opengl_mobject.py +++ b/manim/mobject/opengl_mobject.py @@ -127,7 +127,7 @@ def __init_subclass__(cls, **kwargs): cls._original__init__ = cls.__init__ @classmethod - def change_default(cls, **kwargs): + def set_default(cls, **kwargs): if kwargs: cls.__init__ = partialmethod(cls.__init__, **kwargs) else: