Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Improved last frame output handling for the OpenGL renderer (#2205)

Ryan McCauley7faead9c22c4
diff --git a/manim/_config/default.cfg b/manim/_config/default.cfg
index 4f0083a75..f80c07707 100644
--- a/manim/_config/default.cfg
+++ b/manim/_config/default.cfg
@@ -154,6 +154,9 @@ disable_caching_warning = False
# --enable_wireframe
enable_wireframe = False
+# --dry_run
+dry_run = False
+
# Default tex_template
# --tex_template
tex_template =
diff --git a/manim/_config/utils.py b/manim/_config/utils.py
index ad43b7a63..100b6dbb9 100644
--- a/manim/_config/utils.py
+++ b/manim/_config/utils.py
@@ -245,6 +245,7 @@ class ManimConfig(MutableMapping):
"custom_folders",
"disable_caching",
"disable_caching_warning",
+ "dry_run",
"enable_wireframe",
"ffmpeg_loglevel",
"format",
@@ -700,6 +701,7 @@ class ManimConfig(MutableMapping):
"zero_pad",
"enable_wireframe",
"force_window",
+ "dry_run",
]:
if hasattr(args, key):
attr = getattr(args, key)
@@ -708,10 +710,6 @@ class ManimConfig(MutableMapping):
if attr is not None:
self[key] = attr
- # dry_run is special because it can only be set to True
- if getattr(args, "dry_run", False):
- self["dry_run"] = True
-
for key in [
"media_dir", # always set this one first
"log_dir",
@@ -914,6 +912,12 @@ class ManimConfig(MutableMapping):
doc="Set to force window when using the opengl renderer",
)
+ dry_run = property(
+ lambda self: self._d["dry_run"],
+ lambda self, val: self._set_boolean("dry_run", val),
+ doc="Enable dry_run so that no output files are generated and window is disabled.",
+ )
+
@property
def verbosity(self):
"""Logger verbosity; "DEBUG", "INFO", "WARNING", "ERROR", or "CRITICAL" (-v)."""
@@ -1139,27 +1143,16 @@ class ManimConfig(MutableMapping):
@property
def dry_run(self):
"""Whether dry run is enabled."""
- return (
- self.write_to_movie is False
- and self.write_all is False
- and self.save_last_frame is False
- and not self.format
- )
+ return self._d["dry_run"]
@dry_run.setter
def dry_run(self, val: bool) -> None:
+ self._d["dry_run"] = val
if val:
self.write_to_movie = False
self.write_all = False
self.save_last_frame = False
self.format = None
- else:
- raise ValueError(
- "It is unclear what it means to set dry_run to "
- "False. Instead, try setting each option "
- "individually. (write_to_movie, write_all, "
- "save_last_frame, save_pngs, or save_as_gif)",
- )
@property
def renderer(self):
diff --git a/manim/cli/render/global_options.py b/manim/cli/render/global_options.py
index 2b9aa6722..5d1d59848 100644
--- a/manim/cli/render/global_options.py
+++ b/manim/cli/render/global_options.py
@@ -90,4 +90,10 @@ global_options = option_group(
help="Force window to open when using the opengl renderer, intended for debugging as it may impact performance",
default=False,
),
+ option(
+ "--dry_run",
+ is_flag=True,
+ help="Renders animations without outputting image or video files and disables the window",
+ default=False,
+ ),
)
diff --git a/manim/renderer/opengl_renderer.py b/manim/renderer/opengl_renderer.py
index 61363e653..317765e13 100644
--- a/manim/renderer/opengl_renderer.py
+++ b/manim/renderer/opengl_renderer.py
@@ -282,6 +282,7 @@ class OpenGLRenderer:
and not config["save_last_frame"]
and not config["format"]
and not config["write_to_movie"]
+ and not config["dry_run"]
)
def get_pixel_shape(self):
@@ -443,12 +444,22 @@ class OpenGLRenderer:
def scene_finished(self, scene):
# When num_plays is 0, no images have been output, so output a single
# image in this case
- if config["save_last_frame"] or (
- config["format"] == "png" and self.num_plays == 0
- ):
+ if self.num_plays > 0:
+ self.file_writer.finish()
+ elif self.num_plays == 0 and config.write_to_movie:
+ config.write_to_movie = False
+
+ if self.should_save_last_frame():
+ config.save_last_frame = True
self.update_frame(scene)
self.file_writer.save_final_image(self.get_image())
- self.file_writer.finish()
+
+ def should_save_last_frame(self):
+ if config["save_last_frame"]:
+ return True
+ if self.scene.interactive_mode:
+ return False
+ return self.num_plays == 0
def get_image(self) -> Image.Image:
"""Returns an image from the current frame. The first argument passed to image represents
diff --git a/manim/scene/scene.py b/manim/scene/scene.py
index c854748a2..f81f5d98f 100644
--- a/manim/scene/scene.py
+++ b/manim/scene/scene.py
@@ -115,6 +115,7 @@ class Scene:
self.ambient_light = None
self.key_to_function_map = {}
self.mouse_press_callbacks = []
+ self.interactive_mode = False
if config.renderer == "opengl":
# Items associated with interaction
@@ -1036,6 +1037,9 @@ class Scene:
elif not self.renderer.window:
logger.warning("Disabling interactive embed as no window was created")
return False
+ elif config.dry_run:
+ logger.warning("Disabling interactive embed as dry_run is enabled")
+ return False
return True
def interactive_embed(self):
@@ -1044,6 +1048,7 @@ class Scene:
"""
if not self.check_interactive_embed_is_valid():
return
+ self.interactive_mode = True
def ipython(shell, namespace):
import manim
diff --git a/tests/opengl/test_config_opengl.py b/tests/opengl/test_config_opengl.py
index b2a9a7b9b..764240824 100644
--- a/tests/opengl/test_config_opengl.py
+++ b/tests/opengl/test_config_opengl.py
@@ -121,7 +121,9 @@ def test_temporary_dry_run(using_opengl_renderer):
def test_dry_run_with_png_format(using_opengl_renderer):
"""Test that there are no exceptions when running a png without output"""
- with tempconfig({"write_to_movie": False, "disable_caching": True}):
+ with tempconfig(
+ {"dry_run": True, "write_to_movie": False, "disable_caching": True}
+ ):
assert config["dry_run"] is True
scene = MyScene()
scene.render()
@@ -129,7 +131,9 @@ def test_dry_run_with_png_format(using_opengl_renderer):
def test_dry_run_with_png_format_skipped_animations(using_opengl_renderer):
"""Test that there are no exceptions when running a png without output and skipped animations"""
- with tempconfig({"write_to_movie": False, "disable_caching": True}):
+ with tempconfig(
+ {"dry_run": True, "write_to_movie": False, "disable_caching": True}
+ ):
assert config["dry_run"] is True
scene = MyScene(skip_animations=True)
scene.render()
diff --git a/tests/test_config.py b/tests/test_config.py
index 3d57f6a03..5e7698aae 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -190,7 +190,9 @@ def test_temporary_dry_run():
def test_dry_run_with_png_format():
"""Test that there are no exceptions when running a png without output"""
- with tempconfig({"write_to_movie": False, "disable_caching": True}):
+ with tempconfig(
+ {"dry_run": True, "write_to_movie": False, "disable_caching": True}
+ ):
assert config["dry_run"] is True
scene = MyScene()
scene.render()
@@ -198,7 +200,9 @@ def test_dry_run_with_png_format():
def test_dry_run_with_png_format_skipped_animations():
"""Test that there are no exceptions when running a png without output and skipped animations"""
- with tempconfig({"write_to_movie": False, "disable_caching": True}):
+ with tempconfig(
+ {"dry_run": True, "write_to_movie": False, "disable_caching": True}
+ ):
assert config["dry_run"] is True
scene = MyScene(skip_animations=True)
scene.render()
diff --git a/tests/test_scene_rendering/opengl/test_cli_flags_opengl.py b/tests/test_scene_rendering/opengl/test_cli_flags_opengl.py
index 68023a33f..0b0ae67b5 100644
--- a/tests/test_scene_rendering/opengl/test_cli_flags_opengl.py
+++ b/tests/test_scene_rendering/opengl/test_cli_flags_opengl.py
@@ -1,3 +1,4 @@
+import os
import sys
import numpy as np
@@ -108,6 +109,118 @@ def test_s_flag_no_animations(tmp_path, manim_cfg_file, simple_scenes_path):
assert not is_empty, "running manim with -s flag did not render an image"
+def test_image_output_for_static_scene(tmp_path, manim_cfg_file, simple_scenes_path):
+ scene_name = "StaticScene"
+ command = [
+ sys.executable,
+ "-m",
+ "manim",
+ "--renderer",
+ "opengl",
+ "-ql",
+ "--media_dir",
+ str(tmp_path),
+ simple_scenes_path,
+ scene_name,
+ ]
+ out, err, exit_code = capture(command)
+ assert exit_code == 0, err
+
+ exists = (tmp_path / "videos").exists()
+ assert not exists, "running manim with static scene rendered a video"
+
+ is_empty = not any((tmp_path / "images" / "simple_scenes").iterdir())
+ assert not is_empty, "running manim without animations did not render an image"
+
+
+def test_no_image_output_with_interactive_embed(
+ tmp_path, manim_cfg_file, simple_scenes_path
+):
+ """Check no image is output for a static scene when interactive embed is called"""
+ scene_name = "InteractiveStaticScene"
+ command = [
+ sys.executable,
+ "-m",
+ "manim",
+ "--renderer",
+ "opengl",
+ "-ql",
+ "--media_dir",
+ str(tmp_path),
+ simple_scenes_path,
+ scene_name,
+ ]
+ out, err, exit_code = capture(command)
+ assert exit_code == 0, err
+
+ exists = (tmp_path / "videos").exists()
+ assert not exists, "running manim with static scene rendered a video"
+
+ is_empty = len(os.listdir(tmp_path / "images" / "simple_scenes")) == 0
+ assert (
+ is_empty
+ ), "running manim static scene with interactive embed rendered an image"
+
+
+def test_no_default_image_output_with_non_static_scene(
+ tmp_path, manim_cfg_file, simple_scenes_path
+):
+ scene_name = "SceneWithNonStaticWait"
+ command = [
+ sys.executable,
+ "-m",
+ "manim",
+ "--renderer",
+ "opengl",
+ "-ql",
+ "--media_dir",
+ str(tmp_path),
+ simple_scenes_path,
+ scene_name,
+ ]
+ out, err, exit_code = capture(command)
+ assert exit_code == 0, err
+
+ exists = (tmp_path / "videos").exists()
+ assert not exists, "running manim with static scene rendered a video"
+
+ is_empty = len(os.listdir(tmp_path / "images" / "simple_scenes")) == 0
+ assert (
+ is_empty
+ ), "running manim static scene with interactive embed rendered an image"
+
+
+def test_image_output_for_static_scene_with_write_to_movie(
+ tmp_path, manim_cfg_file, simple_scenes_path
+):
+ scene_name = "StaticScene"
+ command = [
+ sys.executable,
+ "-m",
+ "manim",
+ "--write_to_movie",
+ "--renderer",
+ "opengl",
+ "-ql",
+ "--media_dir",
+ str(tmp_path),
+ simple_scenes_path,
+ scene_name,
+ ]
+ out, err, exit_code = capture(command)
+ assert exit_code == 0, err
+
+ exists = len(os.listdir(tmp_path / "videos")) == 0
+ assert not exists, "running manim with static scene rendered a video"
+
+ is_empty = not any((tmp_path / "images" / "simple_scenes").iterdir())
+ assert not is_empty, "running manim without animations did not render an image"
+
+
@pytest.mark.slow
def test_s_flag(tmp_path, manim_cfg_file, simple_scenes_path):
scene_name = "SquareToCircle"
diff --git a/tests/test_scene_rendering/simple_scenes.py b/tests/test_scene_rendering/simple_scenes.py
index 94a92c183..af82eefb9 100644
--- a/tests/test_scene_rendering/simple_scenes.py
+++ b/tests/test_scene_rendering/simple_scenes.py
@@ -50,6 +50,19 @@ class SceneWithNonStaticWait(Scene):
self.wait()
+class StaticScene(Scene):
+ def construct(self):
+ dot = Dot().set_color(GREEN)
+ self.add(dot)
+
+
+class InteractiveStaticScene(Scene):
+ def construct(self):
+ dot = Dot().set_color(GREEN)
+ self.add(dot)
+ self.interactive_mode = True
+
+
class SceneWithSections(Scene):
def construct(self):
# this would be defined in a third party application using the segmented video API