forked from 3b1b/manim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_scene.py
264 lines (240 loc) · 8.41 KB
/
extract_scene.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python2
import sys
# import getopt
import argparse
import imp
import imp
import inspect
import itertools as it
import os
import subprocess as sp
import traceback
from camera.camera import Camera
from constants import *
from scene.scene import Scene
from utils.sounds import play_error_sound
from utils.sounds import play_finish_sound
HELP_MESSAGE = """
Usage:
python extract_scene.py <module> [<scene name>]
-p preview in low quality
-s show and save picture of last frame
-w write result to file [this is default if nothing else is stated]
-l use low quality
-m use medium quality
-a run and save every scene in the script, or all args for the given scene
-q don't print progress
-f when writing to a movie file, export the frames in png sequence
-t use transperency when exporting images
"""
SCENE_NOT_FOUND_MESSAGE = """
That scene is not in the script
"""
CHOOSE_NUMBER_MESSAGE = """
Choose number corresponding to desired scene/arguments.
(Use comma separated list for multiple entries)
Choice(s): """
INVALID_NUMBER_MESSAGE = "Fine then, if you don't want to give a valid number I'll just quit"
NO_SCENE_MESSAGE = """
There are no scenes inside that module
"""
def get_configuration():
try:
parser = argparse.ArgumentParser()
parser.add_argument(
"file", help = "path to file holding the python code for the scene"
)
parser.add_argument(
"scene_name", help = "Name of the Scene class you want to see"
)
optional_args = [
("-p", "--preview"),
("-w", "--write_to_movie"),
("-s", "--show_last_frame"),
("-l", "--low_quality"),
("-m", "--medium_quality"),
("-g", "--save_pngs"),
("-f", "--show_file_in_finder"),
("-t", "--transparent"),
("-q", "--quiet"),
("-a", "--write_all")
]
for short_arg, long_arg in optional_args:
parser.add_argument(short_arg, long_arg, action = "store_true")
parser.add_argument("-o", "--output_name")
parser.add_argument("-n", "--start_at_animation_number")
args = parser.parse_args()
if args.output_name != None:
output_name_root, output_name_ext = os.path.splitext(args.output_name)
expected_ext = '.png' if args.show_last_frame else '.mp4'
if not output_name_ext in ['', expected_ext]:
print "WARNING: The output will be to (doubly-dotted) %s%s"%output_name_root%expected_ext
output_name = args.output_name
else:
# If anyone wants .mp4.mp4 and is surprised to only get .mp4, or such... Well, too bad.
output_name = output_name_root
else:
output_name = args.output_name
except argparse.ArgumentError as err:
print(str(err))
sys.exit(2)
config = {
"file" : args.file,
"scene_name" : args.scene_name,
"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
"saved_image_mode": "RGBA" if args.transparent else "RGB",
"movie_file_extension" : ".mov" if args.transparent else ".mp4",
"quiet" : args.quiet or args.write_all,
"ignore_waits" : args.preview,
"write_all" : args.write_all,
"output_name" : output_name,
"start_at_animation_number" : args.start_at_animation_number,
"end_at_animation_number" : None,
}
if args.low_quality:
config["camera_config"] = LOW_QUALITY_CAMERA_CONFIG
config["frame_duration"] = LOW_QUALITY_FRAME_DURATION
elif args.medium_quality:
config["camera_config"] = MEDIUM_QUALITY_CAMERA_CONFIG
config["frame_duration"] = MEDIUM_QUALITY_FRAME_DURATION
else:
config["camera_config"] = PRODUCTION_QUALITY_CAMERA_CONFIG
config["frame_duration"] = PRODUCTION_QUALITY_FRAME_DURATION
stan = config["start_at_animation_number"]
if stan is not None:
if "," in stan:
start, end = stan.split(",")
config["start_at_animation_number"] = int(start)
config["end_at_animation_number"] = int(end)
else:
config["start_at_animation_number"] = int(stan)
config["skip_animations"] = any([
config["show_last_frame"] and not config["write_to_movie"],
config["start_at_animation_number"],
])
return config
def handle_scene(scene, **config):
if config["quiet"]:
curr_stdout = sys.stdout
sys.stdout = open(os.devnull, "w")
if config["show_last_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()
sys.stdout = curr_stdout
def is_scene(obj):
if not inspect.isclass(obj):
return False
if not issubclass(obj, Scene):
return False
if obj == Scene:
return False
return True
def prompt_user_for_choice(name_to_obj):
num_to_name = {}
names = sorted(name_to_obj.keys())
for count, name in zip(it.count(1), names):
print("%d: %s"%(count, name))
num_to_name[count] = name
try:
user_input = raw_input(CHOOSE_NUMBER_MESSAGE)
return [
name_to_obj[num_to_name[int(num_str)]]
for num_str in user_input.split(",")
]
except:
print(INVALID_NUMBER_MESSAGE)
sys.exit()
def get_scene_classes(scene_names_to_classes, config):
if len(scene_names_to_classes) == 0:
print(NO_SCENE_MESSAGE)
return []
if len(scene_names_to_classes) == 1:
return scene_names_to_classes.values()
if config["scene_name"] in scene_names_to_classes:
return [scene_names_to_classes[config["scene_name"]] ]
if config["scene_name"] != "":
print(SCENE_NOT_FOUND_MESSAGE)
return []
if config["write_all"]:
return scene_names_to_classes.values()
return prompt_user_for_choice(scene_names_to_classes)
def get_module_windows(file_name):
module_name = file_name.replace(".py", "")
last_module = imp.load_module("__init__", *imp.find_module("__init__", ['.']))
for part in module_name.split(os.sep):
load_args = imp.find_module(part, [os.path.dirname(last_module.__file__)])
last_module = imp.load_module(part, *load_args)
return last_module
def get_module_posix(file_name):
module_name = file_name.replace(".py", "")
last_module = imp.load_module(".", *imp.find_module("."))
for part in module_name.split(os.sep):
load_args = imp.find_module(part, last_module.__path__)
last_module = imp.load_module(part, *load_args)
return last_module
def get_module(file_name):
if os.name == 'nt':
return get_module_windows(file_name)
return get_module_posix(file_name)
def main():
config = get_configuration()
module = get_module(config["file"])
scene_names_to_classes = dict(
inspect.getmembers(module, is_scene)
)
config["output_directory"] = os.path.join(
ANIMATIONS_DIR,
config["file"].replace(".py", "")
)
scene_kwargs = dict([
(key, config[key])
for key in [
"camera_config",
"frame_duration",
"skip_animations",
"write_to_movie",
"output_directory",
"save_pngs",
"movie_file_extension",
"start_at_animation_number",
"end_at_animation_number",
]
])
scene_kwargs["name"] = config["output_name"]
if config["save_pngs"]:
print "We are going to save a PNG sequence as well..."
scene_kwargs["save_pngs"] = True
scene_kwargs["pngs_mode"] = config["saved_image_mode"]
for SceneClass in get_scene_classes(scene_names_to_classes, config):
try:
handle_scene(SceneClass(**scene_kwargs), **config)
play_finish_sound()
except:
print("\n\n")
traceback.print_exc()
print("\n\n")
play_error_sound()
if __name__ == "__main__":
main()