2018-12-24 12:37:51 -08:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from manimlib.animation.animation import Animation
|
|
|
|
from manimlib.mobject.mobject import Group
|
2019-02-08 16:50:48 -08:00
|
|
|
from manimlib.utils.bezier import integer_interpolate
|
2019-02-08 14:48:17 -08:00
|
|
|
from manimlib.utils.bezier import interpolate
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.config_ops import digest_config
|
2019-02-08 16:50:48 -08:00
|
|
|
from manimlib.utils.iterables import remove_list_redundancies
|
2019-02-05 15:39:58 -08:00
|
|
|
from manimlib.utils.rate_functions import linear
|
2020-02-18 22:41:55 -08:00
|
|
|
from manimlib.utils.simple_functions import clip
|
2018-03-08 13:52:37 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-11 22:37:52 -08:00
|
|
|
DEFAULT_LAGGED_START_LAG_RATIO = 0.05
|
|
|
|
|
|
|
|
|
2019-02-08 14:48:17 -08:00
|
|
|
class AnimationGroup(Animation):
|
2018-03-08 13:52:37 -08:00
|
|
|
CONFIG = {
|
2019-02-08 14:48:17 -08:00
|
|
|
# If None, this defaults to the sum of all
|
|
|
|
# internal animations
|
|
|
|
"run_time": None,
|
2019-02-08 16:50:48 -08:00
|
|
|
"rate_func": linear,
|
2019-02-08 14:48:17 -08:00
|
|
|
# If 0, all animations are played at once.
|
|
|
|
# If 1, all are played successively.
|
|
|
|
# If >0 and <1, they start at lagged times
|
|
|
|
# from one and other.
|
|
|
|
"lag_ratio": 0,
|
2019-02-09 11:54:00 -08:00
|
|
|
"group": None,
|
2018-03-08 13:52:37 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-08 14:48:17 -08:00
|
|
|
def __init__(self, *animations, **kwargs):
|
2019-02-09 11:54:00 -08:00
|
|
|
digest_config(self, kwargs)
|
2018-03-08 13:52:37 -08:00
|
|
|
self.animations = animations
|
2019-02-09 11:54:00 -08:00
|
|
|
if self.group is None:
|
|
|
|
self.group = Group(*remove_list_redundancies(
|
|
|
|
[anim.mobject for anim in animations]
|
|
|
|
))
|
2019-09-30 12:06:17 -07:00
|
|
|
self.init_run_time()
|
2019-02-08 14:48:17 -08:00
|
|
|
Animation.__init__(self, self.group, **kwargs)
|
2018-03-08 13:52:37 -08:00
|
|
|
|
2019-02-08 14:48:17 -08:00
|
|
|
def get_all_mobjects(self):
|
|
|
|
return self.group
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-08 20:18:54 -08:00
|
|
|
def begin(self):
|
|
|
|
for anim in self.animations:
|
|
|
|
anim.begin()
|
2019-09-30 12:06:17 -07:00
|
|
|
# self.init_run_time()
|
2019-02-08 20:18:54 -08:00
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
for anim in self.animations:
|
|
|
|
anim.finish()
|
|
|
|
|
|
|
|
def clean_up_from_scene(self, scene):
|
|
|
|
for anim in self.animations:
|
|
|
|
anim.clean_up_from_scene(scene)
|
|
|
|
|
|
|
|
def update_mobjects(self, dt):
|
|
|
|
for anim in self.animations:
|
|
|
|
anim.update_mobjects(dt)
|
|
|
|
|
2019-02-08 14:48:17 -08:00
|
|
|
def init_run_time(self):
|
2019-02-08 15:06:49 -08:00
|
|
|
self.build_animations_with_timings()
|
2019-02-08 20:18:54 -08:00
|
|
|
if self.anims_with_timings:
|
|
|
|
self.max_end_time = np.max([
|
|
|
|
awt[2] for awt in self.anims_with_timings
|
|
|
|
])
|
|
|
|
else:
|
|
|
|
self.max_end_time = 0
|
2019-02-08 15:06:49 -08:00
|
|
|
if self.run_time is None:
|
|
|
|
self.run_time = self.max_end_time
|
|
|
|
|
|
|
|
def build_animations_with_timings(self):
|
|
|
|
"""
|
|
|
|
Creates a list of triplets of the form
|
|
|
|
(anim, start_time, end_time)
|
|
|
|
"""
|
2019-02-08 14:48:17 -08:00
|
|
|
self.anims_with_timings = []
|
|
|
|
curr_time = 0
|
|
|
|
for anim in self.animations:
|
|
|
|
start_time = curr_time
|
|
|
|
end_time = start_time + anim.get_run_time()
|
|
|
|
self.anims_with_timings.append(
|
|
|
|
(anim, start_time, end_time)
|
|
|
|
)
|
|
|
|
# Start time of next animation is based on
|
|
|
|
# the lag_ratio
|
|
|
|
curr_time = interpolate(
|
|
|
|
start_time, end_time, self.lag_ratio
|
|
|
|
)
|
2018-03-08 13:52:37 -08:00
|
|
|
|
2019-02-08 14:48:17 -08:00
|
|
|
def interpolate(self, alpha):
|
2019-02-08 15:06:49 -08:00
|
|
|
# Note, if the run_time of AnimationGroup has been
|
|
|
|
# set to something other than its default, these
|
|
|
|
# times might not correspond to actual times,
|
|
|
|
# e.g. of the surrounding scene. Instead they'd
|
|
|
|
# be a rescaled version. But that's okay!
|
|
|
|
time = alpha * self.max_end_time
|
2019-02-08 14:48:17 -08:00
|
|
|
for anim, start_time, end_time in self.anims_with_timings:
|
|
|
|
anim_time = end_time - start_time
|
|
|
|
if anim_time == 0:
|
|
|
|
sub_alpha = 0
|
|
|
|
else:
|
2020-02-18 22:41:55 -08:00
|
|
|
sub_alpha = clip(
|
2019-02-08 14:48:17 -08:00
|
|
|
(time - start_time) / anim_time,
|
|
|
|
0, 1
|
|
|
|
)
|
|
|
|
anim.interpolate(sub_alpha)
|
2018-03-08 13:52:37 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-08 14:48:17 -08:00
|
|
|
class Succession(AnimationGroup):
|
|
|
|
CONFIG = {
|
|
|
|
"lag_ratio": 1,
|
|
|
|
}
|
2018-03-08 13:52:37 -08:00
|
|
|
|
2019-02-08 16:50:48 -08:00
|
|
|
def begin(self):
|
2019-02-08 20:18:54 -08:00
|
|
|
assert(len(self.animations) > 0)
|
2019-02-09 11:54:00 -08:00
|
|
|
self.init_run_time()
|
2019-02-08 20:18:54 -08:00
|
|
|
self.active_animation = self.animations[0]
|
2019-02-08 16:50:48 -08:00
|
|
|
self.active_animation.begin()
|
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
self.active_animation.finish()
|
|
|
|
|
|
|
|
def update_mobjects(self, dt):
|
|
|
|
self.active_animation.update_mobjects(dt)
|
|
|
|
|
|
|
|
def interpolate(self, alpha):
|
|
|
|
index, subalpha = integer_interpolate(
|
|
|
|
0, len(self.animations), alpha
|
|
|
|
)
|
|
|
|
animation = self.animations[index]
|
|
|
|
if animation is not self.active_animation:
|
|
|
|
self.active_animation.finish()
|
|
|
|
animation.begin()
|
|
|
|
self.active_animation = animation
|
|
|
|
animation.interpolate(subalpha)
|
|
|
|
|
2019-02-08 15:06:49 -08:00
|
|
|
|
|
|
|
class LaggedStart(AnimationGroup):
|
|
|
|
CONFIG = {
|
2019-02-11 22:37:52 -08:00
|
|
|
"lag_ratio": DEFAULT_LAGGED_START_LAG_RATIO,
|
2019-02-08 15:06:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-14 12:06:21 -08:00
|
|
|
class LaggedStartMap(LaggedStart):
|
2018-03-08 13:52:37 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"run_time": 2,
|
2018-03-08 13:52:37 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
|
|
|
def __init__(self, AnimationClass, mobject, arg_creator=None, **kwargs):
|
2019-02-14 12:06:21 -08:00
|
|
|
args_list = []
|
|
|
|
for submob in mobject:
|
|
|
|
if arg_creator:
|
|
|
|
args_list.append(arg_creator(submob))
|
|
|
|
else:
|
|
|
|
args_list.append((submob,))
|
2019-03-28 16:18:35 -07:00
|
|
|
anim_kwargs = dict(kwargs)
|
2019-03-29 15:12:31 -07:00
|
|
|
if "lag_ratio" in anim_kwargs:
|
|
|
|
anim_kwargs.pop("lag_ratio")
|
2019-02-14 12:06:21 -08:00
|
|
|
animations = [
|
2019-03-28 16:18:35 -07:00
|
|
|
AnimationClass(*args, **anim_kwargs)
|
2019-02-14 12:06:21 -08:00
|
|
|
for args in args_list
|
2018-03-31 15:11:35 -07:00
|
|
|
]
|
2020-02-18 22:41:55 -08:00
|
|
|
super().__init__(*animations, group=mobject, **kwargs)
|