From e3d09d161c0742e37c1994dbbcd8a0a447339cb0 Mon Sep 17 00:00:00 2001 From: Tony031218 <975062472@qq.com> Date: Sun, 7 Feb 2021 17:45:18 +0800 Subject: [PATCH] add auto config guide --- custom_defaults.yml | 56 ----------------------- manimlib/__main__.py | 15 ++++--- manimlib/config.py | 21 +++++++-- manimlib/defaults.yml | 56 ----------------------- manimlib/utils/init_config.py | 84 +++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 120 deletions(-) delete mode 100644 custom_defaults.yml delete mode 100644 manimlib/defaults.yml create mode 100644 manimlib/utils/init_config.py diff --git a/custom_defaults.yml b/custom_defaults.yml deleted file mode 100644 index 45b3b77b..00000000 --- a/custom_defaults.yml +++ /dev/null @@ -1,56 +0,0 @@ -directories: - # Set this to true if you want the path to video files - # to match the directory structure of the path to the - # sourcecode generating that video - mirror_module_path: False - # Where should manim output video and image files? - output: "" - # If you want to use images, manim will look to these folders to find them - raster_images: "" - vector_images: "" - # If you want to use sounds, manim will look here to find it. - sounds: "" - # Manim often generates tex_files or other kinds of serialized data - # to keep from having to generate the same thing too many times. By - # default, these will be stored at tempfile.gettempdir(), e.g. this might - # return whatever is at to the TMPDIR environment variable. If you want to - # specify them elsewhere, - temporary_storage: "" -tex: - executable: "latex" - template_file: "tex_template.tex" - intermediate_filetype: "dvi" - text_to_replace: "[tex_expression]" - # For ctex, use the following configuration - # executable: "xelatex -no-pdf" - # template_file: "ctex_template.tex" - # intermediate_filetype: "xdv" -universal_import_line: "from manimlib import *" -style: - font: "Consolas" - background_color: "#333333" -# Set the position of preview window, you can use directions, e.g. UL/DR/OL/OO/... -# also, you can also specify the position(pixel) of the upper left corner of -# the window on the monitor, e.g. "960,540" -window_position: UR -# If break_into_partial_movies is set to True, then many small -# files will be written corresponding to each Scene.play and -# Scene.wait call, and these files will then be combined -# to form the full scene. Sometimes video-editing is made -# easier when working with the broken up scene, which -# effectively has cuts at all the places you might want. -break_into_partial_movies: False -camera_qualities: - low: - resolution: "854x480" - frame_rate: 15 - medium: - resolution: "1280x720" - frame_rate: 30 - high: - resolution: "1920x1080" - frame_rate: 30 - ultra_high: - resolution: "3840x2160" - frame_rate: 60 - default_quality: "high" diff --git a/manimlib/__main__.py b/manimlib/__main__.py index 9b3b3f28..9bc76578 100644 --- a/manimlib/__main__.py +++ b/manimlib/__main__.py @@ -1,12 +1,17 @@ #!/usr/bin/env python import manimlib.config import manimlib.extract_scene +import manimlib.utils.init_config def main(): args = manimlib.config.parse_cli() - config = manimlib.config.get_configuration(args) - scenes = manimlib.extract_scene.main(config) - - for scene in scenes: - scene.run() + + if args.config: + manimlib.utils.init_config.init_customization() + else: + config = manimlib.config.get_configuration(args) + scenes = manimlib.extract_scene.main(config) + + for scene in scenes: + scene.run() diff --git a/manimlib/config.py b/manimlib/config.py index b1411965..583d26bb 100644 --- a/manimlib/config.py +++ b/manimlib/config.py @@ -8,6 +8,7 @@ import yaml from screeninfo import get_monitors from manimlib.utils.config_ops import merge_dicts_recursively +from manimlib.utils.init_config import init_customization def parse_cli(): @@ -98,6 +99,11 @@ def parse_cli(): "--file_name", help="Name for the movie or image file", ) + parser.add_argument( + "--config", + action="store_true", + help="Guide for automatic configuration", + ) parser.add_argument( "-n", "--start_at_animation_number", help="Start rendering not from the first animation, but" @@ -152,9 +158,18 @@ def get_module(file_name): def get_custom_defaults(): filename = "custom_defaults.yml" - manim_defaults_file = os.path.join(get_manim_dir(), "manimlib", "defaults.yml") - with open(manim_defaults_file, "r") as file: - custom_defaults = yaml.safe_load(file) + global_defaults_file = os.path.join(get_manim_dir(), "manimlib", "defaults.yml") + + if not (os.path.exists(global_defaults_file) or os.path.exists(filename)): + print("There is no configuration file detected. Initial configuration:\n") + init_customization() + + if os.path.exists(global_defaults_file): + with open(global_defaults_file, "r") as file: + custom_defaults = yaml.safe_load(file) + else: + with open(filename, "r") as file: + local_defaults = yaml.safe_load(file) # See if there's a custom_defaults file in current directory, # and if so, it further updates the defaults based on it. diff --git a/manimlib/defaults.yml b/manimlib/defaults.yml deleted file mode 100644 index c63ff796..00000000 --- a/manimlib/defaults.yml +++ /dev/null @@ -1,56 +0,0 @@ -directories: - # Set this to true if you want the path to video files - # to match the directory structure of the path to the - # sourcecode generating that video - mirror_module_path: False - # Where should manim output video and image files? - output: "" - # If you want to use images, manim will look to these folders to find them - raster_images: "" - vector_images: "" - # If you want to use sounds, manim will look here to find it. - sounds: "" - # Manim often generates tex_files or other kinds of serialized data - # to keep from having to generate the same thing too many times. By - # default, these will be stored at tempfile.gettempdir(), e.g. this might - # return whatever is at to the TMPDIR environment variable. If you want to - # specify them elsewhere, - temporary_storage: "" -tex: - executable: "latex" - template_file: "tex_template.tex" - intermediate_filetype: "dvi" - text_to_replace: "[tex_expression]" - # # For ctex, use the following configuration - # executable: "xelatex -no-pdf" - # template_file: "ctex_template.tex" - # intermediate_filetype: "xdv" -universal_import_line: "from manimlib import *" -style: - font: "Consolas" - background_color: "#333333" -# Set the position of preview window, you can use directions, e.g. UL/DR/OL/OO/... -# also, you can also specify the position(pixel) of the upper left corner of -# the window on the monitor, e.g. "960,540" -window_position: UR -# If break_into_partial_movies is set to True, then many small -# files will be written corresponding to each Scene.play and -# Scene.wait call, and these files will then be combined -# to form the full scene. Sometimes video-editing is made -# easier when working with the broken up scene, which -# effectively has cuts at all the places you might want. -break_into_partial_movies: False -camera_qualities: - low: - resolution: "854x480" - frame_rate: 15 - medium: - resolution: "1280x720" - frame_rate: 30 - high: - resolution: "1920x1080" - frame_rate: 30 - ultra_high: - resolution: "3840x2160" - frame_rate: 60 - default_quality: "high" diff --git a/manimlib/utils/init_config.py b/manimlib/utils/init_config.py new file mode 100644 index 00000000..c94fc46f --- /dev/null +++ b/manimlib/utils/init_config.py @@ -0,0 +1,84 @@ +import yaml +import os + +def init_customization(): + configuration = { + "directories": { + "mirror_module_path": False, + "output": "", + "raster_images": "", + "vector_images": "", + "sounds": "", + "temporary_storage": "", + }, + "tex": { + "executable": "", + "template_file": "", + "intermediate_filetype": "", + "text_to_replace": "[tex_expression]", + }, + "universal_import_line": "from manimlib import *", + "style": { + "font": "Consolas", + "background_color": "", + }, + "window_position": "UR", + "break_into_partial_movies": False, + "camera_qualities": { + "low": { + "resolution": "854x480", + "frame_rate": 15, + }, + "medium": { + "resolution": "1280x720", + "frame_rate": 30, + }, + "high": { + "resolution": "1920x1080", + "frame_rate": 60, + }, + "ultra_high": { + "resolution": "3840x2160", + "frame_rate": 60, + }, + "default_quality": "", + } + } + + scope = input(" Please select the scope of the configuration [global/local]: ") + if scope == "global": + from manimlib.config import get_manim_dir + file_name = os.path.join(get_manim_dir(), "manimlib", "defaults.yml") + else: + file_name = os.path.join(os.getcwd(), "custom_defaults.yml") + + print("\n directories:") + configuration["directories"]["output"] = input(" [1/8] Where should manim output video and image files place: ") + configuration["directories"]["raster_images"] = input(" [2/8] Which folder should manim find raster images (.jpg .png .gif) in (optional): ") + configuration["directories"]["vector_images"] = input(" [3/8] Which folder should manim find vector images (.svg .xdv) in (optional): ") + configuration["directories"]["sounds"] = input(" [4/8] Which folder should manim find sound files (.mp3 .wav) in (optional): ") + configuration["directories"]["temporary_storage"] = input(" [5/8] Which folder should manim storage temporary files: ") + + print("\n tex:") + tex = input(" [6/8] Which executable file to use to compile [latex/xelatex]: ") + if tex == "latex": + configuration["tex"]["executable"] = "latex" + configuration["tex"]["template_file"] = "tex_template.tex" + configuration["tex"]["intermediate_filetype"] = "dvi" + else: + configuration["tex"]["executable"] = "xelatex -no-pdf" + configuration["tex"]["template_file"] = "ctex_template.tex" + configuration["tex"]["intermediate_filetype"] = "xdv" + + print("\n style:") + configuration["style"]["background_color"] = input(" [7/8] Which background color do you want (hex code): ") + + print("\n camera_qualities:") + print(" Four defined qualities: low: 480p15 medium: 720p30 high: 1080p60 ultra_high: 2160p60") + configuration["camera_qualities"]["default_quality"] = input(" [8/8] Which one to choose as the default rendering quality [low/medium/high/ultra_high]: ") + + with open(file_name, 'w', encoding="utf_8") as file: + yaml.dump(configuration, file) + + print(f"\nYou have set up a {scope} configuration file") + print(f"You can manually modify it again in: {file_name}\n")