clean_up -> clean_up_from_scene

This commit is contained in:
Grant Sanderson 2019-02-08 11:00:04 -08:00
parent f4ddbef136
commit bf3d9c9b5a
10 changed files with 80 additions and 76 deletions

View file

@ -92,9 +92,9 @@ class ChangeProbability(Animation):
self.mobject.add(self.mobject.prob_rect, self.mobject.prob_label) self.mobject.add(self.mobject.prob_rect, self.mobject.prob_label)
def clean_up(self, surrounding_scene=None): def clean_up_from_scene(self, scene=None):
self.mobject.p = self.p1 self.mobject.p = self.p1
super(ChangeProbability, self).clean_up(surrounding_scene = surrounding_scene) super(ChangeProbability, self).clean_up_from_scene(scene = scene)

View file

@ -280,14 +280,14 @@ class FlashThroughHistogram(Animation):
self.mobject.remove(self.prototype_cell) self.mobject.remove(self.prototype_cell)
def clean_up(self, surrounding_scene = None): def clean_up_from_scene(self, scene = None):
Animation.clean_up(self, surrounding_scene) Animation.clean_up_from_scene(self, scene)
self.update(1) self.update(1)
if surrounding_scene is not None: if scene is not None:
if self.is_remover(): if self.is_remover():
surrounding_scene.remove(self.prototype_cell) scene.remove(self.prototype_cell)
else: else:
surrounding_scene.add(self.prototype_cell) scene.add(self.prototype_cell)
return self return self

View file

@ -15,8 +15,7 @@ class Animation(object):
"name": None, "name": None,
# Does this animation add or remove a mobject form the screen # Does this animation add or remove a mobject form the screen
"remover": False, "remover": False,
# Options are lagged_start, smoothed_lagged_start, # TODO, replace this with a single lag parameter
# one_at_a_time, all_at_once
"submobject_mode": "all_at_once", "submobject_mode": "all_at_once",
"lag_factor": 2, "lag_factor": 2,
# Used by EmptyAnimation to announce itself ignorable # Used by EmptyAnimation to announce itself ignorable
@ -26,24 +25,31 @@ class Animation(object):
def __init__(self, mobject, **kwargs): def __init__(self, mobject, **kwargs):
assert(isinstance(mobject, Mobject)) assert(isinstance(mobject, Mobject))
digest_config(self, kwargs, locals()) self.mobject = mobject
digest_config(self, kwargs)
self.all_families_zipped = self.get_all_families_zipped()
def begin(self):
mobject = self.mobject
# Make sure it's all up to date # Make sure it's all up to date
mobject.update() mobject.update()
mobject.suspend_updating()
# Keep track of where it started # Keep track of where it started
self.starting_mobject = self.mobject.copy() self.starting_mobject = mobject.copy()
if self.name is None:
self.name = self.__class__.__name__ + str(self.mobject)
self.all_families_zipped = self.get_all_families_zipped()
self.update(0) self.update(0)
def begin_animation(self): def finish(self):
pass self.mobject.resume_updating()
def end_animation(self): def clean_up_from_scene(self, scene):
pass if self.is_remover():
scene.remove(self.mobject)
return self
def __str__(self): def __str__(self):
return self.name if self.name:
return self.name
return self.__class__.__name__ + str(self.mobject)
def copy(self): def copy(self):
return deepcopy(self) return deepcopy(self)
@ -63,22 +69,6 @@ class Animation(object):
self.update_submobject(*list(mobs) + [sub_alpha]) self.update_submobject(*list(mobs) + [sub_alpha])
return self return self
def update_submobject(self, submobject, starting_sumobject, alpha):
# Typically ipmlemented by subclass
pass
def get_all_mobjects(self):
"""
Ordering must match the ording of arguments to update_submobject
"""
return self.mobject, self.starting_mobject
def get_all_families_zipped(self):
return list(zip(*list(map(
Mobject.family_members_with_points,
self.get_all_mobjects()
))))
def get_sub_alpha(self, alpha, index, num_submobjects): def get_sub_alpha(self, alpha, index, num_submobjects):
if self.submobject_mode in ["lagged_start", "smoothed_lagged_start"]: if self.submobject_mode in ["lagged_start", "smoothed_lagged_start"]:
prop = float(index) / num_submobjects prop = float(index) / num_submobjects
@ -94,6 +84,22 @@ class Animation(object):
return alpha return alpha
raise Exception("Invalid submobject mode") raise Exception("Invalid submobject mode")
def update_submobject(self, submobject, starting_sumobject, alpha):
# Typically ipmlemented by subclass
pass
def get_all_mobjects(self):
"""
Ordering must match the ording of arguments to update_submobject
"""
return self.mobject, self.starting_mobject
def get_all_families_zipped(self):
return list(zip(*map(
Mobject.family_members_with_points,
self.get_all_mobjects()
)))
def filter_out(self, *filter_functions): def filter_out(self, *filter_functions):
self.filter_functions += filter_functions self.filter_functions += filter_functions
return self return self
@ -118,10 +124,3 @@ class Animation(object):
def is_remover(self): def is_remover(self):
return self.remover return self.remover
def clean_up(self, surrounding_scene=None):
self.update(1)
if surrounding_scene is not None:
if self.is_remover():
surrounding_scene.remove(self.mobject)
return self

