From cb7dce0f3c809ea46294fe8f6ccd09c819ddf901 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Thu, 10 Aug 2023 12:21:43 -0400 Subject: [PATCH 01/13] Added docs for functions in mobject_update_utils --- .../updaters/mobject_update_utils.py | 82 +++++++++++++++++-- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index 698dc03d56..9aa7958071 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -24,13 +24,13 @@ from manim.opengl import OpenGLMobject -def assert_is_mobject_method(method): +def assert_is_mobject_method(method: Callable) -> None: assert inspect.ismethod(method) mobject = method.__self__ assert isinstance(mobject, (Mobject, OpenGLMobject)) -def always(method, *args, **kwargs): +def always(method: Callable, *args, **kwargs) -> Mobject: assert_is_mobject_method(method) mobject = method.__self__ func = method.__func__ @@ -38,7 +38,7 @@ def always(method, *args, **kwargs): return mobject -def f_always(method, *arg_generators, **kwargs): +def f_always(method: Callable[[Mobject], None], *arg_generators, **kwargs) -> Mobject: """ More functional version of always, where instead of taking in args, it takes in functions which output @@ -99,7 +99,36 @@ def construct(self): return mob -def always_shift(mobject, direction=RIGHT, rate=0.1): +def always_shift(mobject: Mobject, direction: np.ndarray[np.float64] = RIGHT, rate: float = 0.1) -> Mobject: + """Shift the mobject by `rate*direction` every second + + Parameters + ---------- + mobject + The mobject to shift + direction + The direction to shift + rate + Amount of direction to be covered in one second + + Examples + -------- + + .. manim:: ShiftingSquare + + class ShiftingSquare(Scene): + def construct(self): + sq = Square().set_fill(opacity=1) + tri = Triangle() + VGroup(sq, tri).arrange(LEFT) + + # always shift square one unit to the right + # even if there is an animation going on + always_shift(sq, RIGHT, rate=5) + + self.add(sq) + self.play(tri.animate.set_fill(opacity=1)) + """ def normalize(v): norm = np.linalg.norm(v) if norm == 0: @@ -110,18 +139,57 @@ def normalize(v): return mobject -def always_rotate(mobject, rate=20 * DEGREES, **kwargs): +def always_rotate(mobject: Mobject, rate: float = 20 * DEGREES, **kwargs) -> Mobject: + """Rotate the mobject by `rate` every second + + Parameters + ---------- + mobject + The mobject to rotate + + Examples + -------- + + .. manim:: SpinningTriangle + + class SpinningTriangle(Scene): + def construct(self): + tri = Triangle().set_fill(opacity=1).set_z_index(2) + sq = Square().to_edge(LEFT) + + # will keep spinning while there is an animation going on + always_rotate(tri, rate=2*PI, about_point=ORIGIN) + + self.add(tri, sq) + self.play(sq.animate.to_edge(RIGHT), rate_func=linear, run_time=1) + """ mobject.add_updater(lambda m, dt: m.rotate(dt * rate, **kwargs)) return mobject -def turn_animation_into_updater(animation, cycle=False, **kwargs): +def turn_animation_into_updater(animation, cycle=False, **kwargs) -> Mobject: """ Add an updater to the animation's mobject which applies the interpolation and update functions of the animation If cycle is True, this repeats over and over. Otherwise, the updater will be popped upon completion + + Examples + -------- + + .. manim:: WelcomeToManim + + class WelcomeToManim(Scene): + def construct(self): + words = Text("Welcome to") + banner = ManimBanner().scale(0.5) + VGroup(words, banner).arrange(DOWN) + + turn_animation_into_updater(Write(words, run_time=0.9)) + self.add(words) + self.wait(0.5) + self.play(banner.expand(), run_time=0.5) """ mobject = animation.mobject animation.suspend_mobject_updating = False @@ -147,5 +215,5 @@ def update(m, dt): return mobject -def cycle_animation(animation, **kwargs): +def cycle_animation(animation, **kwargs) -> Mobject: return turn_animation_into_updater(animation, cycle=True, **kwargs) From 88842b091a0a0b26cbcecb33294918396b2bf899 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Aug 2023 16:23:52 +0000 Subject: [PATCH 02/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/animation/updaters/mobject_update_utils.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index 9aa7958071..ba9454576d 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -99,7 +99,9 @@ def construct(self): return mob -def always_shift(mobject: Mobject, direction: np.ndarray[np.float64] = RIGHT, rate: float = 0.1) -> Mobject: +def always_shift( + mobject: Mobject, direction: np.ndarray[np.float64] = RIGHT, rate: float = 0.1 +) -> Mobject: """Shift the mobject by `rate*direction` every second Parameters @@ -125,10 +127,11 @@ def construct(self): # always shift square one unit to the right # even if there is an animation going on always_shift(sq, RIGHT, rate=5) - + self.add(sq) self.play(tri.animate.set_fill(opacity=1)) """ + def normalize(v): norm = np.linalg.norm(v) if norm == 0: @@ -174,10 +177,10 @@ def turn_animation_into_updater(animation, cycle=False, **kwargs) -> Mobject: If cycle is True, this repeats over and over. Otherwise, the updater will be popped upon completion - + Examples -------- - + .. manim:: WelcomeToManim class WelcomeToManim(Scene): From f070fa2c571d3f26d54ce5a2346e59481005aa99 Mon Sep 17 00:00:00 2001 From: Jason Grace <110117391+JasonGrace2282@users.noreply.github.com> Date: Sat, 12 Aug 2023 10:07:05 -0400 Subject: [PATCH 03/13] Updated docstring of always_shift Co-authored-by: Benjamin Hackl --- manim/animation/updaters/mobject_update_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index ba9454576d..c12234cf6a 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -109,7 +109,8 @@ def always_shift( mobject The mobject to shift direction - The direction to shift + The direction to shift. The vector is normalized, the specified magnitude + is not relevant. rate Amount of direction to be covered in one second From 66a54cbc11616ac84c5a77b4b59b6e2749a7eb4d Mon Sep 17 00:00:00 2001 From: Jason Grace <110117391+JasonGrace2282@users.noreply.github.com> Date: Sat, 12 Aug 2023 10:07:19 -0400 Subject: [PATCH 04/13] Added period to sentence. Co-authored-by: Benjamin Hackl --- manim/animation/updaters/mobject_update_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index c12234cf6a..fdbc02b1e8 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -107,7 +107,7 @@ def always_shift( Parameters ---------- mobject - The mobject to shift + The mobject to shift. direction The direction to shift. The vector is normalized, the specified magnitude is not relevant. From dd3e88513ece2fa4f2da0bc8781790d6089016a4 Mon Sep 17 00:00:00 2001 From: Jason Grace <110117391+JasonGrace2282@users.noreply.github.com> Date: Sat, 12 Aug 2023 10:07:44 -0400 Subject: [PATCH 05/13] Updated parameter description in always_redraw Co-authored-by: Benjamin Hackl --- manim/animation/updaters/mobject_update_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index fdbc02b1e8..2191fb51f2 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -112,7 +112,8 @@ def always_shift( The direction to shift. The vector is normalized, the specified magnitude is not relevant. rate - Amount of direction to be covered in one second + Length in Manim units which the mobject travels in one + second along the specified direction. Examples -------- From 4f4a7d4b7b4747a58f339754c76b83ab2008da8e Mon Sep 17 00:00:00 2001 From: Jason Grace <110117391+JasonGrace2282@users.noreply.github.com> Date: Sat, 12 Aug 2023 10:08:04 -0400 Subject: [PATCH 06/13] Update always_rotate description Co-authored-by: Benjamin Hackl --- manim/animation/updaters/mobject_update_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index 2191fb51f2..a4ed848d98 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -145,7 +145,7 @@ def normalize(v): def always_rotate(mobject: Mobject, rate: float = 20 * DEGREES, **kwargs) -> Mobject: - """Rotate the mobject by `rate` every second + """A mobject which is continuously rotated at a certain rate. Parameters ---------- From de9a791f625ac778d27bc0c1be389190c7c16607 Mon Sep 17 00:00:00 2001 From: Jason Grace <110117391+JasonGrace2282@users.noreply.github.com> Date: Sat, 12 Aug 2023 10:08:30 -0400 Subject: [PATCH 07/13] Finished parameters in always_redraw Co-authored-by: Benjamin Hackl --- manim/animation/updaters/mobject_update_utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index a4ed848d98..1330fd60ad 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -150,7 +150,12 @@ def always_rotate(mobject: Mobject, rate: float = 20 * DEGREES, **kwargs) -> Mob Parameters ---------- mobject - The mobject to rotate + The mobject to be rotated. + rate + The angle which the mobject is rotated by + over one second. + kwags + Further arguments to be passed to :meth:`.Mobject.rotate`. Examples -------- From 34ff78c8ec3caa6a7df6f80859413ba3a1857c28 Mon Sep 17 00:00:00 2001 From: Jason Grace <110117391+JasonGrace2282@users.noreply.github.com> Date: Sat, 12 Aug 2023 10:08:50 -0400 Subject: [PATCH 08/13] Changed comment in always_shift Co-authored-by: Benjamin Hackl --- manim/animation/updaters/mobject_update_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index 1330fd60ad..d489ab857b 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -126,8 +126,8 @@ def construct(self): tri = Triangle() VGroup(sq, tri).arrange(LEFT) - # always shift square one unit to the right - # even if there is an animation going on + # construct a square which is continuously + # shifted to the right always_shift(sq, RIGHT, rate=5) self.add(sq) From 28b854d0e2146fe7dc0c0ce4d06a635b848b3573 Mon Sep 17 00:00:00 2001 From: Jason Grace <110117391+JasonGrace2282@users.noreply.github.com> Date: Sat, 12 Aug 2023 10:09:05 -0400 Subject: [PATCH 09/13] update always_shift description Co-authored-by: Benjamin Hackl --- manim/animation/updaters/mobject_update_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index d489ab857b..4d5978545d 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -102,7 +102,8 @@ def construct(self): def always_shift( mobject: Mobject, direction: np.ndarray[np.float64] = RIGHT, rate: float = 0.1 ) -> Mobject: - """Shift the mobject by `rate*direction` every second + """A mobject which is continuously shifted along some direction + at a certain rate. Parameters ---------- From 948509fd8cd9a4831a340d0a7ddebaf10478fd39 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Sat, 12 Aug 2023 10:24:03 -0400 Subject: [PATCH 10/13] used normalize from manim.utils.space_ops --- manim/animation/updaters/mobject_update_utils.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index 4d5978545d..4578ee23df 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -22,6 +22,7 @@ from manim.constants import DEGREES, RIGHT from manim.mobject.mobject import Mobject from manim.opengl import OpenGLMobject +from manim.utils.space_ops import normalize def assert_is_mobject_method(method: Callable) -> None: @@ -134,13 +135,6 @@ def construct(self): self.add(sq) self.play(tri.animate.set_fill(opacity=1)) """ - - def normalize(v): - norm = np.linalg.norm(v) - if norm == 0: - return v - return v / norm - mobject.add_updater(lambda m, dt: m.shift(dt * rate * normalize(direction))) return mobject From b06ab573b6f1389858c0355d9e304316804a8811 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Sat, 12 Aug 2023 11:36:09 -0400 Subject: [PATCH 11/13] fixed indentation in always_redraw --- manim/animation/updaters/mobject_update_utils.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index 4578ee23df..6e1aebf3c7 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -81,16 +81,18 @@ def construct(self): sine = ax.plot(np.sin, color=RED) alpha = ValueTracker(0) point = always_redraw( - lambda: Dot( - sine.point_from_proportion(alpha.get_value()), - color=BLUE) + lambda: Dot( + sine.point_from_proportion(alpha.get_value()), + color=BLUE ) + ) tangent = always_redraw( lambda: TangentLine( sine, alpha=alpha.get_value(), color=YELLOW, - length=4) + length=4 + ) ) self.add(ax, sine, point, tangent) self.play(alpha.animate.set_value(1), rate_func=linear, run_time=2) From 0574172130c9d9dc015e2f8b6d74f863da70f4ba Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Sat, 12 Aug 2023 11:42:41 -0400 Subject: [PATCH 12/13] added type-hints --- manim/animation/updaters/mobject_update_utils.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index 6e1aebf3c7..18d9171806 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -15,7 +15,7 @@ import inspect -from collections.abc import Callable +from typing import Callable, TYPE_CHECKING import numpy as np @@ -24,6 +24,8 @@ from manim.opengl import OpenGLMobject from manim.utils.space_ops import normalize +if TYPE_CHECKING: + from manim.animation.animation import Animation def assert_is_mobject_method(method: Callable) -> None: assert inspect.ismethod(method) @@ -103,7 +105,9 @@ def construct(self): def always_shift( - mobject: Mobject, direction: np.ndarray[np.float64] = RIGHT, rate: float = 0.1 + mobject: Mobject, + direction: np.ndarray[np.float64] = RIGHT, + rate: float = 0.1 ) -> Mobject: """A mobject which is continuously shifted along some direction at a certain rate. @@ -174,7 +178,7 @@ def construct(self): return mobject -def turn_animation_into_updater(animation, cycle=False, **kwargs) -> Mobject: +def turn_animation_into_updater(animation: Animation, cycle: bool = False, **kwargs) -> Mobject: """ Add an updater to the animation's mobject which applies the interpolation and update functions of the animation @@ -203,7 +207,7 @@ def construct(self): animation.begin() animation.total_time = 0 - def update(m, dt): + def update(m: Mobject, dt: float): run_time = animation.get_run_time() time_ratio = animation.total_time / run_time if cycle: @@ -222,5 +226,5 @@ def update(m, dt): return mobject -def cycle_animation(animation, **kwargs) -> Mobject: +def cycle_animation(animation: Animation, **kwargs) -> Mobject: return turn_animation_into_updater(animation, cycle=True, **kwargs) From 0ca9a3e38f1020b5967d8d2ebed75ad7c6e05bfb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Aug 2023 15:43:23 +0000 Subject: [PATCH 13/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/animation/updaters/mobject_update_utils.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index 18d9171806..dee27ff398 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -15,7 +15,7 @@ import inspect -from typing import Callable, TYPE_CHECKING +from typing import TYPE_CHECKING, Callable import numpy as np @@ -27,6 +27,7 @@ if TYPE_CHECKING: from manim.animation.animation import Animation + def assert_is_mobject_method(method: Callable) -> None: assert inspect.ismethod(method) mobject = method.__self__ @@ -105,9 +106,7 @@ def construct(self): def always_shift( - mobject: Mobject, - direction: np.ndarray[np.float64] = RIGHT, - rate: float = 0.1 + mobject: Mobject, direction: np.ndarray[np.float64] = RIGHT, rate: float = 0.1 ) -> Mobject: """A mobject which is continuously shifted along some direction at a certain rate. @@ -178,7 +177,9 @@ def construct(self): return mobject -def turn_animation_into_updater(animation: Animation, cycle: bool = False, **kwargs) -> Mobject: +def turn_animation_into_updater( + animation: Animation, cycle: bool = False, **kwargs +) -> Mobject: """ Add an updater to the animation's mobject which applies the interpolation and update functions of the animation