A community-maintained Python framework for creating mathematical animations.
OpenGL: Add caching, skipping, and user-specified background colors to the OpenGL renderer (#1123)
Mohammad Al-Fetyani01135d4ed402
diff --git a/manim/_config/utils.py b/manim/_config/utils.pyindex 70e497659..67dbb6227 100644--- a/manim/_config/utils.py+++ b/manim/_config/utils.py@@ -1073,8 +1073,6 @@ class ManimConfig(MutableMapping):@use_opengl_renderer.setterdef use_opengl_renderer(self, val: bool) -> None:self._d["use_opengl_renderer"] = val- if val:- self["disable_caching"] = True@propertydef use_webgl_renderer(self):diff --git a/manim/mobject/types/opengl_surface.py b/manim/mobject/types/opengl_surface.pyindex ead3d3556..80acda9b5 100644--- a/manim/mobject/types/opengl_surface.py+++ b/manim/mobject/types/opengl_surface.py@@ -124,7 +124,7 @@ class OpenGLSurface(OpenGLMobject):return normalize_along_axis(normals, 1)def pointwise_become_partial(self, smobject, a, b, axis=None):- assert isinstance(smobject, Surface)+ assert isinstance(smobject, OpenGLSurface)if axis is None:axis = self.prefered_creation_axisif a <= 0 and b >= 1:diff --git a/manim/renderer/cairo_renderer.py b/manim/renderer/cairo_renderer.pyindex b1833aeda..eb7be4a08 100644--- a/manim/renderer/cairo_renderer.py+++ b/manim/renderer/cairo_renderer.py@@ -1,3 +1,4 @@+import timeimport numpy as npfrom .. import configfrom ..utils.iterables import list_update@@ -36,6 +37,7 @@ def handle_play_like_call(func):"""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)diff --git a/manim/renderer/opengl_renderer.py b/manim/renderer/opengl_renderer.pyindex 113b25d78..01297b7c9 100644--- a/manim/renderer/opengl_renderer.py+++ b/manim/renderer/opengl_renderer.py@@ -1,3 +1,7 @@+from manim.utils.exceptions import EndSceneEarlyException+from manim.utils.caching import handle_caching_play+from manim.renderer.cairo_renderer import pass_scene_reference, handle_play_like_call+from manim.utils.color import color_to_rgbaimport modernglfrom .opengl_renderer_window import Windowfrom .shader_wrapper import ShaderWrapper@@ -177,11 +181,15 @@ JOINT_TYPE_MAP = {class OpenGLRenderer:- def __init__(self):+ def __init__(self, skip_animations=False):# Measured in pixel widths, used for vector graphicsself.anti_alias_width = 1.5++ self.original_skipping_status = skip_animations+ self.skip_animations = skip_animations+ self.animations_hashes = []self.num_plays = 0- self.skip_animations = False+self.camera = OpenGLCamera()self.pressed_keys = set()@@ -215,6 +223,12 @@ class OpenGLRenderer:moderngl.ONE,)+ # Initialize shader map.+ self.id_to_shader_program = {}++ # Initialize texture map.+ self.path_to_texture_id = {}+def update_depth_test(self, context, shader_wrapper):if shader_wrapper.depth_test:self.context.enable(moderngl.DEPTH_TEST)@@ -348,25 +362,28 @@ class OpenGLRenderer:except KeyError:pass+ def update_skipping_status(self):+ """+ This method is used internally to check if the current+ animation needs to be skipped or not. It also checks if+ the number of animations that were played correspond to+ the number of animations that need to be played, and+ raises an EndSceneEarlyException if they don't correspond.+ """+ if config["from_animation_number"]:+ if self.num_plays < config["from_animation_number"]:+ self.skip_animations = True+ if config["upto_animation_number"]:+ if self.num_plays > config["upto_animation_number"]:+ self.skip_animations = True+ raise EndSceneEarlyException()++ @handle_caching_play+ @handle_play_like_calldef play(self, scene, *args, **kwargs):- if len(args) == 0:- logger.warning("Called Scene.play with no animations")- return-# TODO: Handle data locking / unlocking.if scene.compile_animation_data(*args, **kwargs):- self.animation_start_time = time.time()- self.animation_elapsed_time = 0-- temp_name = f"media/temp_{self.num_plays}.mp4"- self.partial_movie_files.append(temp_name)- self.file_writer.begin_animation(- not self.skip_animations, file_path=temp_name- )scene.play_internal()- self.file_writer.end_animation(not self.skip_animations)-- self.num_plays += 1def render(self, scene, frame_offset, moving_mobjects):def update_frame():@@ -375,9 +392,12 @@ class OpenGLRenderer:self.render_mobjects(scene.mobjects)self.animation_elapsed_time = time.time() - self.animation_start_time- window_background_color = (0.2, 0.2, 0.2, 1)+ window_background_color = color_to_rgba(config["background_color"])update_frame()+ if self.skip_animations:+ return+if config["write_to_movie"]:self.file_writer.write_frame(self)@@ -388,7 +408,7 @@ class OpenGLRenderer:self.window.swap_buffers()def scene_finished(self, scene):- self.file_writer.finish(self.partial_movie_files)+ self.file_writer.finish()def save_static_frame_data(self, scene, static_mobjects):passdiff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.pyindex fb694d722..5b06c5e56 100644--- a/manim/scene/scene_file_writer.py+++ b/manim/scene/scene_file_writer.py@@ -430,10 +430,7 @@ class SceneFileWriter(object):# which effectively has cuts at all the places you might want. But for# viewing the scene as a whole, one of course wants to see it as a# single piece.- if not config["use_opengl_renderer"]:- partial_movie_files = [- el for el in self.partial_movie_files if el is not None- ]+ partial_movie_files = [el for el in self.partial_movie_files if el is not None]# NOTE : Here we should do a check and raise an exception if partial# movie file is empty. We can't, as a lot of stuff (in particular, in# tests) use scene initialization, and this error would be raised as