Last active
November 3, 2023 16:44
-
-
Save abul4fia/ce6a2a543ba6aaf586f60aefb3be3563 to your computer and use it in GitHub Desktop.
Generic sequences of animations run in paralell (for Manim)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sequences import play_sequence_in_background, join_all | |
class Test(Scene): | |
def construct(self): | |
c = Circle() | |
sq = Square().to_corner(UR) | |
seq1 = play_sequence_in_background(self, | |
[ | |
FadeIn(c, run_time=2), | |
ApplyMethod(c.shift, 3*RIGHT, run_time=3), | |
ApplyMethod(c.set_fill, YELLOW, 1, run_time=0.5), | |
FadeOut(c, run_time=1), | |
]) | |
seq2 = play_sequence_in_background(self, | |
[ | |
GrowFromCenter(sq, run_time=2), | |
ApplyMethod(sq.to_edge, DL, run_time=2), | |
ShrinkToCenter(sq, run_time=1), | |
ApplyMethod(c.set_fill, BLUE, 1, run_time=0.001), | |
FadeIn(c, run_time=3) | |
]) | |
self.add_updater(seq1) | |
self.wait(3) | |
self.add_updater(seq2) | |
self.wait(100, stop_condition=join_all(self, seq1, seq2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def play_sequence_in_background(scene, anims): | |
current_anim = None | |
mobj_updater = None | |
def updater(dt): | |
nonlocal current_anim, mobj_updater | |
if (current_anim is None | |
or mobj_updater not in current_anim.mobject.updaters): | |
if current_anim: | |
current_anim.finish() | |
current_anim.clean_up_from_scene(scene) | |
if not anims: | |
scene.remove_updater(updater) | |
return | |
current_anim = anims.pop(0) | |
scene.add(current_anim.mobject) | |
turn_animation_into_updater(current_anim) | |
mobj_updater = current_anim.mobject.updaters[-1] | |
return updater | |
def join_all(scene, *updaters): | |
def condition(): | |
return not (set(updaters) & set(scene.updaters)) | |
return condition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is brilliant. Would it be possible to add a LaggedStart as one of the animations in the seqyabce to be played?
For example, after line12:
FadeOut(c, run_time=1),
would it be possible to play theLaggedStart(*anims)
in the example below