Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Update extract_scene.py args to better support previewing, showing in finder, opening output files, etc.

Grant Sanderson4e468af6564b
diff --git a/extract_scene.py b/extract_scene.py
index 455c7f256..2580ac8f8 100644
--- a/extract_scene.py
+++ b/extract_scene.py
@@ -9,6 +9,7 @@ import inspect
import traceback
import imp
import os
+import subprocess as sp
from helpers import *
from scene import Scene
@@ -58,7 +59,8 @@ def get_configuration():
("-s", "--show_last_frame"),
("-l", "--low_quality"),
("-m", "--medium_quality"),
- ("-f", "--save_pngs"),
+ ("-g", "--save_pngs"),
+ ("-f", "--show_file_in_finder"),
("-t", "--transparent"),
("-q", "--quiet"),
("-a", "--write_all")
@@ -74,11 +76,10 @@ def get_configuration():
config = {
"file" : args.file,
"scene_name" : args.scene_name,
- "camera_config" : PRODUCTION_QUALITY_CAMERA_CONFIG, #TODO
- "frame_duration" : PRODUCTION_QUALITY_FRAME_DURATION, #TODO
- "preview" : args.preview,
- "write_to_movie" : args.write_to_movie,
- "save_frames" : args.preview, #Scenes only save frame when previewing
+ "open_video_upon_completion" : args.preview,
+ "show_file_in_finder" : args.show_file_in_finder,
+ #By default, write to file
+ "write_to_movie" : args.write_to_movie or not args.show_last_frame,
"show_last_frame" : args.show_last_frame,
"save_pngs" : args.save_pngs,
#If -t is passed in (for transparent), this will be RGBA
@@ -88,7 +89,7 @@ def get_configuration():
"output_name" : args.output_name,
"skip_to_animation_number" : args.skip_to_animation_number,
}
- if args.low_quality or args.preview:
+ if args.low_quality:
config["camera_config"] = LOW_QUALITY_CAMERA_CONFIG
config["frame_duration"] = LOW_QUALITY_FRAME_DURATION
elif args.medium_quality:
@@ -102,10 +103,6 @@ def get_configuration():
if stan is not None:
config["skip_to_animation_number"] = int(stan)
- #By default, write to file
- actions = ["write_to_movie", "preview", "show_last_frame"]
- if not any([config[key] for key in actions]):
- config["write_to_movie"] = True
config["skip_animations"] = any([
config["show_last_frame"] and not config["write_to_movie"],
config["skip_to_animation_number"],
@@ -117,12 +114,23 @@ def handle_scene(scene, **config):
curr_stdout = sys.stdout
sys.stdout = open(os.devnull, "w")
- if config["preview"]:
- scene.preview()
if config["show_last_frame"]:
- if not config["write_all"]:
- scene.show_frame()
scene.save_image(mode = config["saved_image_mode"])
+ open_file = any([
+ config["show_last_frame"],
+ config["open_video_upon_completion"],
+ config["show_file_in_finder"]
+ ])
+ if open_file:
+ commands = ["open"]
+ if config["show_file_in_finder"]:
+ commands.append("-R")
+ #
+ if config["show_last_frame"]:
+ commands.append(scene.get_image_file_path())
+ else:
+ commands.append(scene.get_movie_file_path())
+ sp.call(commands)
if config["quiet"]:
sys.stdout.close()
@@ -209,7 +217,6 @@ def main():
"frame_duration",
"skip_animations",
"write_to_movie",
- "save_frames",
"output_directory",
"save_pngs",
"skip_to_animation_number",
diff --git a/scene/scene.py b/scene/scene.py
index 5b5154721..1a01c4119 100644
--- a/scene/scene.py
+++ b/scene/scene.py
@@ -33,6 +33,7 @@ class Scene(object):
"save_pngs" : False,
"pngs_mode" : "RGBA",
"output_directory" : ANIMATIONS_DIR,
+ "movie_file_extension" : ".mp4",
"name" : None,
"always_continually_update" : False,
"random_seed" : 0,
@@ -470,24 +471,30 @@ class Scene(object):
def preview(self):
TkSceneRoot(self)
-
- def save_image(self, name = None, mode = "RGB", dont_update = False):
+
+ def get_image_file_path(self, name = None, dont_update = False):
folder = "images"
if dont_update:
folder = str(self)
-
path = os.path.join(self.output_directory, folder)
file_name = (name or str(self)) + ".png"
- full_path = os.path.join(path, file_name)
+ return os.path.join(path, file_name)
+
+ def save_image(self, name = None, mode = "RGB", dont_update = False):
+ path = self.get_image_file_path(name, dont_update)
if not os.path.exists(path):
os.makedirs(path)
if not dont_update:
self.update_frame()
image = self.get_image()
image = image.convert(mode)
- image.save(full_path)
+ image.save(path)
- def get_movie_file_path(self, name, extension):
+ def get_movie_file_path(self, name = None, extension = None):
+ if extension is None:
+ extension = self.movie_file_extension
+ if name is None:
+ name = self.name
file_path = os.path.join(self.output_directory, name)
if not file_path.endswith(extension):
file_path += extension
@@ -497,8 +504,8 @@ class Scene(object):
def open_movie_pipe(self):
name = str(self)
- file_path = self.get_movie_file_path(name, ".mp4")
- temp_file_path = file_path.replace(".mp4", "Temp.mp4")
+ file_path = self.get_movie_file_path(name)
+ temp_file_path = file_path.replace(name, name + "Temp")
print("Writing to %s"%temp_file_path)
self.args_to_rename_file = (temp_file_path, file_path)