Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

type creation.py

Syrus Dark38b9ac7ff24f
diff --git a/manim/animation/animation.py b/manim/animation/animation.py
index 9e59642be..44e6a4e5e 100644
--- a/manim/animation/animation.py
+++ b/manim/animation/animation.py
@@ -8,7 +8,8 @@ import typing
from copy import deepcopy
import numpy as np
-from manim.scene.scene import Scene
+if typing.TYPE_CHECKING:
+ from manim.scene.scene import Scene
from .. import logger
from ..mobject.mobject import Mobject
@@ -85,7 +86,7 @@ class Animation:
if self.suspend_mobject_updating:
self.mobject.resume_updating()
- def clean_up_from_scene(self, scene: Scene) -> None:
+ def clean_up_from_scene(self, scene: 'Scene') -> None:
if self.is_remover():
scene.remove(self.mobject)
@@ -206,7 +207,7 @@ class Wait(Animation):
def finish(self) -> None:
pass
- def clean_up_from_scene(self, scene: Scene) -> None:
+ def clean_up_from_scene(self, scene: 'Scene') -> None:
pass
def update_mobjects(self, dt: int) -> None:
diff --git a/manim/animation/creation.py b/manim/animation/creation.py
index 8540fb34f..9adf23da8 100644
--- a/manim/animation/creation.py
+++ b/manim/animation/creation.py
@@ -69,17 +69,21 @@ __all__ = [
]
+import itertools as it
+import typing
+
+import numpy as np
+from colour import Color
+
+if typing.TYPE_CHECKING:
+ from manim.mobject.svg.text_mobject import Text
+
from ..animation.animation import Animation
from ..animation.composition import Succession
+from ..mobject.mobject import Group, Mobject
from ..mobject.types.vectorized_mobject import VMobject
-from ..mobject.mobject import Group
from ..utils.bezier import integer_interpolate
-from ..utils.rate_functions import linear
-from ..utils.rate_functions import double_smooth
-from ..utils.rate_functions import smooth
-
-import numpy as np
-import itertools as it
+from ..utils.rate_functions import double_smooth, linear, smooth
class ShowPartial(Animation):
@@ -96,15 +100,19 @@ class ShowPartial(Animation):
"""
- def __init__(self, mobject, **kwargs):
+ def __init__(self, mobject: VMobject, **kwargs):
if not isinstance(mobject, VMobject):
raise TypeError("This Animation only works on vectorized mobjects")
super().__init__(mobject, **kwargs)
- def interpolate_submobject(self, submob, start_submob, alpha):
- submob.pointwise_become_partial(start_submob, *self._get_bounds(alpha))
+ def interpolate_submobject(
+ self, submobject: Mobject, starting_sumobject: Mobject, alpha: float
+ ) -> None:
+ submobject.pointwise_become_partial(
+ starting_sumobject, *self._get_bounds(alpha)
+ )
- def _get_bounds(self, alpha):
+ def _get_bounds(self, alpha: float) -> None:
raise NotImplementedError("Please use ShowCreation or ShowPassingFlash")
@@ -135,10 +143,10 @@ class ShowCreation(ShowPartial):
"""
- def __init__(self, mobject, lag_ratio=1, **kwargs):
+ def __init__(self, mobject: VMobject, lag_ratio: float = 1, **kwargs) -> None:
super().__init__(mobject, lag_ratio=lag_ratio, **kwargs)
- def _get_bounds(self, alpha):
+ def _get_bounds(self, alpha: float) -> typing.Tuple[int, float]:
return (0, alpha)
@@ -152,8 +160,14 @@ class Uncreate(ShowCreation):
"""
def __init__(
- self, mobject, rate_func=lambda t: smooth(1 - t), remover=True, **kwargs
- ):
+ self,
+ mobject: VMobject,
+ rate_func: typing.Callable[[float, float], np.ndarray] = lambda t: smooth(
+ 1 - t
+ ),
+ remover: bool = True,
+ **kwargs
+ ) -> None:
super().__init__(mobject, rate_func=rate_func, remover=remover, **kwargs)
@@ -162,15 +176,15 @@ class DrawBorderThenFill(Animation):
def __init__(
self,
- vmobject,
- run_time=2,
- rate_func=double_smooth,
- stroke_width=2,
- stroke_color=None,
- draw_border_animation_config={},
- fill_animation_config={},
+ vmobject: VMobject,
+ run_time: float = 2,
+ rate_func: typing.Callable[[float], np.ndarray] = double_smooth,
+ stroke_width: float = 2,
+ stroke_color: str = None,
+ draw_border_animation_config: typing.Dict = {}, # what does this dict accept?
+ fill_animation_config: typing.Dict = {},
**kwargs
- ):
+ ) -> None:
self._typecheck_input(vmobject)
super().__init__(vmobject, run_time=run_time, rate_func=rate_func, **kwargs)
self.stroke_width = stroke_width
@@ -179,46 +193,53 @@ class DrawBorderThenFill(Animation):
self.fill_animation_config = fill_animation_config
self.outline = None
- def _typecheck_input(self, vmobject):
+ def _typecheck_input(self, vmobject: VMobject) -> None:
if not isinstance(vmobject, VMobject):
raise TypeError("DrawBorderThenFill only works for VMobjects")
- def begin(self):
+ def begin(self) -> None:
self.outline = self.get_outline()
super().begin()
- def get_outline(self):
+ def get_outline(self) -> Mobject:
outline = self.mobject.copy()
outline.set_fill(opacity=0)
for sm in outline.family_members_with_points():
sm.set_stroke(color=self.get_stroke_color(sm), width=self.stroke_width)
return outline
- def get_stroke_color(self, vmobject):
+ def get_stroke_color(self, vmobject: VMobject) -> Color:
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):
+ def get_all_mobjects(self) -> typing.List[typing.Union[Mobject, None]]:
return [*super().get_all_mobjects(), self.outline]
- def interpolate_submobject(self, submob, start, outline, alpha):
+ def interpolate_submobject(
+ self, submobject: Mobject, starting_sumobject: Mobject, outline, alpha: float
+ ) -> None: # Fixme: not matching the parent class? What is outline doing here?
index, subalpha = integer_interpolate(0, 2, alpha)
if index == 0:
- submob.pointwise_become_partial(outline, 0, subalpha)
- submob.match_style(outline)
+ submobject.pointwise_become_partial(outline, 0, subalpha)
+ submobject.match_style(outline)
else:
- submob.interpolate(outline, start, subalpha)
+ submobject.interpolate(outline, starting_sumobject, subalpha)
class Write(DrawBorderThenFill):
"""Simulate hand-writing a :class:`~.Text` or hand-drawing a :class:`~.VMobject`."""
def __init__(
- self, vmobject, run_time=None, lag_ratio=None, rate_func=linear, **kwargs
- ):
+ self,
+ vmobject: VMobject,
+ run_time: float = None,
+ lag_ratio: float = None,
+ rate_func: typing.Callable[[float], np.ndarray] = linear,
+ **kwargs
+ ) -> None:
self.run_time = run_time
self.lag_ratio = lag_ratio
self._set_default_config_from_length(vmobject)
@@ -230,7 +251,7 @@ class Write(DrawBorderThenFill):
**kwargs,
)
- def _set_default_config_from_length(self, vmobject):
+ def _set_default_config_from_length(self, vmobject: VMobject) -> None:
length = len(vmobject.family_members_with_points())
if self.run_time is None:
if length < 15:
@@ -245,20 +266,24 @@ class ShowIncreasingSubsets(Animation):
"""Show one submobject at a time, leaving all previous ones displayed on screen."""
def __init__(
- self, group, suspend_mobject_updating=False, int_func=np.floor, **kwargs
- ):
+ self,
+ group: Mobject,
+ suspend_mobject_updating: bool = False,
+ int_func: typing.Callable[[np.ndarray], np.ndarray] = np.floor,
+ **kwargs
+ ) -> None:
self.all_submobs = list(group.submobjects)
self.int_func = int_func
super().__init__(
group, suspend_mobject_updating=suspend_mobject_updating, **kwargs
)
- def interpolate_mobject(self, alpha):
+ def interpolate_mobject(self, alpha: float) -> None:
n_submobs = len(self.all_submobs)
index = int(self.int_func(alpha * n_submobs))
self.update_submobject_list(index)
- def update_submobject_list(self, index):
+ def update_submobject_list(self, index: int) -> None:
self.mobject.submobjects = self.all_submobs[:index]
@@ -274,14 +299,14 @@ class AddTextLetterByLetter(ShowIncreasingSubsets):
def __init__(
self,
- text,
- suspend_mobject_updating=False,
- int_func=np.ceil,
- rate_func=linear,
- time_per_char=0.1,
- run_time=None,
+ text: "Text",
+ suspend_mobject_updating: bool = False,
+ int_func: typing.Callable[[np.ndarray], np.ndarray] = np.ceil,
+ rate_func: typing.Callable[[float], float] = linear,
+ time_per_char: float = 0.1,
+ run_time: typing.Optional[float] = None,
**kwargs
- ):
+ ) -> None:
# time_per_char must be above 0.06, or the animation won't finish
self.time_per_char = time_per_char
self.run_time = run_time
@@ -301,11 +326,16 @@ class AddTextLetterByLetter(ShowIncreasingSubsets):
class ShowSubmobjectsOneByOne(ShowIncreasingSubsets):
"""Show one submobject at a time, removing all previously displayed ones from screen."""
- def __init__(self, group, int_func=np.ceil, **kwargs):
+ def __init__(
+ self,
+ group: typing.Iterable[Mobject],
+ int_func: typing.Callable[[np.ndarray], np.ndarray] = np.ceil,
+ **kwargs
+ ) -> None:
new_group = Group(*group)
super().__init__(new_group, int_func=int_func, **kwargs)
- def update_submobject_list(self, index):
+ def update_submobject_list(self, index: int) -> None:
if index == 0:
self.mobject.submobjects = []
else:
@@ -316,7 +346,13 @@ class ShowSubmobjectsOneByOne(ShowIncreasingSubsets):
class AddTextWordByWord(Succession):
"""Show a :class:`~.Text` word by word on the scene."""
- def __init__(self, text_mobject, run_time=None, time_per_char=0.06, **kwargs):
+ def __init__(
+ self,
+ text_mobject: "Text",
+ run_time: float = None,
+ time_per_char: float = 0.06,
+ **kwargs
+ ) -> None:
self.time_per_char = time_per_char
tpc = self.time_per_char
anims = it.chain(