|
1 | | -from . import extract_scene |
2 | | -from . import config |
| 1 | +import inspect |
| 2 | +import os |
| 3 | +import platform |
| 4 | +import subprocess as sp |
| 5 | +import sys |
| 6 | +import re |
| 7 | +import traceback |
| 8 | +import importlib.util |
| 9 | + |
| 10 | +from .config import file_writer_config |
| 11 | +from .scene.scene import Scene |
| 12 | +from .utils.sounds import play_error_sound |
| 13 | +from .utils.sounds import play_finish_sound |
3 | 14 | from . import constants |
| 15 | +from .logger import logger |
| 16 | + |
| 17 | + |
| 18 | +def open_file_if_needed(file_writer): |
| 19 | + if file_writer_config["quiet"]: |
| 20 | + curr_stdout = sys.stdout |
| 21 | + sys.stdout = open(os.devnull, "w") |
| 22 | + |
| 23 | + open_file = any([ |
| 24 | + file_writer_config["preview"], |
| 25 | + file_writer_config["show_file_in_finder"] |
| 26 | + ]) |
| 27 | + if open_file: |
| 28 | + current_os = platform.system() |
| 29 | + file_paths = [] |
| 30 | + |
| 31 | + if file_writer_config["save_last_frame"]: |
| 32 | + file_paths.append(file_writer.get_image_file_path()) |
| 33 | + if file_writer_config["write_to_movie"]: |
| 34 | + file_paths.append(file_writer.get_movie_file_path()) |
| 35 | + |
| 36 | + for file_path in file_paths: |
| 37 | + if current_os == "Windows": |
| 38 | + os.startfile(file_path) |
| 39 | + else: |
| 40 | + commands = [] |
| 41 | + if current_os == "Linux": |
| 42 | + commands.append("xdg-open") |
| 43 | + elif current_os.startswith("CYGWIN"): |
| 44 | + commands.append("cygstart") |
| 45 | + else: # Assume macOS |
| 46 | + commands.append("open") |
| 47 | + |
| 48 | + if file_writer_config["show_file_in_finder"]: |
| 49 | + commands.append("-R") |
| 50 | + |
| 51 | + commands.append(file_path) |
| 52 | + |
| 53 | + # commands.append("-g") |
| 54 | + FNULL = open(os.devnull, 'w') |
| 55 | + sp.call(commands, stdout=FNULL, stderr=sp.STDOUT) |
| 56 | + FNULL.close() |
| 57 | + |
| 58 | + if file_writer_config["quiet"]: |
| 59 | + sys.stdout.close() |
| 60 | + sys.stdout = curr_stdout |
| 61 | + |
| 62 | + |
| 63 | +def is_child_scene(obj, module): |
| 64 | + return (inspect.isclass(obj) |
| 65 | + and issubclass(obj, Scene) |
| 66 | + and obj != Scene |
| 67 | + and obj.__module__.startswith(module.__name__)) |
| 68 | + |
| 69 | + |
| 70 | +def prompt_user_for_choice(scene_classes): |
| 71 | + num_to_class = {} |
| 72 | + for count, scene_class in enumerate(scene_classes): |
| 73 | + count += 1 # start with 1 instead of 0 |
| 74 | + name = scene_class.__name__ |
| 75 | + print("%d: %s" % (count, name)) |
| 76 | + num_to_class[count] = scene_class |
| 77 | + try: |
| 78 | + user_input = input(constants.CHOOSE_NUMBER_MESSAGE) |
| 79 | + return [num_to_class[int(num_str)] |
| 80 | + for num_str in re.split(r"\s*,\s*", user_input.strip())] |
| 81 | + except KeyError: |
| 82 | + logger.error(constants.INVALID_NUMBER_MESSAGE) |
| 83 | + sys.exit(2) |
| 84 | + except EOFError: |
| 85 | + sys.exit(1) |
| 86 | + |
| 87 | + |
| 88 | +def get_scenes_to_render(scene_classes): |
| 89 | + if not scene_classes: |
| 90 | + logger.error(constants.NO_SCENE_MESSAGE) |
| 91 | + return [] |
| 92 | + if file_writer_config["write_all"]: |
| 93 | + return scene_classes |
| 94 | + result = [] |
| 95 | + for scene_name in file_writer_config["scene_names"]: |
| 96 | + found = False |
| 97 | + for scene_class in scene_classes: |
| 98 | + if scene_class.__name__ == scene_name: |
| 99 | + result.append(scene_class) |
| 100 | + found = True |
| 101 | + break |
| 102 | + if not found and (scene_name != ""): |
| 103 | + logger.error( |
| 104 | + constants.SCENE_NOT_FOUND_MESSAGE.format( |
| 105 | + scene_name |
| 106 | + ) |
| 107 | + ) |
| 108 | + if result: |
| 109 | + return result |
| 110 | + return [scene_classes[0]] if len(scene_classes) == 1 else prompt_user_for_choice(scene_classes) |
| 111 | + |
| 112 | + |
| 113 | +def get_scene_classes_from_module(module): |
| 114 | + if hasattr(module, "SCENES_IN_ORDER"): |
| 115 | + return module.SCENES_IN_ORDER |
| 116 | + else: |
| 117 | + return [ |
| 118 | + member[1] |
| 119 | + for member in inspect.getmembers( |
| 120 | + module, |
| 121 | + lambda x: is_child_scene(x, module) |
| 122 | + ) |
| 123 | + ] |
| 124 | + |
| 125 | + |
| 126 | +def get_module(file_name): |
| 127 | + if file_name == "-": |
| 128 | + module = types.ModuleType("input_scenes") |
| 129 | + code = sys.stdin.read() |
| 130 | + try: |
| 131 | + exec(code, module.__dict__) |
| 132 | + return module |
| 133 | + except Exception as e: |
| 134 | + logger.error(f"Failed to render scene: {str(e)}") |
| 135 | + sys.exit(2) |
| 136 | + else: |
| 137 | + if os.path.exists(file_name): |
| 138 | + module_name = re.sub(r"\..+$", "", file_name.replace(os.sep, ".")) |
| 139 | + spec = importlib.util.spec_from_file_location(module_name, file_name) |
| 140 | + module = importlib.util.module_from_spec(spec) |
| 141 | + spec.loader.exec_module(module) |
| 142 | + return module |
| 143 | + else: |
| 144 | + raise FileNotFoundError(f'{file_name} not found') |
4 | 145 |
|
5 | 146 |
|
6 | 147 | def main(): |
7 | | - args = config.parse_cli() |
8 | | - cfg = config.get_configuration(args) |
9 | | - config.initialize_directories(cfg) |
10 | | - config.initialize_tex(cfg) |
11 | | - extract_scene.main(cfg) |
| 148 | + module = get_module(file_writer_config["input_file"]) |
| 149 | + all_scene_classes = get_scene_classes_from_module(module) |
| 150 | + scene_classes_to_render = get_scenes_to_render(all_scene_classes) |
| 151 | + sound_on = file_writer_config["sound"] |
| 152 | + for SceneClass in scene_classes_to_render: |
| 153 | + try: |
| 154 | + # By invoking, this renders the full scene |
| 155 | + scene = SceneClass() |
| 156 | + open_file_if_needed(scene.file_writer) |
| 157 | + if sound_on: |
| 158 | + play_finish_sound() |
| 159 | + except Exception: |
| 160 | + print("\n\n") |
| 161 | + traceback.print_exc() |
| 162 | + print("\n\n") |
| 163 | + if sound_on: |
| 164 | + play_error_sound() |
12 | 165 |
|
13 | 166 |
|
14 | 167 | if __name__ == "__main__": |
|
0 commit comments