The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.
Python
Draw a storyboard picture without dropping the frame the film holds back
Sina Atalayc614b4ee1504
diff --git a/docs/content/changelog.md b/docs/content/changelog.mdindex e12f7989..372a9252 100644--- a/docs/content/changelog.md+++ b/docs/content/changelog.md@@ -34,6 +34,7 @@ The notable changes to ManimGX, release by release. The format follows### Fixed- 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.- Preserve the benchmark comparison images' aspect ratios when PyPI narrows the README table.- Credit Filament, whose code the 3D lighting is adapted from, in the README and the wheels' third-party notices.diff --git a/src/manimgx/rendering/feed.py b/src/manimgx/rendering/feed.pyindex dc6cea8c..30cfab27 100644--- a/src/manimgx/rendering/feed.py+++ b/src/manimgx/rendering/feed.py@@ -564,7 +564,8 @@ class Feeder:self.swept = 0# sweeps that evicted: a record made since draws only what is residentself.evictions = 0- self.latest: list[bytes] = [] # the records of the last frame made+ # the records of the film's last frame made, and of its camera views (not a picture's)+ self.latest: list[bytes] = []# ── residency ────────────────────────────────────────────────────────────def shown(self, records: np.ndarray, last: int) -> None:@@ -872,12 +873,26 @@ class Feeder:def frame(self, camera: "Camera", mobjects: "list[Mobject]") -> tuple[bytes, bytes, list[CameraView]]:- """(view, records, camera views to draw first) — the arguments of `Player.render`."""+ """(view, records, camera views to draw first) of the film's next frame: the arguments+ of `Player.render`. It is the latest frame, which the film may hold back (see `sweep`).+ """+ uniform, records, cameras = self.picture(camera, mobjects)+ self.latest = [records, *[c[4] for c in cameras]]+ if self.frames % MARK == 0:+ for data in self.latest:+ self.shown(np.frombuffer(data, RECORD), self.frames)+ self.frames += 1+ return uniform, records, cameras++ def picture(+ self, camera: "Camera", mobjects: "list[Mobject]"+ ) -> tuple[bytes, bytes, list[CameraView]]:+ """(view, records, camera views to draw first) of the world now, drawn at once: not a+ frame of the film, so the latest frame, which the film may still hold back, stays the+ one a sweep keeps."""self.sweep()- self.latest = []cameras: list[CameraView] = []uniform, records = self.view(camera, mobjects, self.width, self.height, cameras)- self.frames += 1return uniform, records, camerasdef camera_view(@@ -924,10 +939,6 @@ class Feeder:)])- if data:- self.latest.append(data)- if self.frames % MARK == 0:- self.shown(np.frombuffer(data, RECORD), self.frames)return uniform, datadef record(diff --git a/src/manimgx/rendering/film.py b/src/manimgx/rendering/film.pyindex 91957eb8..72c86efe 100644--- a/src/manimgx/rendering/film.py+++ b/src/manimgx/rendering/film.py@@ -417,7 +417,7 @@ class Film:Returns:Its pixels: RGBA, a byte a channel, row after row from the top."""- view, records, cameras = self.feeder.frame(camera, mobjects)+ view, records, cameras = self.feeder.picture(camera, mobjects)if self._player is None:raise RuntimeError("a film recorded as a take is drawn by ManimGX's player")if self._player.pressured():diff --git a/tests/integration/test_film.py b/tests/integration/test_film.pyindex a2b74030..7a861619 100644--- a/tests/integration/test_film.py+++ b/tests/integration/test_film.py@@ -1,5 +1,6 @@"""A film's hooks: each frame handed over as it is sent, and `Cut`, which ends the film wherever-a hook raises it — at its last frame too, which is sent only when the film closes."""+a hook raises it — at its last frame too, which is sent only when the film closes. A storyboard's+picture drawn between frames keeps what the frame the film holds back draws."""from collections.abc import Callablefrom pathlib import Path@@ -9,7 +10,8 @@ import pytestimport manimgx as mfrom manimgx.config import config-from manimgx.rendering.film import Cut, Frame, X264Preset+from manimgx.rendering import feed+from manimgx.rendering.film import Cut, Film, Frame, X264Presetclass Held(m.Scene):@@ -88,3 +90,21 @@ def test_an_invalid_crf_fails_before_writing(tmp_path: Path, crf: float) -> None):Held().render(tmp_path / "bad.mp4", crf=crf)assert list(tmp_path.iterdir()) == []+++def test_a_storyboards_picture_between_frames_keeps_what_the_held_frame_draws(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """`manimgx inspect` draws a storyboard's picture between a film's frames. Had the picture+ taken the place of the frame the film holds back, the sweep before the next frame would+ evict what that frame draws, and drawing it would fail with "unknown shape"."""+ monkeypatch.setattr(feed, "KEEP", 0) # what no frame shows now goes at a sweep+ monkeypatch.setattr(feed, "SWEEP", 1) # and there is a sweep at every frame+ drawn: list[bytes] = []+ film = Film(frames=lambda frame: drawn.append(frame.pixels()))+ camera = m.Camera()+ film.record(camera, [m.Square(fill_opacity=1)]) # held back until the next frame+ film.picture(camera, [m.Circle(fill_opacity=1)])+ film.record(camera, [m.Triangle(fill_opacity=1)]) # sends the square's frame+ film.close()+ assert len(drawn) == 2diff --git a/tests/unit/rendering/test_feed.py b/tests/unit/rendering/test_feed.pyindex 190b1247..a1697403 100644--- a/tests/unit/rendering/test_feed.py+++ b/tests/unit/rendering/test_feed.py@@ -11,6 +11,9 @@ keeps the player's store in step with its own books; the player derives what dra- A record's reveal window is over u, which counts its shape's steps: a path's curves, acloud's points, a mesh's triangles, a surface's faces.- A surface's part uploads the faces it draws, cut from its whole lattice as it draws them.+- A picture (a storyboard's) is not one of the film's frames: drawn between them, it leaves+ the latest frame, which the film may still hold back, the one a sweep keeps, and the count+ of frames as it was.- A batch of a reveal's frames has each frame's window, as the frame alone would.- A path and a cloud of points drawn in one frame draw as each does alone.- Over any run of frames of any mobjects (paths, graded paths, colored clouds and meshes,@@ -921,3 +924,16 @@ class Residency(RuleBasedStateMachine):TestResidency = Residency.TestCaseTestResidency.settings = settings(max_examples=60, stateful_step_count=40)+++def test_a_picture_is_not_one_of_the_films_frames() -> None:+ sink = Sink()+ feeder = feed.Feeder(32, 32, sink) # ty: ignore[invalid-argument-type]+ camera = m.Camera()+ feeder.frame(camera, [m.Square(fill_opacity=1)])+ held = set(sink.resident)+ frames, latest = feeder.frames, feeder.latest+ feeder.picture(camera, [m.RegularPolygon(5, fill_opacity=1)])+ assert (feeder.frames, feeder.latest) == (frames, latest)+ feeder.sweep(pressed=True) # the picture's shapes may go: never the held frame's+ assert held <= set(sink.resident)