Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix Bug `Uncreate()` with `rate_func` via introducing new parameter `reversed` to class `Animation` (#2597)

darkways5071e9418b40
diff --git a/manim/animation/animation.py b/manim/animation/animation.py
index 2805e0402..385917e16 100644
--- a/manim/animation/animation.py
+++ b/manim/animation/animation.py
@@ -49,6 +49,13 @@ class Animation:
For example ``rate_func(0.5)`` is the proportion of the animation that is done
after half of the animations run time.
+
+
+ reverse_rate_function
+ Reverses the rate function of the animation. Setting ``reverse_rate_function``
+ does not have any effect on ``remover`` or ``introducer``. These need to be
+ set explicitly if an introducer-animation should be turned into a remover one
+ and vice versa.
name
The name of the animation. This gets displayed while rendering the animation.
Defaults to <class-name>(<Mobject-name>).
@@ -123,6 +130,7 @@ class Animation:
lag_ratio: float = DEFAULT_ANIMATION_LAG_RATIO,
run_time: float = DEFAULT_ANIMATION_RUN_TIME,
rate_func: Callable[[float], float] = smooth,
+ reverse_rate_function: bool = False,
name: str = None,
remover: bool = False, # remove a mobject from the screen?
suspend_mobject_updating: bool = True,
@@ -134,6 +142,7 @@ class Animation:
self._typecheck_input(mobject)
self.run_time: float = run_time
self.rate_func: Callable[[float], float] = rate_func
+ self.reverse_rate_function: bool = reverse_rate_function
self.name: str | None = name
self.remover: bool = remover
self.introducer: bool = introducer
@@ -361,7 +370,10 @@ class Animation:
full_length = (num_submobjects - 1) * lag_ratio + 1
value = alpha * full_length
lower = index * lag_ratio
- return self.rate_func(value - lower)
+ if self.reverse_rate_function:
+ return self.rate_func(1 - (value - lower))
+ else:
+ return self.rate_func(value - lower)
# Getters and setters
def set_run_time(self, run_time: float) -> Animation:
diff --git a/manim/animation/creation.py b/manim/animation/creation.py
index 1b2eddda7..8abc26568 100644
--- a/manim/animation/creation.py
+++ b/manim/animation/creation.py
@@ -191,13 +191,13 @@ class Uncreate(Create):
def __init__(
self,
mobject: VMobject | OpenGLVMobject,
- rate_func: Callable[[float], float] = lambda t: smooth(1 - t),
+ reverse_rate_function: bool = True,
remover: bool = True,
**kwargs,
) -> None:
super().__init__(
mobject,
- rate_func=rate_func,
+ reverse_rate_function=reverse_rate_function,
introducer=False,
remover=remover,
**kwargs,
@@ -405,7 +405,7 @@ class Unwrite(Write):
vmobject,
run_time=run_time,
lag_ratio=lag_ratio,
- rate_func=lambda t: -rate_func(t) + 1,
+ reverse_rate_function=True,
reverse=reverse,
**kwargs,
)
diff --git a/tests/test_graphical_units/control_data/creation/uncreate_rate_func.npz b/tests/test_graphical_units/control_data/creation/uncreate_rate_func.npz
new file mode 100644
index 000000000..3826c14fa
Binary files /dev/null and b/tests/test_graphical_units/control_data/creation/uncreate_rate_func.npz differ
diff --git a/tests/test_graphical_units/test_creation.py b/tests/test_graphical_units/test_creation.py
index c6ce35ba1..c6cfc3e50 100644
--- a/tests/test_graphical_units/test_creation.py
+++ b/tests/test_graphical_units/test_creation.py
@@ -19,6 +19,13 @@ def test_uncreate(scene):
scene.play(Uncreate(square))
+@frames_comparison(last_frame=False)
+def test_uncreate_rate_func(scene):
+ square = Square()
+ scene.add(square)
+ scene.play(Uncreate(square), rate_func=linear)
+
+
@frames_comparison(last_frame=False)
def test_DrawBorderThenFill(scene):
square = Square(fill_opacity=1)