mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Some preliminary changes to get rid of ContinualAnimation dependencies
This commit is contained in:
parent
7c2f98df0f
commit
e09fa811e5
1 changed files with 43 additions and 66 deletions
|
@ -24,7 +24,7 @@ class Scene(Container):
|
|||
"camera_config": {},
|
||||
"file_writer_config": {},
|
||||
"skip_animations": False,
|
||||
"always_continually_update": False,
|
||||
"always_update_mobjects": False,
|
||||
"random_seed": 0,
|
||||
"start_at_animation_number": None,
|
||||
"end_at_animation_number": None,
|
||||
|
@ -39,7 +39,6 @@ class Scene(Container):
|
|||
)
|
||||
|
||||
self.mobjects = []
|
||||
self.continual_animations = []
|
||||
# TODO, remove need for foreground mobjects
|
||||
self.foreground_mobjects = []
|
||||
self.num_plays = 0
|
||||
|
@ -146,12 +145,11 @@ class Scene(Container):
|
|||
self.clear()
|
||||
###
|
||||
|
||||
def continual_update(self, dt):
|
||||
def update_mobjects(self, dt):
|
||||
for mobject in self.mobjects:
|
||||
mobject.update(dt)
|
||||
for continual_animation in self.continual_animations:
|
||||
continual_animation.update(dt)
|
||||
|
||||
# TODO, remove this, and calls to this
|
||||
def wind_down(self, *continual_animations, **kwargs):
|
||||
wind_down_time = kwargs.get("wind_down_time", 1)
|
||||
for continual_animation in continual_animations:
|
||||
|
@ -161,18 +159,11 @@ class Scene(Container):
|
|||
# keep the relevant mobjects. Better way?
|
||||
self.continual_animations = [ca for ca in self.continual_animations if ca in continual_animations]
|
||||
|
||||
def should_continually_update(self):
|
||||
if self.always_continually_update:
|
||||
return True
|
||||
if len(self.continual_animations) > 0:
|
||||
return True
|
||||
any_time_based_update = any([
|
||||
len(m.get_time_based_updaters()) > 0
|
||||
for m in self.get_mobject_family_members()
|
||||
def should_update_mobjects(self):
|
||||
return self.always_update_mobjects or any([
|
||||
mob.has_time_based_updater()
|
||||
for mob in self.get_mobject_family_members()
|
||||
])
|
||||
if any_time_based_update:
|
||||
return True
|
||||
return False
|
||||
|
||||
###
|
||||
|
||||
|
@ -201,64 +192,52 @@ class Scene(Container):
|
|||
def get_mobject_family_members(self):
|
||||
return self.camera.extract_mobject_family_members(self.mobjects)
|
||||
|
||||
def separate_mobjects_and_continual_animations(self, mobjects_or_continual_animations):
|
||||
mobjects = []
|
||||
continual_animations = []
|
||||
for item in mobjects_or_continual_animations:
|
||||
if isinstance(item, Mobject):
|
||||
mobjects.append(item)
|
||||
elif isinstance(item, ContinualAnimation):
|
||||
mobjects.append(item.mobject)
|
||||
continual_animations.append(item)
|
||||
else:
|
||||
raise Exception("""
|
||||
Adding/Removing something which is
|
||||
not a Mobject or a ContinualAnimation
|
||||
""")
|
||||
return mobjects, continual_animations
|
||||
# def separate_mobjects_and_continual_animations(self, mobjects_or_continual_animations):
|
||||
# mobjects = []
|
||||
# continual_animations = []
|
||||
# for item in mobjects_or_continual_animations:
|
||||
# if isinstance(item, Mobject):
|
||||
# mobjects.append(item)
|
||||
# elif isinstance(item, ContinualAnimation):
|
||||
# mobjects.append(item.mobject)
|
||||
# continual_animations.append(item)
|
||||
# else:
|
||||
# raise Exception("""
|
||||
# Adding/Removing something which is
|
||||
# not a Mobject or a ContinualAnimation
|
||||
# """)
|
||||
# return mobjects, continual_animations
|
||||
|
||||
def add(self, *mobjects_or_continual_animations):
|
||||
def add(self, *mobjects):
|
||||
"""
|
||||
Mobjects will be displayed, from background to foreground,
|
||||
in the order with which they are entered.
|
||||
Mobjects will be displayed, from background to
|
||||
foreground in the order with which they are added.
|
||||
"""
|
||||
mobjects, continual_animations = self.separate_mobjects_and_continual_animations(
|
||||
mobjects_or_continual_animations
|
||||
)
|
||||
mobjects += self.foreground_mobjects
|
||||
self.restructure_mobjects(to_remove=mobjects)
|
||||
self.mobjects += mobjects
|
||||
self.continual_animations += continual_animations
|
||||
return self
|
||||
|
||||
def add_mobjects_among(self, values):
|
||||
"""
|
||||
So a scene can just add all mobjects it's defined up to that point
|
||||
by calling add_mobjects_among(locals().values())
|
||||
This is meant mostly for quick prototyping,
|
||||
e.g. to add all mobjects defined up to a point,
|
||||
call self.add_mobjects_among(locals().values())
|
||||
"""
|
||||
mobjects = [x for x in values if isinstance(x, Mobject)]
|
||||
self.add(*mobjects)
|
||||
self.add(*filter(
|
||||
lambda m: isinstance(m, Mobject),
|
||||
values
|
||||
))
|
||||
return self
|
||||
|
||||
def remove(self, *mobjects_or_continual_animations):
|
||||
mobjects, continual_animations = self.separate_mobjects_and_continual_animations(
|
||||
mobjects_or_continual_animations
|
||||
)
|
||||
|
||||
to_remove = self.camera.extract_mobject_family_members(mobjects)
|
||||
def remove(self, *mobjects):
|
||||
for list_name in "mobjects", "foreground_mobjects":
|
||||
self.restructure_mobjects(mobjects, list_name, False)
|
||||
|
||||
self.continual_animations = [
|
||||
ca for ca in self.continual_animations if ca not in
|
||||
continual_animations and ca.mobject not in to_remove]
|
||||
return self
|
||||
|
||||
def restructure_mobjects(
|
||||
self, to_remove,
|
||||
mobject_list_name="mobjects",
|
||||
extract_families=True
|
||||
):
|
||||
def restructure_mobjects(self, to_remove,
|
||||
mobject_list_name="mobjects",
|
||||
extract_families=True):
|
||||
"""
|
||||
In cases where the scene contains a group, e.g. Group(m1, m2, m3), but one
|
||||
of its submobjects is removed, e.g. scene.remove(m1), the list of mobjects
|
||||
|
@ -287,6 +266,7 @@ class Scene(Container):
|
|||
add_safe_mobjects_from_list(mobjects, set(to_remove))
|
||||
return new_mobjects
|
||||
|
||||
# TODO, remove this, and calls to this
|
||||
def add_foreground_mobjects(self, *mobjects):
|
||||
self.foreground_mobjects = list_update(
|
||||
self.foreground_mobjects,
|
||||
|
@ -317,7 +297,6 @@ class Scene(Container):
|
|||
def clear(self):
|
||||
self.mobjects = []
|
||||
self.foreground_mobjects = []
|
||||
self.continual_animation = []
|
||||
return self
|
||||
|
||||
def get_mobjects(self):
|
||||
|
@ -332,12 +311,10 @@ class Scene(Container):
|
|||
# some kind per frame, return the list from that
|
||||
# point forward.
|
||||
animation_mobjects = [anim.mobject for anim in animations]
|
||||
ca_mobjects = [ca.mobject for ca in self.continual_animations]
|
||||
mobjects = self.get_mobject_family_members()
|
||||
for i, mob in enumerate(mobjects):
|
||||
update_possibilities = [
|
||||
mob in animation_mobjects,
|
||||
mob in ca_mobjects,
|
||||
len(mob.get_updaters()) > 0,
|
||||
mob in self.foreground_mobjects
|
||||
]
|
||||
|
@ -483,7 +460,7 @@ class Scene(Container):
|
|||
animation.update_mobjects(dt)
|
||||
alpha = t / animation.run_time
|
||||
animation.interpolate(alpha)
|
||||
self.continual_update(dt)
|
||||
self.update_mobjects(dt)
|
||||
self.update_frame(moving_mobjects, static_image)
|
||||
self.add_frames(self.get_frame())
|
||||
|
||||
|
@ -495,9 +472,9 @@ class Scene(Container):
|
|||
anim.mobject for anim in animations
|
||||
]
|
||||
if self.skip_animations:
|
||||
self.continual_update(self.get_run_time(animations))
|
||||
self.update_mobjects(self.get_run_time(animations))
|
||||
else:
|
||||
self.continual_update(0)
|
||||
self.update_mobjects(0)
|
||||
|
||||
@handle_play_like_call
|
||||
def play(self, *args, **kwargs):
|
||||
|
@ -545,11 +522,11 @@ class Scene(Container):
|
|||
@handle_play_like_call
|
||||
def wait(self, duration=DEFAULT_WAIT_TIME, stop_condition=None):
|
||||
dt = 1 / self.camera.frame_rate
|
||||
self.continual_update(dt=0) # Any problems with this?
|
||||
if self.should_continually_update():
|
||||
self.update_mobjects(dt=0) # Any problems with this?
|
||||
if self.should_update_mobjects():
|
||||
time_progression = self.get_wait_time_progression(duration, stop_condition)
|
||||
for t in time_progression:
|
||||
self.continual_update(dt)
|
||||
self.update_mobjects(dt)
|
||||
self.update_frame()
|
||||
self.add_frames(self.get_frame())
|
||||
if stop_condition and stop_condition():
|
||||
|
|
Loading…
Add table
Reference in a new issue