View file

@ -103,7 +103,7 @@ class Succession(Animation):
self.scene_mobjects_at_time[0] = Group() self.scene_mobjects_at_time[0] = Group()
for i in range(self.num_anims): for i in range(self.num_anims):
self.scene_mobjects_at_time[i + 1] = self.scene_mobjects_at_time[i].copy() self.scene_mobjects_at_time[i + 1] = self.scene_mobjects_at_time[i].copy()
self.animations[i].clean_up(self.scene_mobjects_at_time[i + 1]) self.animations[i].clean_up_from_scene(self.scene_mobjects_at_time[i + 1])
self.current_alpha = 0 self.current_alpha = 0
# If self.num_anims == 0, this is an invalid index, but so it goes # If self.num_anims == 0, this is an invalid index, but so it goes
@ -168,11 +168,11 @@ class Succession(Animation):
self.animations[i].update(sub_alpha) self.animations[i].update(sub_alpha)
self.current_alpha = alpha self.current_alpha = alpha
def clean_up(self, *args, **kwargs): def clean_up_from_scene(self, *args, **kwargs):
# We clean up as though we've played ALL animations, even if # We clean up as though we've played ALL animations, even if
# clean_up is called in middle of things # clean_up is called in middle of things
for anim in self.animations: for anim in self.animations:
anim.clean_up(*args, **kwargs) anim.clean_up_from_scene(*args, **kwargs)
class AnimationGroup(Animation): class AnimationGroup(Animation):
@ -197,9 +197,9 @@ class AnimationGroup(Animation):
for anim in self.sub_anims: for anim in self.sub_anims:
anim.update(alpha * self.run_time / anim.run_time) anim.update(alpha * self.run_time / anim.run_time)
def clean_up(self, *args, **kwargs): def clean_up_from_scene(self, *args, **kwargs):
for anim in self.sub_anims: for anim in self.sub_anims:
anim.clean_up(*args, **kwargs) anim.clean_up_from_scene(*args, **kwargs)
def update_config(self, **kwargs): def update_config(self, **kwargs):
Animation.update_config(self, **kwargs) Animation.update_config(self, **kwargs)
@ -251,9 +251,9 @@ class LaggedStart(Animation):
anim.update(alpha) anim.update(alpha)
return self return self
def clean_up(self, *args, **kwargs): def clean_up_from_scene(self, *args, **kwargs):
for anim in self.subanimations: for anim in self.subanimations:
anim.clean_up(*args, **kwargs) anim.clean_up_from_scene(*args, **kwargs)
class ApplyToCenters(Animation): class ApplyToCenters(Animation):

View file

@ -137,8 +137,8 @@ class FadeOut(Transform):
target.fade(1) target.fade(1)
Transform.__init__(self, mobject, target, **kwargs) Transform.__init__(self, mobject, target, **kwargs)
def clean_up(self, surrounding_scene=None): def clean_up_from_scene(self, scene=None):
Transform.clean_up(self, surrounding_scene) Transform.clean_up_from_scene(self, scene)
self.update(0) self.update(0)

View file

@ -132,8 +132,8 @@ class ShowPassingFlash(ShowPartial):
lower = max(lower, 0) lower = max(lower, 0)
return (lower, upper) return (lower, upper)
def clean_up(self, *args, **kwargs): def clean_up_from_scene(self, *args, **kwargs):
ShowPartial.clean_up(self, *args, **kwargs) ShowPartial.clean_up_from_scene(self, *args, **kwargs)
for submob, start_submob in self.get_all_families_zipped(): for submob, start_submob in self.get_all_families_zipped():
submob.pointwise_become_partial(start_submob, 0, 1) submob.pointwise_become_partial(start_submob, 0, 1)

View file

