Small refactor of extract_scene et. al.

This commit is contained in:
Grant Sanderson 2020-02-11 19:52:14 -08:00
parent 960e918e61
commit ebd4016fb3
4 changed files with 45 additions and 105 deletions

View file

@ -3,5 +3,3 @@ import manimlib
if __name__ == "__main__": if __name__ == "__main__":
manimlib.main() manimlib.main()
else:
manimlib.stream_starter.start_livestream()

View file

@ -2,17 +2,13 @@
import manimlib.config import manimlib.config
import manimlib.constants import manimlib.constants
import manimlib.extract_scene import manimlib.extract_scene
import manimlib.stream_starter
def main(): def main():
args = manimlib.config.parse_cli() args = manimlib.config.parse_cli()
if not args.livestream: config = manimlib.config.get_configuration(args)
config = manimlib.config.get_configuration(args) manimlib.constants.initialize_directories(config)
manimlib.constants.initialize_directories(config) scenes = manimlib.extract_scene.main(config)
manimlib.extract_scene.main(config)
else: for scene in scenes:
manimlib.stream_starter.start_livestream( scene.run()
to_twitch=args.to_twitch,
twitch_key=args.twitch_key,
)

View file

@ -83,9 +83,13 @@ def parse_cli():
help="Write all the scenes from a file", help="Write all the scenes from a file",
), ),
parser.add_argument( parser.add_argument(
"-o", "--file_name", "-o", "--open",
help="Specify the name of the output file, if" action="store_true",
"it should be different from the scene class name", 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( parser.add_argument(
"-n", "--start_at_animation_number", "-n", "--start_at_animation_number",
@ -102,11 +106,6 @@ def parse_cli():
"-c", "--color", "-c", "--color",
help="Background color", help="Background color",
) )
parser.add_argument(
"--sound",
action="store_true",
help="Play a success/failure sound",
)
parser.add_argument( parser.add_argument(
"--leave_progress_bars", "--leave_progress_bars",
action="store_true", action="store_true",
@ -185,7 +184,7 @@ def get_configuration(args):
module = get_module(args.file) module = get_module(args.file)
file_writer_config = { file_writer_config = {
# By default, write to file # 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_last_frame": args.save_last_frame,
"save_pngs": args.save_pngs, "save_pngs": args.save_pngs,
"save_as_gif": args.save_as_gif, "save_as_gif": args.save_as_gif,
@ -194,21 +193,21 @@ def get_configuration(args):
"movie_file_extension": ".mov" if args.transparent else ".mp4", "movie_file_extension": ".mov" if args.transparent else ".mp4",
"file_name": args.file_name, "file_name": args.file_name,
"input_file_path": args.file, "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"): if hasattr(module, "OUTPUT_DIRECTORY"):
file_writer_config["output_directory"] = module.OUTPUT_DIRECTORY file_writer_config["output_directory"] = module.OUTPUT_DIRECTORY
config = { config = {
"module": module, "module": module,
"scene_names": args.scene_names, "scene_names": args.scene_names,
"open_video_upon_completion": args.preview, "preview": args.preview,
"show_file_in_finder": args.show_file_in_finder,
"file_writer_config": file_writer_config, "file_writer_config": file_writer_config,
"quiet": args.quiet or args.write_all, "quiet": args.quiet or args.write_all,
"ignore_waits": args.preview,
"write_all": args.write_all, "write_all": args.write_all,
"start_at_animation_number": args.start_at_animation_number, "start_at_animation_number": args.start_at_animation_number,
"end_at_animation_number": None, "end_at_animation_number": None,
"sound": args.sound,
"leave_progress_bars": args.leave_progress_bars, "leave_progress_bars": args.leave_progress_bars,
"media_dir": args.media_dir, "media_dir": args.media_dir,
"video_dir": args.video_dir, "video_dir": args.video_dir,
@ -218,6 +217,12 @@ def get_configuration(args):
# Camera configuration # Camera configuration
config["camera_config"] = get_camera_configuration(args) 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 # Arguments related to skipping
stan = config["start_at_animation_number"] stan = config["start_at_animation_number"]

View file

@ -1,62 +1,11 @@
import inspect import inspect
import itertools as it import itertools as it
import os
import platform
import subprocess as sp
import sys import sys
import traceback
from manimlib.scene.scene import Scene 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 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): def is_child_scene(obj, module):
if not inspect.isclass(obj): if not inspect.isclass(obj):
return False return False
@ -97,14 +46,31 @@ def get_scenes_to_render(scene_classes, config):
if len(scene_classes) == 0: if len(scene_classes) == 0:
print(manimlib.constants.NO_SCENE_MESSAGE) print(manimlib.constants.NO_SCENE_MESSAGE)
return [] 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"]: if config["write_all"]:
return scene_classes return [sc(**scene_kwargs) for sc in scene_classes]
result = [] result = []
for scene_name in config["scene_names"]: for scene_name in config["scene_names"]:
found = False found = False
for scene_class in scene_classes: for scene_class in scene_classes:
if scene_class.__name__ == scene_name: if scene_class.__name__ == scene_name:
result.append(scene_class) scene = scene_class(**scene_kwargs)
result.append(scene)
found = True found = True
break break
if not found and (scene_name != ""): if not found and (scene_name != ""):
@ -135,33 +101,8 @@ def get_scene_classes_from_module(module):
def main(config): def main(config):
module = config["module"] module = config["module"]
all_scene_classes = get_scene_classes_from_module(module) all_scene_classes = get_scene_classes_from_module(module)
scene_classes_to_render = get_scenes_to_render(all_scene_classes, config) scenes = get_scenes_to_render(all_scene_classes, config)
return scenes
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()
if __name__ == "__main__": if __name__ == "__main__":