Skip to content

Commit 5071e94

Browse files
darkway-sSefik-Palazoglubehackl
authored
Fix Bug Uncreate() with rate_func via introducing new parameter reversed to class Animation (ManimCommunity#2597)
* `Uncreate()` works with `rate_func` ,fix ManimCommunity#2469 Place the reverse of `rate_func` to become part of the process in `Uncreate()`. The original `uncreate()` is implemented by setting a specific value to `rate_func`. Now, if you want to set parameter`rate_func` when using `Uncreate()`, you can set the parameter `reverse_rate_func` to `Uncreate()`. Examples ```python .. manim:: ShowUncreate class ShowUncreate(Scene): def construct(self): self.play(Uncreate(Square(), reverse_rate_func=linear)) ``` But problems remains when you try to pass parameter `rate_func` to `Scene.play()` * fix issue ManimCommunity#2469 * Add parameter reversed to class Animation Fix an error in the last [commit](darkway-s@47b5196). The "remover" decided whether it would reverse. Now it correctly passes the power of decision to "reversed". Add a bool parameter `reversed` to class `Animation`. It decides whether the animation need to be played backwards. Default to be False. * Add unit tests for usages of `Uncreate()` * Add control data for the new unit test * change the implementation of `Unwrite` to the same logic as `Uncreate` * Rename parameter `reversed`, add descriptions, style ManimCommunity#2597 Rename `reversed` to the more explicit `reverse_rate_function`, in class Animation. Explicitly mention in documentation that setting `reverse_rate_function` doesn't have any effect on `remover` and `introducer`. * improve documentation of parameter `reverse_rate_function`, doc ManimCommunity#2597 improve documentation of parameter `reverse_rate_function` in class `Animation` Co-authored-by: Benjamin Hackl <[email protected]> * doc: remove unnecessary comments in creation.py The comment was added because the name of the parameter `reversed` was similar to reverse, which is a also parameter, but defined in Write(). Now the parameter has a more explicit name `reverse_rate_function`. Developers can get the main idea from its name. So we don't need the comment to tell. Co-authored-by: Sefik-Palazoglu <[email protected]> Co-authored-by: Benjamin Hackl <[email protected]>
1 parent 574ff69 commit 5071e94

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

manim/animation/animation.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class Animation:
4949
5050
For example ``rate_func(0.5)`` is the proportion of the animation that is done
5151
after half of the animations run time.
52+
53+
54+
reverse_rate_function
55+
Reverses the rate function of the animation. Setting ``reverse_rate_function``
56+
does not have any effect on ``remover`` or ``introducer``. These need to be
57+
set explicitly if an introducer-animation should be turned into a remover one
58+
and vice versa.
5259
name
5360
The name of the animation. This gets displayed while rendering the animation.
5461
Defaults to <class-name>(<Mobject-name>).
@@ -123,6 +130,7 @@ def __init__(
123130
lag_ratio: float = DEFAULT_ANIMATION_LAG_RATIO,
124131
run_time: float = DEFAULT_ANIMATION_RUN_TIME,
125132
rate_func: Callable[[float], float] = smooth,
133+
reverse_rate_function: bool = False,
126134
name: str = None,
127135
remover: bool = False, # remove a mobject from the screen?
128136
suspend_mobject_updating: bool = True,
@@ -134,6 +142,7 @@ def __init__(
134142
self._typecheck_input(mobject)
135143
self.run_time: float = run_time
136144
self.rate_func: Callable[[float], float] = rate_func
145+
self.reverse_rate_function: bool = reverse_rate_function
137146
self.name: str | None = name
138147
self.remover: bool = remover
139148
self.introducer: bool = introducer
@@ -361,7 +370,10 @@ def get_sub_alpha(self, alpha: float, index: int, num_submobjects: int) -> float
361370
full_length = (num_submobjects - 1) * lag_ratio + 1
362371
value = alpha * full_length
363372
lower = index * lag_ratio
364-
return self.rate_func(value - lower)
373+
if self.reverse_rate_function:
374+
return self.rate_func(1 - (value - lower))
375+
else:
376+
return self.rate_func(value - lower)
365377

366378
# Getters and setters
367379
def set_run_time(self, run_time: float) -> Animation:

manim/animation/creation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ def construct(self):
191191
def __init__(
192192
self,
193193
mobject: VMobject | OpenGLVMobject,
194-
rate_func: Callable[[float], float] = lambda t: smooth(1 - t),
194+
reverse_rate_function: bool = True,
195195
remover: bool = True,
196196
**kwargs,
197197
) -> None:
198198
super().__init__(
199199
mobject,
200-
rate_func=rate_func,
200+
reverse_rate_function=reverse_rate_function,
201201
introducer=False,
202202
remover=remover,
203203
**kwargs,
@@ -405,7 +405,7 @@ def __init__(
405405
vmobject,
406406
run_time=run_time,
407407
lag_ratio=lag_ratio,
408-
rate_func=lambda t: -rate_func(t) + 1,
408+
reverse_rate_function=True,
409409
reverse=reverse,
410410
**kwargs,
411411
)
Binary file not shown.

tests/test_graphical_units/test_creation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ def test_uncreate(scene):
1919
scene.play(Uncreate(square))
2020

2121

22+
@frames_comparison(last_frame=False)
23+
def test_uncreate_rate_func(scene):
24+
square = Square()
25+
scene.add(square)
26+
scene.play(Uncreate(square), rate_func=linear)
27+
28+
2229
@frames_comparison(last_frame=False)
2330
def test_DrawBorderThenFill(scene):
2431
square = Square(fill_opacity=1)

0 commit comments

Comments
 (0)