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