Skip to content

Commit b631109

Browse files
Improved handling of attributes when using the .animate syntax (#2665)
* apply all methods to original mobject after finishing _MethodBuilder animation * added test to check whether custom attributes are changed * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f73861e commit b631109

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

manim/animation/transform.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ def __init__(self, mobject, methods):
360360
self.methods = methods
361361
super().__init__(mobject)
362362

363+
def finish(self) -> None:
364+
for method, method_args, method_kwargs in self.methods:
365+
method.__func__(self.mobject, *method_args, **method_kwargs)
366+
super().finish()
367+
363368

364369
class ApplyMethod(Transform):
365370
"""Animates a mobject by applying a method.

manim/mobject/mobject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2783,7 +2783,6 @@ def __call__(self, **kwargs):
27832783

27842784
def __getattr__(self, method_name):
27852785
method = getattr(self.mobject.target, method_name)
2786-
self.methods.append(method)
27872786
has_overridden_animation = hasattr(method, "_override_animate")
27882787

27892788
if (self.is_chaining and has_overridden_animation) or self.overridden_animation:
@@ -2801,6 +2800,7 @@ def update_target(*method_args, **method_kwargs):
28012800
**method_kwargs,
28022801
)
28032802
else:
2803+
self.methods.append([method, method_args, method_kwargs])
28042804
method(*method_args, **method_kwargs)
28052805
return self
28062806

tests/test_scene_rendering/test_play_logic.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,19 @@ def test_t_values_save_last_frame(using_temp_config):
101101
scene.update_to_time = Mock()
102102
scene.render()
103103
scene.update_to_time.assert_called_once_with(1)
104+
105+
106+
def test_animate_with_changed_custom_attribute(using_temp_config):
107+
"""Test that animating the change of a custom attribute
108+
using the animate syntax works correctly.
109+
"""
110+
111+
class CustomAnimateScene(Scene):
112+
def construct(self):
113+
vt = ValueTracker(0)
114+
vt.custom_attribute = "hello"
115+
self.play(vt.animate.set_value(42).set(custom_attribute="world"))
116+
assert vt.get_value() == 42
117+
assert vt.custom_attribute == "world"
118+
119+
CustomAnimateScene().render()

0 commit comments

Comments
 (0)