Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fixed issue with ``manim init scene SCENE_NAME filename.py`` and removed necessity of ``main.py`` to be present in working directory (#2870)

Joshua Mankelow999d81ee92a0
diff --git a/manim/cli/init/commands.py b/manim/cli/init/commands.py
index 28766f821..8124e687f 100644
--- a/manim/cli/init/commands.py
+++ b/manim/cli/init/commands.py
@@ -149,9 +149,6 @@ def scene(**args):
FILE is the name of file in which the SCENE will be inserted.
"""
- if not Path("main.py").exists():
- raise FileNotFoundError(f"{Path('main.py')} : Not a valid project directory.")
-
template_name = click.prompt(
"template",
type=click.Choice(get_template_names(), False),
@@ -163,8 +160,12 @@ def scene(**args):
scene = scene.replace(template_name + "Template", args["scene_name"], 1)
if args["file_name"]:
- file_name = Path(args["file_name"] + ".py")
+ if args["file_name"][-3:] == ".py":
+ file_name = args["file_name"]
+ else:
+ file_name = args["file_name"] + ".py"
+ file_name = Path(file_name)
if file_name.is_file():
# file exists so we are going to append new scene to that file
with open(file_name, "a") as f:
diff --git a/tests/interface/test_commands.py b/tests/interface/test_commands.py
index 5ab293964..9f4dbfe0a 100644
--- a/tests/interface/test_commands.py
+++ b/tests/interface/test_commands.py
@@ -93,6 +93,28 @@ def test_manim_init_project(tmp_path):
assert (Path(tmp_dir) / "testproject/manim.cfg").exists()
+def test_manim_init_scene(tmp_path):
+ command_named = ["init", "scene", "NamedFileTestScene", "my_awesome_file.py"]
+ command_unnamed = ["init", "scene", "DefaultFileTestScene"]
+ runner = CliRunner()
+ with runner.isolated_filesystem(temp_dir=tmp_path) as tmp_dir:
+ result = runner.invoke(
+ main, command_named, prog_name="manim", input="Default\n"
+ )
+ assert not result.exception
+ assert (Path(tmp_dir) / "my_awesome_file.py").exists()
+ with open(Path(tmp_dir) / "my_awesome_file.py") as f:
+ file_content = f.read()
+ assert "NamedFileTestScene(Scene):" in file_content
+ result = runner.invoke(
+ main, command_unnamed, prog_name="manim", input="Default\n"
+ )
+ assert (Path(tmp_dir) / "main.py").exists()
+ with open(Path(tmp_dir) / "main.py") as f:
+ file_content = f.read()
+ assert "DefaultFileTestScene(Scene):" in file_content
+
+
def test_manim_new_command():
command = ["new"]
runner = CliRunner()