A community-maintained Python framework for creating mathematical animations.
Finished SceneFileWriter refactor
Grant Sandersone5e1fa908bed
diff --git a/README.md b/README.mdindex 72846d994..69300a70c 100644--- a/README.md+++ b/README.md@@ -60,7 +60,7 @@ Set MEDIA_DIR environment variable to determine where image and animation filesLook through the old_projects folder to see the code for previous 3b1b videos. Note, however, that developments are often made to the library without considering backwards compatibility on those old_projects. To run them with a guarantee that they will work, you will have to go back to the commit which complete that project.-While developing a scene, the `-s` flag is helpful to just see what things look like at the end without having to generate the full animation. It can also be helpful to use the `-n` flag to skip over some number of animations.+While developing a scene, the `-sp` flags are helpful to just see what things look like at the end without having to generate the full animation. It can also be helpful to use the `-n` flag to skip over some number of animations.### DocumentationDocumentation is in progress at [manim.readthedocs.io](https://manim.readthedocs.io).diff --git a/active_projects/clacks/question/480p15/NameIntro.mp4 b/active_projects/clacks/question/480p15/NameIntro.mp4deleted file mode 100644index ac3fb91eb..000000000Binary files a/active_projects/clacks/question/480p15/NameIntro.mp4 and /dev/null differdiff --git a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00000.mp4 b/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00000.mp4deleted file mode 100644index aca9e20f1..000000000Binary files a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00000.mp4 and /dev/null differdiff --git a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00001.mp4 b/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00001.mp4deleted file mode 100644index fde665b6d..000000000Binary files a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00001.mp4 and /dev/null differdiff --git a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00002.mp4 b/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00002.mp4deleted file mode 100644index b94f67a7a..000000000Binary files a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00002.mp4 and /dev/null differdiff --git a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00003.mp4 b/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00003.mp4deleted file mode 100644index 4b87ccf12..000000000Binary files a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00003.mp4 and /dev/null differdiff --git a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/partial_movie_file_list.txt b/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/partial_movie_file_list.txtdeleted file mode 100644index d98edb468..000000000--- a/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/partial_movie_file_list.txt+++ /dev/null@@ -1,4 +0,0 @@-file '/Users/grant/cs/manim/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00000.mp4'-file '/Users/grant/cs/manim/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00001.mp4'-file '/Users/grant/cs/manim/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00002.mp4'-file '/Users/grant/cs/manim/active_projects/clacks/question/480p15/partial_movie_directory/NameIntro/00003.mp4'diff --git a/example_scenes.py b/example_scenes.pyindex d57ad5e58..d05e65954 100644--- a/example_scenes.py+++ b/example_scenes.py@@ -7,8 +7,9 @@ from big_ol_pile_of_manim_imports import *## Use the flat -l for a faster rendering at a lower# quality.-# Use -s to skip to the end and just show the final frame-# Use the -p to have the animation pop up once done.+# Use -s to skip to the end and just save the final frame+# Use the -p to have the animation (or image, if -s was+# used) pop up once done.# Use -n <number> to skip ahead to the n'th animation of a scene.diff --git a/manimlib/config.py b/manimlib/config.pyindex ee632759c..db415cefc 100644--- a/manimlib/config.py+++ b/manimlib/config.py@@ -25,7 +25,7 @@ def parse_cli():parser.add_argument("-p", "--preview",action="store_true",- help="Automatically open movie file once its done",+ help="Automatically open the saved file once its done",),parser.add_argument("-w", "--write_to_movie",@@ -35,7 +35,7 @@ def parse_cli():parser.add_argument("-s", "--save_last_frame",action="store_true",- help="Save the last frame and open the image file",+ help="Save the last frame",),parser.add_argument("-l", "--low_quality",diff --git a/manimlib/scene/scene.py b/manimlib/scene/scene.pyindex c0b0aed9a..009bfd981 100644--- a/manimlib/scene/scene.py+++ b/manimlib/scene/scene.py@@ -33,7 +33,6 @@ class Scene(Container):}def __init__(self, **kwargs):- # Perhaps allow passing in a non-empty *mobjects parameter?Container.__init__(self, **kwargs)self.camera = self.camera_class(**self.camera_config)self.file_writer = SceneFileWriter(@@ -42,6 +41,7 @@ class Scene(Container):self.mobjects = []self.continual_animations = []+ # TODO, remove need for foreground mobjectsself.foreground_mobjects = []self.num_plays = 0self.time = 0@@ -59,16 +59,6 @@ class Scene(Container):self.file_writer.finish()self.print_end_message()- def handle_play_like_call(func):- def wrapper(self, *args, **kwargs):- self.handle_animation_skipping()- allow_write = not self.skip_animations- self.file_writer.begin_animation(allow_write)- func(self, *args, **kwargs)- self.file_writer.end_animation(allow_write)- self.num_plays += 1- return wrapper-def setup(self):"""This is meant to be implement by any scenes which@@ -111,8 +101,6 @@ class Scene(Container):def get_attrs(self, *keys):return [getattr(self, key) for key in keys]- # TODO, Scene file writer now handles sound-# Only these methods should touch the cameradef set_camera(self, camera):self.camera = camera@@ -449,7 +437,7 @@ class Scene(Container):compile_method(state)return animations- def handle_animation_skipping(self):+ def update_skipping_status(self):if self.start_at_animation_number:if self.num_plays == self.start_at_animation_number:self.skip_animations = False@@ -458,6 +446,16 @@ class Scene(Container):self.skip_animations = Trueraise EndSceneEarlyException()+ def handle_play_like_call(func):+ def wrapper(self, *args, **kwargs):+ self.update_skipping_status()+ allow_write = not self.skip_animations+ self.file_writer.begin_animation(allow_write)+ func(self, *args, **kwargs)+ self.file_writer.end_animation(allow_write)+ self.num_plays += 1+ return wrapper+@handle_play_like_calldef play(self, *args, **kwargs):if len(args) == 0:@@ -496,7 +494,6 @@ class Scene(Container):return self- # TODOdef idle_stream(self):self.file_writer.idle_stream()@@ -569,6 +566,10 @@ class Scene(Container):for frame in frames:self.file_writer.write_frame(frame)+ def add_sound(self, sound_file, time_offset=0):+ time = self.get_time() + time_offset+ self.file_writer.add_sound(sound_file, time)+def show_frame(self):self.update_frame(ignore_skipping=True)self.get_image().show()diff --git a/manimlib/scene/scene_file_writer.py b/manimlib/scene/scene_file_writer.pyindex 4cc56beff..a4fd88789 100644--- a/manimlib/scene/scene_file_writer.py+++ b/manimlib/scene/scene_file_writer.py@@ -16,6 +16,7 @@ from manimlib.utils.config_ops import digest_configfrom manimlib.utils.file_ops import guarantee_existancefrom manimlib.utils.file_ops import add_extension_if_not_presentfrom manimlib.utils.file_ops import get_sorted_integer_files+from manimlib.utils.sounds import get_full_sound_file_pathclass SceneFileWriter(object):@@ -38,12 +39,11 @@ class SceneFileWriter(object):def __init__(self, scene, **kwargs):digest_config(self, kwargs)self.scene = scene- self.init_audio()- self.init_output_directories()self.stream_lock = False+ self.init_output_directories()+ self.init_audio()# Output directories and files-def init_output_directories(self):output_directory = self.output_directory or self.get_default_output_directory()file_name = self.file_name or self.get_default_file_name()@@ -59,6 +59,7 @@ class SceneFileWriter(object):)if self.write_to_movie:movie_dir = guarantee_existance(os.path.join(+ VIDEO_DIR,output_directory,self.get_movie_directory(),))@@ -94,25 +95,42 @@ class SceneFileWriter(object):def get_partial_movie_directory(self):return "partial_movie_directory"+ # Directory getters+ def get_image_file_path(self):+ return self.image_file_path++ def get_next_partial_movie_path(self):+ result = os.path.join(+ self.partial_movie_directory,+ "{:05}{}".format(+ self.scene.num_plays,+ self.movie_file_extension,+ )+ )+ return result++ def get_movie_file_path(self):+ return self.movie_file_path+# Sound- # TODO, make work with Scenedef init_audio(self):self.includes_sound = Falsedef create_audio_segment(self):self.audio_segment = AudioSegment.silent()- def add_audio_segment(self, new_segment, time_offset=0):+ def add_audio_segment(self, new_segment, time=None):if not self.includes_sound:self.includes_sound = Trueself.create_audio_segment()segment = self.audio_segment- overly_time = self.get_time() + time_offset- if overly_time < 0:+ curr_end = segment.duration_seconds+ if time is None:+ time = curr_end+ if time < 0:raise Exception("Adding sound at timestamp < 0")- curr_end = segment.duration_seconds- new_end = overly_time + new_segment.duration_seconds+ new_end = time + new_segment.duration_secondsdiff = new_end - curr_endif diff > 0:segment = segment.append(@@ -120,40 +138,15 @@ class SceneFileWriter(object):crossfade=0,)self.audio_segment = segment.overlay(- new_segment, position=int(1000 * overly_time)- )-- def add_sound(self, sound_file, time_offset=0):- new_segment = AudioSegment.from_file(sound_file)- self.add_audio_segment(new_segment, 0)-- # Directory getters- def get_image_file_path(self):- return self.image_file_path-- def get_next_partial_movie_path(self):- result = os.path.join(- self.partial_movie_directory,- "{:05}{}".format(- self.scene.num_plays,- self.movie_file_extension,- )+ new_segment, position=int(1000 * time))- return result- def get_movie_file_path(self):- return self.movie_file_path+ def add_sound(self, sound_file, time):+ file_path = get_full_sound_file_path(sound_file)+ new_segment = AudioSegment.from_file(file_path)+ self.add_audio_segment(new_segment, time)# Writers- def write_frame(self, frame):- if self.write_to_movie:- self.writing_process.stdin.write(frame.tostring())-- def save_image(self, image):- file_path = self.get_image_file_path()- image.save(file_path)- self.print_file_ready_message(file_path)-def begin_animation(self, allow_write=False):if self.write_to_movie and allow_write:self.open_movie_pipe()@@ -167,6 +160,15 @@ class SceneFileWriter(object):self.stream_lock = Truethread.start_new_thread(self.idle_stream, ())+ def write_frame(self, frame):+ if self.write_to_movie:+ self.writing_process.stdin.write(frame.tostring())++ def save_image(self, image):+ file_path = self.get_image_file_path()+ image.save(file_path)+ self.print_file_ready_message(file_path)+def idle_stream(self):while self.stream_lock:a = datetime.datetime.now()diff --git a/manimlib/utils/file_ops.py b/manimlib/utils/file_ops.pyindex c06bd148c..36a21e4e2 100644--- a/manimlib/utils/file_ops.py+++ b/manimlib/utils/file_ops.py@@ -16,38 +16,16 @@ def guarantee_existance(path):return os.path.abspath(path)-# def get_scene_output_directory(scene_class):-# return guarantee_existance(os.path.join(-# VIDEO_DIR,-# scene_class.__module__.replace(".", os.path.sep)-# ))---# def get_movie_output_directory(scene_class, camera_config, frame_duration):-# directory = get_scene_output_directory(scene_class)-# sub_dir = "%dp%d" % (-# camera_config["pixel_height"],-# int(1.0 / frame_duration)-# )-# return guarantee_existance(os.path.join(directory, sub_dir))---# def get_partial_movie_output_directory(scene, camera_config, frame_duration):-# directory = get_movie_output_directory(-# scene.__class__, camera_config, frame_duration-# )-# return guarantee_existance(-# os.path.join(-# directory,-# "partial_movie_files",-# scene.get_output_file_name(),-# )-# )---# def get_image_output_directory(scene_class, sub_dir="images"):-# directory = get_scene_output_directory(scene_class)-# return guarantee_existance(os.path.join(directory, sub_dir))+def seek_full_path_from_defaults(file_name, default_dir, extensions):+ possible_paths = [file_name]+ possible_paths += [+ os.path.join(default_dir, file_name + extension)+ for extension in ["", *extensions]+ ]+ for path in possible_paths:+ if os.path.exists(path):+ return path+ raise IOError("File {} not Found".format(file_name))def get_sorted_integer_files(directory,diff --git a/manimlib/utils/images.py b/manimlib/utils/images.pyindex 57d089b4d..63dcc58b4 100644--- a/manimlib/utils/images.py+++ b/manimlib/utils/images.py@@ -4,20 +4,15 @@ import osfrom PIL import Imagefrom manimlib.constants import RASTER_IMAGE_DIR+from manimlib.utils.file_ops import seek_full_path_from_defaultsdef get_full_raster_image_path(image_file_name):- possible_paths = [+ return seek_full_path_from_defaults(image_file_name,- os.path.join(RASTER_IMAGE_DIR, image_file_name),- os.path.join(RASTER_IMAGE_DIR, image_file_name + ".jpg"),- os.path.join(RASTER_IMAGE_DIR, image_file_name + ".png"),- os.path.join(RASTER_IMAGE_DIR, image_file_name + ".gif"),- ]- for path in possible_paths:- if os.path.exists(path):- return path- raise IOError("File %s not Found" % image_file_name)+ default_dir=RASTER_IMAGE_DIR,+ extensions=[".jpg", ".png", ".gif"]+ )def drag_pixels(frames):diff --git a/manimlib/utils/sounds.py b/manimlib/utils/sounds.pyindex e9d3dc7ec..17a185cee 100644--- a/manimlib/utils/sounds.py+++ b/manimlib/utils/sounds.py@@ -1,4 +1,6 @@import os+from manimlib.constants import SOUND_DIR+from manimlib.utils.file_ops import seek_full_path_from_defaultsdef play_chord(*nums):@@ -28,3 +30,11 @@ def play_error_sound():def play_finish_sound():play_chord(12, 9, 5, 2)+++def get_full_sound_file_path(sound_file_name):+ return seek_full_path_from_defaults(+ sound_file_name,+ default_dir=SOUND_DIR,+ extensions=[".wav", ".mp3"]+ )