mirror of
https://github.com/3b1b/manim.git
synced 2025-08-02 19:46:21 +08:00
Allow scene extraction from a special file which contains an ALL_SCENE_CLASSES list and an OUTPUT_DIRECTORY. This is part of a move to make the output file organization independent from the sourcecode organization
This commit is contained in:
@ -155,6 +155,7 @@ def get_module(file_name):
|
||||
|
||||
|
||||
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,
|
||||
@ -165,8 +166,10 @@ def get_configuration(args):
|
||||
"movie_file_extension": ".mov" if args.transparent else ".mp4",
|
||||
"file_name": args.file_name,
|
||||
}
|
||||
if hasattr(module, "OUTPUT_DIRECTORY"):
|
||||
file_writer_config["output_directory"] = module.OUTPUT_DIRECTORY
|
||||
config = {
|
||||
"module": get_module(args.file),
|
||||
"module": module,
|
||||
"scene_names": args.scene_names,
|
||||
"open_video_upon_completion": args.preview,
|
||||
"show_file_in_finder": args.show_file_in_finder,
|
||||
|
@ -67,16 +67,16 @@ def is_child_scene(obj, module):
|
||||
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):
|
||||
def prompt_user_for_choice(scene_classes):
|
||||
num_to_class = {}
|
||||
for count, scene_class in zip(it.count(1), scene_classes):
|
||||
name = scene_class.__name__
|
||||
print("%d: %s" % (count, name))
|
||||
num_to_name[count] = name
|
||||
num_to_class[count] = scene_class
|
||||
try:
|
||||
user_input = input(manimlib.constants.CHOOSE_NUMBER_MESSAGE)
|
||||
return [
|
||||
name_to_obj[num_to_name[int(num_str)]]
|
||||
num_to_class[int(num_str)]
|
||||
for num_str in user_input.split(",")
|
||||
]
|
||||
except KeyError:
|
||||
@ -84,54 +84,70 @@ def prompt_user_for_choice(name_to_obj):
|
||||
sys.exit(2)
|
||||
user_input = input(manimlib.constants.CHOOSE_NUMBER_MESSAGE)
|
||||
return [
|
||||
name_to_obj[num_to_name[int(num_str)]]
|
||||
num_to_class[int(num_str)]
|
||||
for num_str in user_input.split(",")
|
||||
]
|
||||
except EOFError:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def get_scene_classes(scene_names_to_classes, config):
|
||||
if len(scene_names_to_classes) == 0:
|
||||
def get_scenes_to_render(scene_classes, config):
|
||||
if len(scene_classes) == 0:
|
||||
print(manimlib.constants.NO_SCENE_MESSAGE)
|
||||
return []
|
||||
if config["write_all"]:
|
||||
return list(scene_names_to_classes.values())
|
||||
scene_classes = []
|
||||
return scene_classes
|
||||
result = []
|
||||
for scene_name in config["scene_names"]:
|
||||
if scene_name in scene_names_to_classes:
|
||||
scene_classes.append(scene_names_to_classes[scene_name])
|
||||
elif scene_name != "":
|
||||
found = False
|
||||
for scene_class in scene_classes:
|
||||
if scene_class.__name__ == scene_name:
|
||||
result.append(scene_class)
|
||||
found = True
|
||||
break
|
||||
if not found and (scene_name != ""):
|
||||
print(
|
||||
manimlib.constants.SCENE_NOT_FOUND_MESSAGE.format(
|
||||
scene_name
|
||||
),
|
||||
file=sys.stderr
|
||||
)
|
||||
if scene_classes:
|
||||
return scene_classes
|
||||
return prompt_user_for_choice(scene_names_to_classes)
|
||||
if result:
|
||||
return result
|
||||
return prompt_user_for_choice(scene_classes)
|
||||
|
||||
|
||||
def get_scene_classes_from_module(module):
|
||||
if hasattr(module, "ALL_SCENE_CLASSES"):
|
||||
return module.ALL_SCENE_CLASSES
|
||||
else:
|
||||
return [
|
||||
member[1]
|
||||
for member in inspect.getmembers(
|
||||
module,
|
||||
lambda x: is_child_scene(x, module)
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def main(config):
|
||||
module = config["module"]
|
||||
scene_names_to_classes = dict(
|
||||
inspect.getmembers(module, lambda x: is_child_scene(x, 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",
|
||||
"skip_animations",
|
||||
"file_writer_config",
|
||||
"skip_animations",
|
||||
"start_at_animation_number",
|
||||
"end_at_animation_number",
|
||||
"leave_progress_bars",
|
||||
]
|
||||
])
|
||||
|
||||
for SceneClass in get_scene_classes(scene_names_to_classes, config):
|
||||
for SceneClass in scene_classes_to_render:
|
||||
try:
|
||||
# By invoking, this renders the full scene
|
||||
scene = SceneClass(**scene_kwargs)
|
||||
|
Reference in New Issue
Block a user