Skip to content

The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.

Python

Make the folder a video or a storyboard is written in

Mert Kayad740988a659d
diff --git a/docs/content/changelog.md b/docs/content/changelog.md
index dc121dc1..4b0c995a 100644
--- a/docs/content/changelog.md
+++ b/docs/content/changelog.md
@@ -41,6 +41,7 @@ The notable changes to ManimGX, release by release. The format follows
- Redraw a shape that empties with `always_redraw` without a RecursionError, and keep a mobject's updaters off the member its points move into when a transform aligns it, so a relative updater such as `always_shift` no longer runs twice on them ([#32](https://github.com/academa-labs/manimgx/issues/32)).
- Measure a text's size in `inspect` and `render` at 1080p, which is 1080 px on a video's short side, so a tall video (1080x1920) no longer reports readable text as small ([#37](https://github.com/academa-labs/manimgx/issues/37)).
+- Make the folder a video or a storyboard is written in when it is missing (`render -o new/out.mp4`, `inspect -o new/sheet.png` and `Scene.render`), instead of failing with "No such file or directory" ([#34](https://github.com/academa-labs/manimgx/issues/34)).
- Keep resources needed by queued video frames until their transparency retries finish, fixing render failures during cache cleanup ([#16](https://github.com/academa-labs/manimgx/issues/16)).
- Draw a storyboard picture without dropping the frame the film holds back, fixing `manimgx render` failures ("unknown shape") under memory pressure.
- Draw a vectorized curve whose function gives a component as one number (`use_vectorized` with `lambda t: (t, t**2, 0)`), which failed with numpy's inhomogeneous-shape error.
diff --git a/src/manimgx/cli/storyboard.py b/src/manimgx/cli/storyboard.py
index cd973c63..fdeb913e 100644
--- a/src/manimgx/cli/storyboard.py
+++ b/src/manimgx/cli/storyboard.py
@@ -81,6 +81,7 @@ class Sheets:
out = self.path.with_name(
self.path.name if n == 1 else f"{self.path.stem}-{n}{self.path.suffix}"
)
+ out.parent.mkdir(parents=True, exist_ok=True)
self._writes.append(
self._writer.submit(_sheet(self._tiles).save, out, compress_level=3)
)
diff --git a/src/manimgx/rendering/film.py b/src/manimgx/rendering/film.py
index 3bdd7f2e..3d9329e7 100644
--- a/src/manimgx/rendering/film.py
+++ b/src/manimgx/rendering/film.py
@@ -8,6 +8,7 @@ still made); each play, as it ends, to a callback. The sounds are mixed as the v
import contextlib
import json
import os
+import pathlib
from collections.abc import Callable
from dataclasses import asdict, dataclass
from fractions import Fraction
@@ -227,7 +228,8 @@ class Film:
scene runs (`manimgx preview`), or in the browser, where Python has no GPU (Pyodide).
Args:
- video: The MP4 file the frames are encoded into as they come, if any.
+ video: The MP4 file the frames are encoded into as they come, if any; its folder
+ is made if it is missing.
frames: A function handed each frame, a [`Frame`][manimgx.rendering.film.Frame] (see
[`FrameSink`][manimgx.rendering.film.FrameSink]).
plays: A function called as each play ends (see
@@ -278,6 +280,7 @@ class Film:
_engine.start_gpu() # the film draws: the GPU comes up as it begins
self._player = drawn = gpu(width, height)
if video is not None:
+ pathlib.Path(video).parent.mkdir(parents=True, exist_ok=True)
drawn.begin_export(
os.fspath(video), fps=int(fps), preset=preset, crf=crf
)
diff --git a/src/manimgx/scene.py b/src/manimgx/scene.py
index 416737c6..7ccc3a1f 100644
--- a/src/manimgx/scene.py
+++ b/src/manimgx/scene.py
@@ -644,7 +644,7 @@ class Scene:
Args:
video: The MP4 file to write, if any, at the size and frame rate of
[`config`][manimgx.config.config] (a whole number of frames a second,
- and an even width and height).
+ and an even width and height); its folder is made if it is missing.
preset: The x264 encoding preset; faster presets make larger files.
crf: The x264 constant rate factor, from 0 to 51; lower values give
higher quality.
diff --git a/tests/integration/test_cli_export.py b/tests/integration/test_cli_export.py
index c78bd93b..0cb2ff7b 100644
--- a/tests/integration/test_cli_export.py
+++ b/tests/integration/test_cli_export.py
@@ -7,8 +7,9 @@ from pathlib import Path
import pytest
import typer
from PIL import Image
+from typer.testing import CliRunner
-from manimgx.cli import export
+from manimgx.cli import app, export
from manimgx.cli.export import inspect
from manimgx.cli.storyboard import Sheets
@@ -82,3 +83,33 @@ def test_a_scene_error_finishes_its_storyboard_writer(
finally:
sheets._writer.shutdown()
sys.modules.pop(source.stem, None)
+
+
+def test_an_output_in_a_missing_folder_is_written_there(tmp_path: Path) -> None:
+ """`-o` names where a command writes, and the folder is made as its files are written:
+ the video, the storyboard and the page, each in a folder that does not exist yet."""
+ source = tmp_path / "scene.py"
+ source.write_text(
+ "import manimgx as m\n"
+ "m.config.pixel_width = m.config.pixel_height = 16\n"
+ "m.config.frame_rate = 10\n"
+ "class Only(m.Scene):\n"
+ " def construct(self):\n"
+ " self.wait(0.1)\n",
+ encoding="utf-8",
+ )
+ runner = CliRunner()
+ video = tmp_path / "new" / "folder" / "out.mp4"
+ result = runner.invoke(app, ["render", str(source), "-o", str(video)])
+ assert result.exit_code == 0, result.output
+ assert video.exists()
+ assert video.with_name("out.storyboard.png").exists()
+ storyboard = tmp_path / "sheets" / "sheet.png"
+ result = runner.invoke(app, ["inspect", str(source), "-o", str(storyboard)])
+ assert result.exit_code == 0, result.output
+ assert storyboard.exists()
+ deck = tmp_path / "deck" / "talk.mp4"
+ result = runner.invoke(app, ["present", str(source), "-o", str(deck), "--no-open"])
+ assert result.exit_code == 0, result.output
+ assert deck.exists()
+ assert deck.with_suffix(".html").exists()
diff --git a/tests/integration/test_film.py b/tests/integration/test_film.py
index 7a861619..70d0b6b3 100644
--- a/tests/integration/test_film.py
+++ b/tests/integration/test_film.py
@@ -42,6 +42,13 @@ def test_a_cut_at_the_last_frame_keeps_the_film(tmp_path: Path) -> None:
assert (tmp_path / "held.mp4").stat().st_size > 0
+def test_a_video_makes_the_folder_it_is_written_in(tmp_path: Path) -> None:
+ video = tmp_path / "new" / "folder" / "held.mp4"
+ Held().render(video)
+ assert video.stat().st_size > 0
+ assert sorted(p.name for p in video.parent.iterdir()) == ["held.mp4"]
+
+
def test_an_error_at_the_last_frame_abandons_the_video(tmp_path: Path) -> None:
_, sink = at_the_last(ValueError("the sink failed"))
with pytest.raises(ValueError, match="the sink failed"):
diff --git a/tests/unit/cli/test_storyboard.py b/tests/unit/cli/test_storyboard.py
index 5b0a4225..d42e727e 100644
--- a/tests/unit/cli/test_storyboard.py
+++ b/tests/unit/cli/test_storyboard.py
@@ -40,11 +40,20 @@ def test_a_sound_is_named_by_its_length() -> None:
)
+def test_sheets_make_the_folder_they_are_written_in(tmp_path: Path) -> None:
+ sheet = tmp_path / "new" / "folder" / "storyboard.png"
+ sheets = Sheets(sheet)
+ sheets.add("a frame", Image.new("RGB", (2, 2)))
+ assert sheets.close() == [sheet]
+ assert sheet.is_file()
+
+
def test_a_failed_sheet_write_closes_the_writer(tmp_path: Path) -> None:
- sheets = Sheets(tmp_path / "missing" / "storyboard.png")
+ (tmp_path / "storyboard.png").mkdir() # a folder where the sheet goes
+ sheets = Sheets(tmp_path / "storyboard.png")
sheets.add("a frame", Image.new("RGB", (2, 2)))
try:
- with pytest.raises(FileNotFoundError):
+ with pytest.raises(OSError, match=r"storyboard\.png"):
sheets.close()
with pytest.raises(RuntimeError, match="after shutdown"):
sheets._writer.submit(lambda: None)