Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Moved more of animation cleanup handling to the animations themselves

Grant Sandersonff0c122d4d0a
diff --git a/animation/animation.py b/animation/animation.py
index cbdee8ba5..5e0dfacd9 100644
--- a/animation/animation.py
+++ b/animation/animation.py
@@ -118,8 +118,11 @@ class Animation(object):
def is_remover(self):
return self.remover
- def clean_up(self):
+ def clean_up(self, surrounding_scene = None):
self.update(1)
+ if self.is_remover() and surrounding_scene is not None:
+ surrounding_scene.remove(self.mobject)
+ return self
def sync_animation_run_times_and_rate_funcs(*animations, **kwargs):
diff --git a/animation/transform.py b/animation/transform.py
index 40ae0e5b7..739576418 100644
--- a/animation/transform.py
+++ b/animation/transform.py
@@ -55,6 +55,12 @@ class Transform(Animation):
submob.interpolate(start, end, alpha, self.path_func)
return self
+ def clean_up(self, surrounding_scene = None):
+ Animation.clean_up(self, surrounding_scene)
+ if self.replace_mobject_with_target_in_scene and surrounding_scene is not None:
+ surrounding_scene.remove(self.mobject)
+ surrounding_scene.add(self.original_target_mobject)
+
class ReplacementTransform(Transform):
CONFIG = {
"replace_mobject_with_target_in_scene" : True,
@@ -147,7 +153,8 @@ class FadeOut(Transform):
target.set_fill(opacity = 0)
Transform.__init__(self, mobject, target, **kwargs)
- def clean_up(self):
+ def clean_up(self, surrounding_scene = None):
+ Transform.clean_up(self, surrounding_scene)
self.update(0)
class FadeIn(Transform):
diff --git a/scene/scene.py b/scene/scene.py
index 1c652b9a7..28a6f84b5 100644
--- a/scene/scene.py
+++ b/scene/scene.py
@@ -17,7 +17,7 @@ from tk_scene import TkSceneRoot
from mobject import Mobject, VMobject
from animation import Animation
from animation.animation import sync_animation_run_times_and_rate_funcs
-from animation.transform import MoveToTarget, Transform
+from animation.transform import MoveToTarget
class Scene(object):
CONFIG = {
@@ -142,6 +142,20 @@ class Scene(object):
lambda m : m not in mobjects_to_remove,
self.mobjects
)
+ self.remove_mobjects_without_family_on_screen()
+ return self
+
+ def remove_mobjects_without_family_on_screen(self):
+ def should_keep(mobject):
+ if mobject.get_num_points() > 0:
+ return True
+ num_family_members_among_mobjects = sum([
+ submob in self.mobjects
+ for submob in mobject.submobject_family()
+ ])
+ return num_family_members_among_mobjects > 1
+
+ self.mobjects = filter(should_keep, self.mobjects)
return self
def bring_to_front(self, *mobjects):
@@ -266,13 +280,7 @@ class Scene(object):
def clean_up_animations(self, *animations):
for animation in animations:
- animation.clean_up()
- if animation.is_remover():
- self.remove(animation.mobject)
- if isinstance(animation, Transform) :
- if animation.replace_mobject_with_target_in_scene:
- self.remove(animation.mobject)
- self.add(animation.original_target_mobject)
+ animation.clean_up(self)
return self
def get_mobjects_from_last_animation(self):
diff --git a/topics/characters.py b/topics/characters.py
index 7cc7c4ddc..373e8e438 100644
--- a/topics/characters.py
+++ b/topics/characters.py
@@ -329,6 +329,7 @@ class RemovePiCreatureBubble(AnimationGroup):
CONFIG = {
"target_mode" : "plain",
"look_at_arg" : None,
+ "remover" : True,
}
def __init__(self, pi_creature, **kwargs):
assert hasattr(pi_creature, "bubble")
@@ -346,9 +347,12 @@ class RemovePiCreatureBubble(AnimationGroup):
FadeOut(pi_creature.bubble.content),
)
- def clean_up(self):
- AnimationGroup.clean_up(self)
+ def clean_up(self, surrounding_scene = None):
+ AnimationGroup.clean_up(self, surrounding_scene)
self.pi_creature.bubble = None
+ if surrounding_scene is not None:
+ surrounding_scene.add(self.pi_creature)
+
class PiCreatureScene(Scene):