A community-maintained Python framework for creating mathematical animations.
Allow setting the input file via a config file (#2151)
heitor25b3126626fb
diff --git a/manim/_config/utils.py b/manim/_config/utils.pyindex 100b6dbb9..4181d74a4 100644--- a/manim/_config/utils.py+++ b/manim/_config/utils.py@@ -662,14 +662,24 @@ class ManimConfig(MutableMapping):digesting any other CLI arguments."""+ # if the input file is a config file, parse it properly+ if args.file.suffix == ".cfg":+ args.config_file = args.file++ # if args.file is `-`, the animation code has to be taken from STDIN, so the+ # input file path shouldn't be absolute, since that file won't be read.+ if str(args.file) == "-":+ self.input_file = args.file+# if a config file has been passed, digest it first so that other CLI# flags supersede itif args.config_file:self.digest_file(args.config_file)- # If args.file is `-`, the animation code has to be taken from STDIN, so the- # input file path shouldn't be absolute, since that file won't be read.- self.input_file = Path(args.file).absolute() if args.file != "-" else args.file+ # read input_file from the args if it wasn't set by the config file+ if not self.input_file:+ self.input_file = Path(args.file).absolute()+self.scene_names = args.scene_names if args.scene_names is not None else []self.output_file = args.output_filediff --git a/manim/cli/render/commands.py b/manim/cli/render/commands.pyindex 7bac2e9bb..67d196981 100644--- a/manim/cli/render/commands.py+++ b/manim/cli/render/commands.py@@ -37,7 +37,7 @@ def render():"""Render SCENE(S) from the input FILE.- FILE is the file path of the script.+ FILE is the file path of the script or a config file.SCENES is an optional list of scenes in the file."""@@ -95,7 +95,7 @@ def render(return click_argsconfig.digest_args(click_args)- file = args["file"]+ file = Path(config.input_file)if config.renderer == "opengl":from manim.renderer.opengl_renderer import OpenGLRendererdiff --git a/tests/control_data/videos_data/InputFileViaCfg.json b/tests/control_data/videos_data/InputFileViaCfg.jsonnew file mode 100644index 000000000..37d916567--- /dev/null+++ b/tests/control_data/videos_data/InputFileViaCfg.json@@ -0,0 +1,13 @@+{+ "name": "InputFileViaCfg",+ "movie_metadata": {+ "codec_name": "h264",+ "width": 854,+ "height": 480,+ "avg_frame_rate": "15/1",+ "duration": "1.000000",+ "nb_frames": "15"+ },+ "section_dir_layout": [],+ "section_index": []+}diff --git a/tests/test_scene_rendering/test_cli_flags.py b/tests/test_scene_rendering/test_cli_flags.pyindex 9abc8df21..5d920e253 100644--- a/tests/test_scene_rendering/test_cli_flags.py+++ b/tests/test_scene_rendering/test_cli_flags.py@@ -1,4 +1,5 @@import itertools+import osimport sysfrom pathlib import Path@@ -638,3 +639,32 @@ def test_mov_can_be_set_as_output_format(tmp_path, manim_cfg_file, simple_scenesassert expected_mov_path.exists(), "expected .mov file not found at " + str(expected_mov_path,)+++@video_comparison(+ "InputFileViaCfg.json",+ "videos/simple_scenes/480p15/SquareToCircle.mp4",+)+def test_input_file_via_cfg(tmp_path, manim_cfg_file, simple_scenes_path):+ scene_name = "SquareToCircle"+ with open(os.path.join(tmp_path, "manim.cfg"), "w") as file:+ file.write(+ f"""+[CLI]+input_file = {simple_scenes_path}+ """+ )++ command = [+ sys.executable,+ "-m",+ "manim",+ "-ql",+ "--media_dir",+ ".",+ str(tmp_path),+ scene_name,+ ]+ out, err, exit_code = capture(command, cwd=tmp_path)+ assert exit_code == 0, err