From ebd4016fb33e6be89d8a544f3f0e16db6e5a5d5e Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Tue, 11 Feb 2020 19:52:14 -0800 Subject: [PATCH] Small refactor of extract_scene et. al. --- manim.py | 2 - manimlib/__init__.py | 16 +++--- manimlib/config.py | 31 +++++++----- manimlib/extract_scene.py | 101 ++++++++------------------------------ 4 files changed, 45 insertions(+), 105 deletions(-) diff --git a/manim.py b/manim.py index 2f659056..2bebaea6 100755 --- a/manim.py +++ b/manim.py @@ -3,5 +3,3 @@ import manimlib if __name__ == "__main__": manimlib.main() -else: - manimlib.stream_starter.start_livestream() diff --git a/manimlib/__init__.py b/manimlib/__init__.py index 5eb9e228..4c8d7331 100644 --- a/manimlib/__init__.py +++ b/manimlib/__init__.py @@ -2,17 +2,13 @@ import manimlib.config import manimlib.constants import manimlib.extract_scene -import manimlib.stream_starter def main(): args = manimlib.config.parse_cli() - if not args.livestream: - config = manimlib.config.get_configuration(args) - manimlib.constants.initialize_directories(config) - manimlib.extract_scene.main(config) - else: - manimlib.stream_starter.start_livestream( - to_twitch=args.to_twitch, - twitch_key=args.twitch_key, - ) + config = manimlib.config.get_configuration(args) + manimlib.constants.initialize_directories(config) + scenes = manimlib.extract_scene.main(config) + + for scene in scenes: + scene.run() diff --git a/manimlib/config.py b/manimlib/config.py index 7518d14d..38043b90 100644 --- a/manimlib/config.py +++ b/manimlib/config.py @@ -83,9 +83,13 @@ def parse_cli(): help="Write all the scenes from a file", ), parser.add_argument( - "-o", "--file_name", - help="Specify the name of the output file, if" - "it should be different from the scene class name", + "-o", "--open", + action="store_true", + help="Automatically open the saved file once its done", + ) + parser.add_argument( + "--file_name", + help="Name for the movie or image file", ) parser.add_argument( "-n", "--start_at_animation_number", @@ -102,11 +106,6 @@ def parse_cli(): "-c", "--color", help="Background color", ) - parser.add_argument( - "--sound", - action="store_true", - help="Play a success/failure sound", - ) parser.add_argument( "--leave_progress_bars", action="store_true", @@ -185,7 +184,7 @@ def get_configuration(args): module = get_module(args.file) file_writer_config = { # By default, write to file - "write_to_movie": args.write_to_movie or not args.save_last_frame, + "write_to_movie": args.write_to_movie or args.open or args.show_file_in_finder, "save_last_frame": args.save_last_frame, "save_pngs": args.save_pngs, "save_as_gif": args.save_as_gif, @@ -194,21 +193,21 @@ def get_configuration(args): "movie_file_extension": ".mov" if args.transparent else ".mp4", "file_name": args.file_name, "input_file_path": args.file, + "open_file_upon_completion": args.open, + "show_file_location_upon_completion": args.show_file_in_finder, + "quiet": args.quiet, } if hasattr(module, "OUTPUT_DIRECTORY"): file_writer_config["output_directory"] = module.OUTPUT_DIRECTORY config = { "module": module, "scene_names": args.scene_names, - "open_video_upon_completion": args.preview, - "show_file_in_finder": args.show_file_in_finder, + "preview": args.preview, "file_writer_config": file_writer_config, "quiet": args.quiet or args.write_all, - "ignore_waits": args.preview, "write_all": args.write_all, "start_at_animation_number": args.start_at_animation_number, "end_at_animation_number": None, - "sound": args.sound, "leave_progress_bars": args.leave_progress_bars, "media_dir": args.media_dir, "video_dir": args.video_dir, @@ -218,6 +217,12 @@ def get_configuration(args): # Camera configuration config["camera_config"] = get_camera_configuration(args) + config["window_config"] = { + "size": ( + config["camera_config"]["pixel_width"], + config["camera_config"]["pixel_height"], + ) + } # Arguments related to skipping stan = config["start_at_animation_number"] diff --git a/manimlib/extract_scene.py b/manimlib/extract_scene.py index 7185f6eb..a70c7cff 100644 --- a/manimlib/extract_scene.py +++ b/manimlib/extract_scene.py @@ -1,62 +1,11 @@ import inspect import itertools as it -import os -import platform -import subprocess as sp import sys -import traceback from manimlib.scene.scene import Scene -from manimlib.utils.sounds import play_error_sound -from manimlib.utils.sounds import play_finish_sound import manimlib.constants -def open_file_if_needed(file_writer, **config): - if config["quiet"]: - curr_stdout = sys.stdout - sys.stdout = open(os.devnull, "w") - - open_file = any([ - config["open_video_upon_completion"], - config["show_file_in_finder"] - ]) - if open_file: - current_os = platform.system() - file_paths = [] - - if config["file_writer_config"]["save_last_frame"]: - file_paths.append(file_writer.get_image_file_path()) - if config["file_writer_config"]["write_to_movie"]: - file_paths.append(file_writer.get_movie_file_path()) - - for file_path in file_paths: - if current_os == "Windows": - os.startfile(file_path) - else: - commands = [] - if current_os == "Linux": - commands.append("xdg-open") - elif current_os.startswith("CYGWIN"): - commands.append("cygstart") - else: # Assume macOS - commands.append("open") - - if config["show_file_in_finder"]: - commands.append("-R") - - commands.append(file_path) - - # commands.append("-g") - FNULL = open(os.devnull, 'w') - sp.call(commands, stdout=FNULL, stderr=sp.STDOUT) - FNULL.close() - - if config["quiet"]: - sys.stdout.close() - sys.stdout = curr_stdout - - def is_child_scene(obj, module): if not inspect.isclass(obj): return False @@ -97,14 +46,31 @@ def get_scenes_to_render(scene_classes, config): if len(scene_classes) == 0: print(manimlib.constants.NO_SCENE_MESSAGE) return [] + + scene_kwargs = dict([ + (key, config[key]) + for key in [ + "window_config", + "camera_config", + "file_writer_config", + "skip_animations", + "start_at_animation_number", + "end_at_animation_number", + "leave_progress_bars", + "preview", + ] + ]) + if config["write_all"]: - return scene_classes + return [sc(**scene_kwargs) for sc in scene_classes] + result = [] for scene_name in config["scene_names"]: found = False for scene_class in scene_classes: if scene_class.__name__ == scene_name: - result.append(scene_class) + scene = scene_class(**scene_kwargs) + result.append(scene) found = True break if not found and (scene_name != ""): @@ -135,33 +101,8 @@ def get_scene_classes_from_module(module): def main(config): module = config["module"] all_scene_classes = get_scene_classes_from_module(module) - scene_classes_to_render = get_scenes_to_render(all_scene_classes, config) - - scene_kwargs = dict([ - (key, config[key]) - for key in [ - "camera_config", - "file_writer_config", - "skip_animations", - "start_at_animation_number", - "end_at_animation_number", - "leave_progress_bars", - ] - ]) - - for SceneClass in scene_classes_to_render: - try: - # By invoking, this renders the full scene - scene = SceneClass(**scene_kwargs) - open_file_if_needed(scene.file_writer, **config) - if config["sound"]: - play_finish_sound() - except Exception: - print("\n\n") - traceback.print_exc() - print("\n\n") - if config["sound"]: - play_error_sound() + scenes = get_scenes_to_render(all_scene_classes, config) + return scenes if __name__ == "__main__":