@ -7,7 +7,6 @@ from manimlib.constants import *
from manimlib.mobject.mobject import Group from manimlib.mobject.mobject import Group
from manimlib.mobject.mobject import Mobject from manimlib.mobject.mobject import Mobject
from manimlib.utils.config_ops import digest_config from manimlib.utils.config_ops import digest_config
from manimlib.utils.config_ops import instantiate
from manimlib.utils.iterables import adjacent_pairs from manimlib.utils.iterables import adjacent_pairs
from manimlib.utils.paths import path_along_arc from manimlib.utils.paths import path_along_arc
from manimlib.utils.paths import straight_path from manimlib.utils.paths import straight_path
@ -65,12 +64,12 @@ class Transform(Animation):
submob.interpolate(start, end, alpha, self.path_func) submob.interpolate(start, end, alpha, self.path_func)
return self return self
def clean_up(self, surrounding_scene=None): def clean_up_from_scene(self, scene=None):
Animation.clean_up(self, surrounding_scene) Animation.clean_up_from_scene(self, scene)
if self.replace_mobject_with_target_in_scene and surrounding_scene is not None: if self.replace_mobject_with_target_in_scene and scene is not None:
surrounding_scene.remove(self.mobject) scene.remove(self.mobject)
if not self.remover: if not self.remover:
surrounding_scene.add(self.original_target_mobject) scene.add(self.original_target_mobject)
class ReplacementTransform(Transform): class ReplacementTransform(Transform):

View file

@ -93,11 +93,11 @@ class RemovePiCreatureBubble(AnimationGroup):
FadeOut(pi_creature.bubble.content), FadeOut(pi_creature.bubble.content),
) )
def clean_up(self, surrounding_scene=None): def clean_up_from_scene(self, scene=None):
AnimationGroup.clean_up(self, surrounding_scene) AnimationGroup.clean_up_from_scene(self, scene)
self.pi_creature.bubble = None self.pi_creature.bubble = None
if surrounding_scene is not None: if scene is not None:
surrounding_scene.add(self.pi_creature) scene.add(self.pi_creature)
class FlashThroughClass(Animation): class FlashThroughClass(Animation):

View file

@ -458,17 +458,20 @@ class Scene(Container):
return return
animations = self.compile_play_args_to_animation_list(*args) animations = self.compile_play_args_to_animation_list(*args)
curr_mobjects = self.get_mobject_family_members()
for animation in animations: for animation in animations:
# This is where kwargs to play like run_time and rate_func # This is where kwargs to play like run_time and rate_func
# get applied to all animations # get applied to all animations
animation.update_config(**kwargs) animation.update_config(**kwargs)
# Anything animated that's not already in the # Anything animated that's not already in the
# scene gets added to the scene # scene gets added to the scene
if animation.mobject not in self.get_mobject_family_members(): mob = animation.mobject
self.add(animation.mobject) if mob not in curr_mobjects:
# Don't call the update functions of a mobject self.add(mob)
# being animated curr_mobjects += mob.get_family()
animation.mobject.suspend_updating() # Begin animation
animation.begin()
moving_mobjects = self.get_moving_mobjects(*animations) moving_mobjects = self.get_moving_mobjects(*animations)
# Paint all non-moving objects onto the screen, so they don't # Paint all non-moving objects onto the screen, so they don't
@ -482,10 +485,14 @@ class Scene(Container):
self.continual_update(dt) self.continual_update(dt)
self.update_frame(moving_mobjects, static_image) self.update_frame(moving_mobjects, static_image)
self.add_frames(self.get_frame()) self.add_frames(self.get_frame())
self.mobjects_from_last_animation = [ self.mobjects_from_last_animation = [
anim.mobject for anim in animations anim.mobject for anim in animations
] ]
self.clean_up_animations(*animations) for animation in animations:
animation.finish()
if self.skip_animations: if self.skip_animations:
self.continual_update(self.get_run_time(animations)) self.continual_update(self.get_run_time(animations))
else: else:
@ -498,8 +505,7 @@ class Scene(Container):
def clean_up_animations(self, *animations): def clean_up_animations(self, *animations):
for animation in animations: for animation in animations:
animation.clean_up(self) animation.clean_up_from_scene(self)
animation.mobject.resume_updating()
return self return self
def get_mobjects_from_last_animation(self): def get_mobjects_from_last_animation(self):

View file

@ -79,11 +79,11 @@ class SlidingBlocks(VGroup):
"collect_clack_data": True, "collect_clack_data": True,
} }
def __init__(self, surrounding_scene, **kwargs): def __init__(self, scene, **kwargs):
VGroup.__init__(self, **kwargs) VGroup.__init__(self, **kwargs)
self.surrounding_scene = surrounding_scene self.scene = scene
self.floor = surrounding_scene.floor self.floor = scene.floor
self.wall = surrounding_scene.wall self.wall = scene.wall
self.block1 = self.get_block(**self.block1_config) self.block1 = self.get_block(**self.block1_config)
self.block2 = self.get_block(**self.block2_config) self.block2 = self.get_block(**self.block2_config)
@ -159,7 +159,7 @@ class SlidingBlocks(VGroup):
DR, DR,
) )
self.surrounding_scene.update_num_clacks(n_clacks) self.scene.update_num_clacks(n_clacks)
def get_clack_data(self): def get_clack_data(self):
ps_point = self.phase_space_point_tracker.get_location() ps_point = self.phase_space_point_tracker.get_location()