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__":
manimlib.main()
else:
manimlib.stream_starter.start_livestream()

View file

@ -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,
)
scenes = manimlib.extract_scene.main(config)
for scene in scenes:
scene.run()

View file

@ -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"]

View file

@ -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__":