mirror of
https://github.com/3b1b/manim.git
synced 2025-07-31 05:52:34 +08:00
Add .animate syntax
This commit is contained in:
@ -80,6 +80,11 @@ class Mobject(object):
|
||||
if self.depth_test:
|
||||
self.apply_depth_test()
|
||||
|
||||
@property
|
||||
def animate(self):
|
||||
# Borrowed from https://github.com/ManimCommunity/manim/
|
||||
return _AnimationBuilder(self)
|
||||
|
||||
def __str__(self):
|
||||
return self.__class__.__name__
|
||||
|
||||
@ -1571,3 +1576,51 @@ class Point(Mobject):
|
||||
|
||||
def set_location(self, new_loc):
|
||||
self.set_points(np.array(new_loc, ndmin=2, dtype=float))
|
||||
|
||||
|
||||
class _AnimationBuilder:
|
||||
def __init__(self, mobject):
|
||||
self.mobject = mobject
|
||||
self.overridden_animation = None
|
||||
self.mobject.generate_target()
|
||||
self.is_chaining = False
|
||||
self.methods = []
|
||||
|
||||
def __getattr__(self, method_name):
|
||||
method = getattr(self.mobject.target, method_name)
|
||||
self.methods.append(method)
|
||||
has_overridden_animation = hasattr(method, "_override_animate")
|
||||
|
||||
if (self.is_chaining and has_overridden_animation) or self.overridden_animation:
|
||||
raise NotImplementedError(
|
||||
"Method chaining is currently not supported for "
|
||||
"overridden animations"
|
||||
)
|
||||
|
||||
def update_target(*method_args, **method_kwargs):
|
||||
if has_overridden_animation:
|
||||
self.overridden_animation = method._override_animate(
|
||||
self.mobject, *method_args, **method_kwargs
|
||||
)
|
||||
else:
|
||||
method(*method_args, **method_kwargs)
|
||||
return self
|
||||
|
||||
self.is_chaining = True
|
||||
return update_target
|
||||
|
||||
def build(self):
|
||||
from manimlib.animation.transform import _MethodAnimation
|
||||
|
||||
if self.overridden_animation:
|
||||
return self.overridden_animation
|
||||
|
||||
return _MethodAnimation(self.mobject, self.methods)
|
||||
|
||||
|
||||
def override_animate(method):
|
||||
def decorator(animation_method):
|
||||
method._override_animate = animation_method
|
||||
return animation_method
|
||||
|
||||
return decorator
|
||||
|
Reference in New Issue
Block a user