Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Cleanup: ensure ``Scene.renderer.time`` returns something that makes sense (#2220)

Benjamin Hackl81fa93de8cec
diff --git a/manim/renderer/cairo_renderer.py b/manim/renderer/cairo_renderer.py
index 2c619208c..574a384de 100644
--- a/manim/renderer/cairo_renderer.py
+++ b/manim/renderer/cairo_renderer.py
@@ -13,43 +13,6 @@ from ..utils.exceptions import EndSceneEarlyException
from ..utils.iterables import list_update
-def handle_play_like_call(func):
- """
- This method is used internally to wrap the
- passed function, into a function that
- actually writes to the video stream.
- Simultaneously, it also adds to the number
- of animations played.
-
- Parameters
- ----------
- func : function
- The play() like function that has to be
- written to the video file stream.
-
- Returns
- -------
- function
- The play() like function that can now write
- to the video file stream.
- """
-
- # NOTE : This is only kept for OpenGL renderer.
- # The play logic of the cairo renderer as been refactored and does not need this function anymore.
- # When OpenGL renderer will have a proper testing system,
- # the play logic of the latter has to be refactored in the same way the cairo renderer has been, and thus this
- # method has to be deleted.
-
- def wrapper(self, scene, *args, **kwargs):
- self.animation_start_time = time.time()
- self.file_writer.begin_animation(not self.skip_animations)
- func(self, scene, *args, **kwargs)
- self.file_writer.end_animation(not self.skip_animations)
- self.num_plays += 1
-
- return wrapper
-
-
class CairoRenderer:
"""A renderer using Cairo.
@@ -95,6 +58,7 @@ class CairoRenderer:
if self.skip_animations:
logger.debug(f"Skipping animation {self.num_plays}")
hash_current_animation = None
+ self.time += scene.duration
else:
if config["disable_caching"]:
logger.info("Caching disabled.")
@@ -112,6 +76,7 @@ class CairoRenderer:
{"hash_current_animation": hash_current_animation},
)
self.skip_animations = True
+ self.time += scene.duration
# 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)
@@ -204,9 +169,9 @@ class CairoRenderer:
The number of times to add frame.
"""
dt = 1 / self.camera.frame_rate
- self.time += num_frames * dt
if self.skip_animations:
return
+ self.time += num_frames * dt
for _ in range(num_frames):
self.file_writer.write_frame(frame)
diff --git a/manim/renderer/opengl_renderer.py b/manim/renderer/opengl_renderer.py
index 317765e13..b3b506573 100644
--- a/manim/renderer/opengl_renderer.py
+++ b/manim/renderer/opengl_renderer.py
@@ -6,7 +6,6 @@ import numpy as np
from PIL import Image
from manim import config, logger
-from manim.renderer.cairo_renderer import handle_play_like_call
from manim.utils.caching import handle_caching_play
from manim.utils.color import color_to_rgba
from manim.utils.exceptions import EndSceneEarlyException
@@ -225,6 +224,8 @@ class OpenGLRenderer:
self._original_skipping_status = skip_animations
self.skip_animations = skip_animations
self.animation_start_time = 0
+ self.animation_elapsed_time = 0
+ self.time = 0
self.animations_hashes = []
self.num_plays = 0
@@ -400,13 +401,19 @@ class OpenGLRenderer:
raise EndSceneEarlyException()
@handle_caching_play
- @handle_play_like_call
def play(self, scene, *args, **kwargs):
# TODO: Handle data locking / unlocking.
+ self.animation_start_time = time.time()
+ self.file_writer.begin_animation(not self.skip_animations)
+
if scene.compile_animation_data(*args, **kwargs):
scene.begin_animations()
scene.play_internal()
+ self.file_writer.end_animation(not self.skip_animations)
+ self.time += scene.duration
+ self.num_plays += 1
+
def clear_screen(self):
self.frame_buffer_object.clear(*self.background_color)
self.window.swap_buffers()
diff --git a/tests/test_scene.py b/tests/test_scene.py
index 3c34a7915..f842864df 100644
--- a/tests/test_scene.py
+++ b/tests/test_scene.py
@@ -1,6 +1,6 @@
import pytest
-from manim import Mobject, Scene, tempconfig
+from manim import Circle, FadeIn, Mobject, Scene, Square, config, tempconfig
def test_scene_add_remove():
@@ -33,3 +33,16 @@ def test_scene_add_remove():
# Check that Scene.remove() returns the instance (for chained calls)
assert scene.add(Mobject()) is scene
+
+
+def test_scene_time():
+ with tempconfig({"dry_run": True}):
+ scene = Scene()
+ assert scene.renderer.time == 0
+ scene.wait(2)
+ assert scene.renderer.time == 2
+ scene.play(FadeIn(Circle()), run_time=0.5)
+ assert pytest.approx(scene.renderer.time) == 2.5
+ scene.renderer._original_skipping_status = True
+ scene.play(FadeIn(Square()), run_time=5) # this animation gets skipped.
+ assert pytest.approx(scene.renderer.time) == 7.5