Kill config in creation.py

This commit is contained in:
Grant Sanderson 2022-12-14 10:58:35 -08:00
parent 958002152e
commit 187de0163f

View file

@ -5,11 +5,10 @@ from abc import ABC, abstractmethod
import numpy as np import numpy as np
from manimlib.animation.animation import Animation from manimlib.animation.animation import Animation
from manimlib.constants import WHITE
from manimlib.mobject.svg.string_mobject import StringMobject from manimlib.mobject.svg.string_mobject import StringMobject
from manimlib.mobject.types.vectorized_mobject import VGroup
from manimlib.mobject.types.vectorized_mobject import VMobject from manimlib.mobject.types.vectorized_mobject import VMobject
from manimlib.utils.bezier import integer_interpolate from manimlib.utils.bezier import integer_interpolate
from manimlib.utils.config_ops import digest_config
from manimlib.utils.rate_functions import linear from manimlib.utils.rate_functions import linear
from manimlib.utils.rate_functions import double_smooth from manimlib.utils.rate_functions import double_smooth
from manimlib.utils.rate_functions import smooth from manimlib.utils.rate_functions import smooth
@ -18,15 +17,17 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from manimlib.mobject.mobject import Mobject from manimlib.mobject.mobject import Mobject
from manimlib.scene.scene import Scene
from manimlib.constants import ManimColor
class ShowPartial(Animation, ABC): class ShowPartial(Animation, ABC):
""" """
Abstract class for ShowCreation and ShowPassingFlash Abstract class for ShowCreation and ShowPassingFlash
""" """
CONFIG = { def __init__(self, mobject: Mobject, should_match_start: bool = False, **kwargs):
"should_match_start": False, self.should_match_start = should_match_start
} super().__init__(mobject, **kwargs)
def begin(self) -> None: def begin(self) -> None:
super().begin() super().begin()
@ -53,39 +54,56 @@ class ShowPartial(Animation, ABC):
class ShowCreation(ShowPartial): class ShowCreation(ShowPartial):
CONFIG = { def __init__(self, mobject: Mobject, lag_ratio: float = 1.0, **kwargs):
"lag_ratio": 1, super().__init__(mobject, lag_ratio=lag_ratio, **kwargs)
}
def get_bounds(self, alpha: float) -> tuple[float, float]: def get_bounds(self, alpha: float) -> tuple[float, float]:
return (0, alpha) return (0, alpha)
class Uncreate(ShowCreation): class Uncreate(ShowCreation):
CONFIG = { def __init__(
"rate_func": lambda t: smooth(1 - t), self,
"remover": True, mobject: Mobject,
"should_match_start": True, rate_func: Callable[[float], float] = lambda t: smooth(1 - t),
} remover: bool = True,
should_match_start: bool = True,
**kwargs,
):
super().__init__(
mobject,
rate_func=rate_func,
remover=remover,
should_match_start=should_match_start,
**kwargs,
)
class DrawBorderThenFill(Animation): class DrawBorderThenFill(Animation):
CONFIG = { def __init__(
"run_time": 2, self,
"rate_func": double_smooth, vmobject: VMobject,
"stroke_width": 2, run_time: float = 2.0,
"stroke_color": None, rate_func: Callable[[float], float] = double_smooth,
"draw_border_animation_config": {}, stroke_width: float = 2.0,
"fill_animation_config": {}, stroke_color: ManimColor = None,
} draw_border_animation_config: dict = {},
fill_animation_config: dict = {},
def __init__(self, vmobject: VMobject, **kwargs): **kwargs
):
assert(isinstance(vmobject, VMobject)) assert(isinstance(vmobject, VMobject))
self.sm_to_index = dict([ self.sm_to_index = {hash(sm): 0 for sm in vmobject.get_family()}
(hash(sm), 0) self.stroke_width = stroke_width
for sm in vmobject.get_family() self.stroke_color = stroke_color
]) self.draw_border_animation_config = draw_border_animation_config
super().__init__(vmobject, **kwargs) self.fill_animation_config = fill_animation_config
super().__init__(
vmobject,
run_time=run_time,
rate_func=rate_func,
**kwargs
)
self.mobject = vmobject
def begin(self) -> None: def begin(self) -> None:
# Trigger triangulation calculation # Trigger triangulation calculation
@ -104,21 +122,14 @@ class DrawBorderThenFill(Animation):
def get_outline(self) -> VMobject: def get_outline(self) -> VMobject:
outline = self.mobject.copy() outline = self.mobject.copy()
outline.set_fill(opacity=0) outline.set_fill(opacity=0)
for sm in outline.get_family(): for sm in outline.family_members_with_points():
sm.set_stroke( sm.set_stroke(
color=self.get_stroke_color(sm), color=self.stroke_color or sm.get_stroke_color(),
width=float(self.stroke_width) width=self.stroke_width,
) )
return outline return outline
def get_stroke_color(self, vmobject: VMobject) -> str: def get_all_mobjects(self) -> list[Mobject]:
if self.stroke_color:
return self.stroke_color
elif vmobject.get_stroke_width() > 0:
return vmobject.get_stroke_color()
return vmobject.get_color()
def get_all_mobjects(self) -> list[VMobject]:
return [*super().get_all_mobjects(), self.outline] return [*super().get_all_mobjects(), self.outline]
def interpolate_submobject( def interpolate_submobject(
@ -146,39 +157,51 @@ class DrawBorderThenFill(Animation):
class Write(DrawBorderThenFill): class Write(DrawBorderThenFill):
CONFIG = { def __init__(
# To be figured out in self,
# set_default_config_from_lengths vmobject: VMobject,
"run_time": None, run_time: float = -1, # If negative, this will be reassigned
"lag_ratio": None, lag_ratio: float = -1, # If negative, this will be reassigned
"rate_func": linear, rate_func: Callable[[float], float] = linear,
} stroke_color: ManimColor = WHITE,
**kwargs
):
family_size = len(vmobject.family_members_with_points())
super().__init__(
vmobject,
run_time=self.compute_run_time(family_size, run_time),
lag_ratio=self.compute_lag_ratio(family_size, lag_ratio),
rate_func=rate_func,
stroke_color=stroke_color,
**kwargs
)
def __init__(self, vmobject: VMobject, **kwargs): def compute_run_time(self, family_size: int, run_time: float):
digest_config(self, kwargs) if run_time < 0:
self.set_default_config_from_length(vmobject) return 1 if family_size < 15 else 2
super().__init__(vmobject, **kwargs) return run_time
def set_default_config_from_length(self, vmobject: VMobject) -> None: def compute_lag_ratio(self, family_size: int, lag_ratio: float):
length = len(vmobject.family_members_with_points()) if lag_ratio < 0:
if self.run_time is None: return min(4.0 / (family_size + 1.0), 0.2)
if length < 15: return lag_ratio
self.run_time = 1
else:
self.run_time = 2
if self.lag_ratio is None:
self.lag_ratio = min(4.0 / (length + 1.0), 0.2)
class ShowIncreasingSubsets(Animation): class ShowIncreasingSubsets(Animation):
CONFIG = { def __init__(
"suspend_mobject_updating": False, self,
"int_func": np.round, group: Mobject,
} int_func: Callable[[float], float] = np.round,
suspend_mobject_updating: bool = False,
def __init__(self, group: Mobject, **kwargs): **kwargs
):
self.all_submobs = list(group.submobjects) self.all_submobs = list(group.submobjects)
super().__init__(group, **kwargs) self.int_func = int_func
super().__init__(
group,
suspend_mobject_updating=suspend_mobject_updating,
**kwargs
)
def interpolate_mobject(self, alpha: float) -> None: def interpolate_mobject(self, alpha: float) -> None:
n_submobs = len(self.all_submobs) n_submobs = len(self.all_submobs)
@ -190,9 +213,13 @@ class ShowIncreasingSubsets(Animation):
class ShowSubmobjectsOneByOne(ShowIncreasingSubsets): class ShowSubmobjectsOneByOne(ShowIncreasingSubsets):
CONFIG = { def __init__(
"int_func": np.ceil, self,
} group: Mobject,
int_func: Callable[[float], float] = np.ceil,
**kwargs
):
super().__init__(group, int_func=int_func, **kwargs)
def update_submobject_list(self, index: int) -> None: def update_submobject_list(self, index: int) -> None:
# N = len(self.all_submobs) # N = len(self.all_submobs)
@ -203,18 +230,27 @@ class ShowSubmobjectsOneByOne(ShowIncreasingSubsets):
class AddTextWordByWord(ShowIncreasingSubsets): class AddTextWordByWord(ShowIncreasingSubsets):
CONFIG = { def __init__(
# If given a value for run_time, it will self,
# override the time_per_word string_mobject: StringMobject,
"run_time": None, time_per_word: float = 0.2,
"time_per_word": 0.2, run_time: float = -1.0, # If negative, it will be recomputed with time_per_word
"rate_func": linear, rate_func: Callable[[float], float] = linear,
} **kwargs
):
def __init__(self, string_mobject, **kwargs):
assert isinstance(string_mobject, StringMobject) assert isinstance(string_mobject, StringMobject)
grouped_mobject = string_mobject.build_groups() grouped_mobject = string_mobject.build_groups()
digest_config(self, kwargs) if run_time < 0:
if self.run_time is None: run_time = time_per_word * len(grouped_mobject)
self.run_time = self.time_per_word * len(grouped_mobject) super().__init__(
super().__init__(grouped_mobject, **kwargs) grouped_mobject,
run_time=run_time,
rate_func=rate_func,
**kwargs
)
self.string_mobject = string_mobject
def clean_up_from_scene(self, scene: Scene) -> None:
scene.remove(self.mobject)
if not self.is_remover():
scene.add(self.string_mobject)