3b1b-manim/manimlib/animation/creation.py

204 lines
5.8 KiB
Python
Raw Normal View History

from manimlib.animation.animation import Animation
from manimlib.animation.composition import Succession
from manimlib.mobject.types.vectorized_mobject import VMobject
2019-09-19 13:38:03 -07:00
from manimlib.mobject.mobject import Group
2019-02-09 08:59:02 -08:00
from manimlib.utils.bezier import integer_interpolate
from manimlib.utils.config_ops import digest_config
from manimlib.utils.rate_functions import linear
2019-02-09 08:59:02 -08:00
from manimlib.utils.rate_functions import double_smooth
from manimlib.utils.rate_functions import smooth
import numpy as np
import itertools as it
class ShowPartial(Animation):
2019-02-08 15:44:58 -08:00
"""
Abstract class for ShowCreation and ShowPassingFlash
"""
2021-01-13 11:55:45 -10:00
CONFIG = {
"should_match_start": False,
}
def begin(self):
super().begin()
if not self.should_match_start:
self.mobject.lock_matching_data(self.mobject, self.starting_mobject)
2019-02-08 15:44:58 -08:00
def interpolate_submobject(self, submob, start_submob, alpha):
submob.pointwise_become_partial(
start_submob, *self.get_bounds(alpha)
)
def get_bounds(self, alpha):
raise Exception("Not Implemented")
class ShowCreation(ShowPartial):
CONFIG = {
2019-02-08 15:44:58 -08:00
"lag_ratio": 1,
}
def get_bounds(self, alpha):
return (0, alpha)
class Uncreate(ShowCreation):
CONFIG = {
"rate_func": lambda t: smooth(1 - t),
2021-01-13 11:55:45 -10:00
"remover": True,
"should_match_start": True,
}
2019-02-09 08:59:02 -08:00
class DrawBorderThenFill(Animation):
CONFIG = {
"run_time": 2,
2019-02-09 08:59:02 -08:00
"rate_func": double_smooth,
"stroke_width": 2,
"stroke_color": None,
"draw_border_animation_config": {},
"fill_animation_config": {},
}
def __init__(self, vmobject, **kwargs):
2021-01-13 11:55:45 -10:00
assert(isinstance(vmobject, VMobject))
self.sm_to_index = dict([
(hash(sm), 0)
for sm in vmobject.get_family()
])
2019-02-09 10:56:51 -08:00
super().__init__(vmobject, **kwargs)
def begin(self):
# Trigger triangulation calculation
for submob in self.mobject.get_family():
submob.get_triangulation()
2019-02-09 08:59:02 -08:00
self.outline = self.get_outline()
super().begin()
2021-01-13 11:55:45 -10:00
self.mobject.match_style(self.outline)
self.mobject.lock_matching_data(self.mobject, self.outline)
2021-02-03 14:17:55 -08:00
def finish(self):
super().finish()
self.mobject.unlock_data()
2019-02-09 08:59:02 -08:00
def get_outline(self):
outline = self.mobject.copy()
outline.set_fill(opacity=0)
2021-01-13 00:07:58 -10:00
for sm in outline.get_family():
2019-03-24 11:32:12 -07:00
sm.set_stroke(
color=self.get_stroke_color(sm),
2021-01-13 00:07:58 -10:00
width=float(self.stroke_width)
2019-03-24 11:32:12 -07:00
)
2019-02-09 08:59:02 -08:00
return outline
def get_stroke_color(self, vmobject):
if self.stroke_color:
return self.stroke_color
elif vmobject.get_stroke_width() > 0:
return vmobject.get_stroke_color()
return vmobject.get_color()
2019-02-09 08:59:02 -08:00
def get_all_mobjects(self):
return [*super().get_all_mobjects(), self.outline]
2019-02-09 08:59:02 -08:00
def interpolate_submobject(self, submob, start, outline, alpha):
index, subalpha = integer_interpolate(0, 2, alpha)
2021-01-13 11:55:45 -10:00
if index == 1 and self.sm_to_index[hash(submob)] == 0:
# First time crossing over
submob.set_data(outline.data)
submob.unlock_data()
submob.lock_matching_data(submob, start)
submob.needs_new_triangulation = False
2021-01-13 11:55:45 -10:00
self.sm_to_index[hash(submob)] = 1
2019-02-09 08:59:02 -08:00
if index == 0:
2020-02-18 22:40:12 -08:00
submob.pointwise_become_partial(outline, 0, subalpha)
2019-02-09 08:59:02 -08:00
else:
submob.interpolate(outline, start, subalpha)
2019-02-09 08:59:02 -08:00
class Write(DrawBorderThenFill):
CONFIG = {
# To be figured out in
# set_default_config_from_lengths
"run_time": None,
"lag_ratio": None,
2019-02-09 08:59:02 -08:00
"rate_func": linear,
}
2019-02-09 08:59:02 -08:00
def __init__(self, mobject, **kwargs):
digest_config(self, kwargs)
2019-02-09 08:59:02 -08:00
self.set_default_config_from_length(mobject)
2019-02-09 10:56:51 -08:00
super().__init__(mobject, **kwargs)
2019-02-09 08:59:02 -08:00
def set_default_config_from_length(self, mobject):
length = len(mobject.family_members_with_points())
if self.run_time is None:
if length < 15:
self.run_time = 1
else:
self.run_time = 2
if self.lag_ratio is None:
self.lag_ratio = min(4.0 / length, 0.2)
2019-01-17 14:09:15 -08:00
class ShowIncreasingSubsets(Animation):
2019-02-09 10:56:51 -08:00
CONFIG = {
"suspend_mobject_updating": False,
"int_func": np.round,
2019-02-09 10:56:51 -08:00
}
2019-01-17 14:09:15 -08:00
def __init__(self, group, **kwargs):
self.all_submobs = list(group.submobjects)
2019-02-09 10:56:51 -08:00
super().__init__(group, **kwargs)
2019-01-17 14:09:15 -08:00
def interpolate_mobject(self, alpha):
2019-01-17 14:09:15 -08:00
n_submobs = len(self.all_submobs)
index = int(self.int_func(alpha * n_submobs))
2019-09-19 13:38:03 -07:00
self.update_submobject_list(index)
def update_submobject_list(self, index):
self.mobject.set_submobjects(self.all_submobs[:index])
2019-09-19 13:38:03 -07:00
class ShowSubmobjectsOneByOne(ShowIncreasingSubsets):
CONFIG = {
"int_func": np.ceil,
}
2019-09-19 13:38:03 -07:00
def __init__(self, group, **kwargs):
new_group = Group(*group)
super().__init__(new_group, **kwargs)
def update_submobject_list(self, index):
# N = len(self.all_submobs)
if index == 0:
self.mobject.set_submobjects([])
2019-09-19 13:38:03 -07:00
else:
self.mobject.set_submobjects(self.all_submobs[index - 1])
# TODO, this is broken...
class AddTextWordByWord(Succession):
CONFIG = {
# If given a value for run_time, it will
# override the time_per_char
"run_time": None,
"time_per_char": 0.06,
}
def __init__(self, text_mobject, **kwargs):
digest_config(self, kwargs)
tpc = self.time_per_char
anims = it.chain(*[
[
ShowIncreasingSubsets(word, run_time=tpc * len(word)),
Animation(word, run_time=0.005 * len(word)**1.5),
]
for word in text_mobject
])
super().__init__(*anims, **kwargs)