Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fixed issue when an animation is cached, manim can't merge the partial movie files. (#1192)

Hugues Devimeux21ad14ed148a
diff --git a/manim/renderer/cairo_renderer.py b/manim/renderer/cairo_renderer.py
index d82412fef..372f34301 100644
--- a/manim/renderer/cairo_renderer.py
+++ b/manim/renderer/cairo_renderer.py
@@ -84,24 +84,23 @@ class CairoRenderer:
scene.compile_animation_data(*args, **kwargs)
- # If skip_animations is already True, we can skip all the caching process.
- if not config["disable_caching"] and not self.skip_animations:
- hash_current_animation = get_hash_from_play_call(
- scene, self.camera, scene.animations, scene.mobjects
- )
- if self.file_writer.is_already_cached(hash_current_animation):
- logger.info(
- f"Animation {self.num_plays} : Using cached data (hash : %(hash_current_animation)s)",
- {"hash_current_animation": hash_current_animation},
- )
- self.skip_animations = True
- else:
- hash_current_animation = f"uncached_{self.num_plays:05}"
-
if self.skip_animations:
logger.debug(f"Skipping animation {self.num_plays}")
hash_current_animation = None
-
+ else:
+ if config["disable_caching"]:
+ logger.info("Caching disabled.")
+ hash_current_animation = f"uncached_{self.num_plays:05}"
+ else:
+ hash_current_animation = get_hash_from_play_call(
+ scene, self.camera, scene.animations, scene.mobjects
+ )
+ if self.file_writer.is_already_cached(hash_current_animation):
+ logger.info(
+ f"Animation {self.num_plays} : Using cached data (hash : %(hash_current_animation)s)",
+ {"hash_current_animation": hash_current_animation},
+ )
+ self.skip_animations = True
# adding None as a partial movie file will make file_writer ignore the latter.
self.file_writer.add_partial_movie_file(hash_current_animation)
self.animations_hashes.append(hash_current_animation)
diff --git a/tests/assert_utils.py b/tests/assert_utils.py
new file mode 100644
index 000000000..039b57369
--- /dev/null
+++ b/tests/assert_utils.py
@@ -0,0 +1,25 @@
+import typing
+from os import PathLike
+from pathlib import Path
+from pprint import pformat
+
+
+def assert_file_exists(filepath: typing.Union[str, PathLike]) -> None:
+ """Assert if filepath points to an existing file. Print all the elements (files and dir) of the parent dir of the given filepath.
+
+ This is mostly to have better assert message than using a raw assert os.path.isfile(filepath).
+
+ Parameters
+ ----------
+ filepath
+ Filepath to check.
+
+ Raises
+ ------
+ AssertionError
+ If filepath does not point to a file (if the file does not exist or it's a dir).
+ """
+ path = Path(filepath)
+ if not path.is_file():
+ message = f"{path.absolute()} is not a file. Other elements in the parent directory are \n{pformat([path.name for path in list(path.parent.iterdir())])}"
+ raise AssertionError(message)
diff --git a/tests/test_scene_rendering/test_cairo_renderer.py b/tests/test_scene_rendering/test_cairo_renderer.py
index d29368588..2516719e7 100644
--- a/tests/test_scene_rendering/test_cairo_renderer.py
+++ b/tests/test_scene_rendering/test_cairo_renderer.py
@@ -1,17 +1,21 @@
+import os
+from unittest.mock import Mock, patch
+
from manim import *
-import pytest
+
+from ..assert_utils import assert_file_exists
from .simple_scenes import *
-from unittest.mock import Mock
def test_render(using_temp_config, disabling_caching):
scene = SquareToCircle()
renderer = scene.renderer
- renderer.update_frame = Mock()
- renderer.add_frame = Mock()
+ renderer.update_frame = Mock(wraps=renderer.update_frame)
+ renderer.add_frame = Mock(wraps=renderer.add_frame)
scene.render()
assert renderer.add_frame.call_count == config["frame_rate"]
assert renderer.update_frame.call_count == config["frame_rate"]
+ assert_file_exists(config["output_file"])
def test_skipping_status_with_from_to_and_up_to(using_temp_config, disabling_caching):
@@ -29,3 +33,44 @@ def test_skipping_status_with_from_to_and_up_to(using_temp_config, disabling_cac
assert ((i >= 2) and (i <= 6)) or self.renderer.skip_animations
SceneWithMultipleCalls().render()
+
+
+def test_when_animation_is_cached(using_temp_config):
+ scene = SquareToCircle()
+ # Render twice to create a cache.
+ scene.render()
+ pmf = scene.renderer.file_writer.partial_movie_files
+ scene = SquareToCircle()
+ scene.update_to_time = Mock()
+ scene.render()
+ assert scene.renderer.file_writer.is_already_cached(
+ scene.renderer.animations_hashes[0]
+ )
+ # Check that the same partial movie files has been used (with he same hash)
+ assert pmf == scene.renderer.file_writer.partial_movie_files
+ # Check that manim correctly skipped the animation.
+ scene.update_to_time.assert_called_once_with(1)
+ # Check that the ouput video has been generated.
+ assert_file_exists(config["output_file"])
+
+
+def test_hash_logic_is_not_called_when_caching_is_disabled(
+ using_temp_config, disabling_caching
+):
+ with patch("manim.renderer.cairo_renderer.get_hash_from_play_call") as mocked:
+ scene = SquareToCircle()
+ scene.render()
+ mocked.assert_not_called()
+ assert_file_exists(config["output_file"])
+
+
+def test_hash_logic_is_called_when_caching_is_enabled(using_temp_config):
+ from manim.renderer.cairo_renderer import get_hash_from_play_call
+
+ with patch(
+ "manim.renderer.cairo_renderer.get_hash_from_play_call",
+ wraps=get_hash_from_play_call,
+ ) as mocked:
+ scene = SquareToCircle()
+ scene.render()
+ mocked.assert_called_once()