From 93fe783b80a9859360a7f3b5d116ac23bf16c76b Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Sat, 2 Jan 2021 22:03:00 -0800 Subject: [PATCH] Make it so that simply typing 'python -m manim' will embed you in a blank scene to work with --- custom_defaults.yml | 1 + manimlib/config.py | 22 ++++++++++++---------- manimlib/extract_scene.py | 16 +++++++++++----- manimlib/scene/scene.py | 7 +++++++ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/custom_defaults.yml b/custom_defaults.yml index bcfb72ec..fba64382 100644 --- a/custom_defaults.yml +++ b/custom_defaults.yml @@ -25,6 +25,7 @@ tex: # executable: "xelatex -no-pdf" # template_file: "ctex_template.tex" # intermediate_filetype: "xdv" +universal_import_line: "from manimlib.imports import *" style: background_color: "#333333" quality: diff --git a/manimlib/config.py b/manimlib/config.py index b2a044a0..64282757 100644 --- a/manimlib/config.py +++ b/manimlib/config.py @@ -143,15 +143,17 @@ def get_manim_dir(): def get_module(file_name): - if file_name == "-": - module = types.ModuleType("input_scenes") - code = "from manimlib.imports import *\n\n" + sys.stdin.read() - try: - exec(code, module.__dict__) - return module - except Exception as e: - print(f"Failed to render scene: {str(e)}") - sys.exit(2) + # if file_name == "-": + # module = types.ModuleType("input_scenes") + # code = "from manimlib.imports import *\n\n" + sys.stdin.read() + # try: + # exec(code, module.__dict__) + # return module + # except Exception as e: + # print(f"Failed to render scene: {str(e)}") + # sys.exit(2) + if file_name is None: + return None else: module_name = file_name.replace(os.sep, ".").replace(".py", "") spec = importlib.util.spec_from_file_location(module_name, file_name) @@ -187,7 +189,7 @@ def get_configuration(args): "mirror_module_path": custom_defaults["directories"]["mirror_module_path"], "output_directory": args.video_dir or custom_defaults["directories"]["output"], "file_name": args.file_name, - "input_file_path": args.file, + "input_file_path": args.file or "", "open_file_upon_completion": args.open, "show_file_location_upon_completion": args.finder, "quiet": args.quiet, diff --git a/manimlib/extract_scene.py b/manimlib/extract_scene.py index 01db98cd..6848ab7e 100644 --- a/manimlib/extract_scene.py +++ b/manimlib/extract_scene.py @@ -1,8 +1,10 @@ import inspect import itertools as it import sys +import logging from manimlib.scene.scene import Scene +from manimlib.scene.scene import BlankScene import manimlib.constants @@ -43,10 +45,6 @@ def prompt_user_for_choice(scene_classes): 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 [ @@ -61,6 +59,10 @@ def get_scenes_to_render(scene_classes, config): ] ]) + if len(config["scene_names"]) == 0: + # If no module or scenes were passed in, just run the blank scene + return [BlankScene(**scene_kwargs)] + if config["write_all"]: return [sc(**scene_kwargs) for sc in scene_classes] @@ -74,7 +76,8 @@ def get_scenes_to_render(scene_classes, config): found = True break if not found and (scene_name != ""): - print( + logging.log( + logging.ERROR, manimlib.constants.SCENE_NOT_FOUND_MESSAGE.format( scene_name ), @@ -87,6 +90,9 @@ def get_scenes_to_render(scene_classes, config): def get_scene_classes_from_module(module): + if module is None: + # If no module was passed in, just run the blank scene + return [] if hasattr(module, "SCENES_IN_ORDER"): return module.SCENES_IN_ORDER else: diff --git a/manimlib/scene/scene.py b/manimlib/scene/scene.py index c7d137de..a48bb8d4 100644 --- a/manimlib/scene/scene.py +++ b/manimlib/scene/scene.py @@ -13,6 +13,7 @@ from manimlib.animation.animation import Animation from manimlib.animation.transform import MoveToTarget from manimlib.mobject.mobject import Point from manimlib.camera.camera import Camera +from manimlib.config import get_custom_defaults from manimlib.constants import * from manimlib.container.container import Container from manimlib.mobject.mobject import Mobject @@ -574,3 +575,9 @@ class Scene(Container): class EndSceneEarlyException(Exception): pass + + +class BlankScene(Scene): + def construct(self): + exec(get_custom_defaults()["universal_import_line"]) + self.embed()