A community-maintained Python framework for creating mathematical animations.
Refactor skip animations (#696)
Leo Torresf37748109605
diff --git a/docs/source/changelog.rst b/docs/source/changelog.rstindex 774580826..63cd1427c 100644--- a/docs/source/changelog.rst+++ b/docs/source/changelog.rst@@ -18,6 +18,9 @@ FixesConfiguration^^^^^^^^^^^^^+#. Removed the ``skip_animations`` config option and added the+ ``Renderer.skip_animations`` attribute instead (via :pr:`696`).+#. The global ``config`` dict has been replaced by a global ``config`` instanceof the new class :class:`~.ManimConfig`. This class has a dict-like API, sothis should not break user code, only make it more robust. See thediff --git a/manim/_config/utils.py b/manim/_config/utils.pyindex 84d8168df..42560b1d9 100644--- a/manim/_config/utils.py+++ b/manim/_config/utils.py@@ -278,7 +278,6 @@ class ManimConfig(MutableMapping):"save_pngs","scene_names","show_in_file_browser",- "skip_animations","sound","tex_dir","tex_template_file",@@ -506,7 +505,6 @@ class ManimConfig(MutableMapping):"disable_caching","flush_cache","custom_folders",- "skip_animations","use_js_renderer",]:setattr(self, key, parser["CLI"].getboolean(key, fallback=False))@@ -906,12 +904,6 @@ class ManimConfig(MutableMapping):doc="Stop rendering animations at this nmber. Use -1 to avoid skipping (-n).",)- skip_animations = property(- lambda self: self._d["skip_animations"],- lambda self, val: self._set_boolean("skip_animations", val),- doc="Whether to skip the next animation. Internal use only.",- )-max_files_cached = property(lambda self: self._d["max_files_cached"],lambda self, val: self._set_pos_number("max_files_cached", val, True),diff --git a/manim/grpc/impl/frame_server_impl.py b/manim/grpc/impl/frame_server_impl.pyindex 851d7ec80..1865fe011 100644--- a/manim/grpc/impl/frame_server_impl.py+++ b/manim/grpc/impl/frame_server_impl.py@@ -124,7 +124,7 @@ class FrameServer(frameserver_pb2_grpc.FrameServerServicer):frame_response.frame_pending = Trueselected_scene.renderer_waiting = Truereturn frame_response- elif selected_scene.skip_animations:+ elif selected_scene.renderer.skip_animations:# Do nothingreturnelse:diff --git a/manim/renderer/cairo_renderer.py b/manim/renderer/cairo_renderer.pyindex 0d8399367..f55d883fc 100644--- a/manim/renderer/cairo_renderer.py+++ b/manim/renderer/cairo_renderer.py@@ -37,10 +37,9 @@ def handle_play_like_call(func):"""def wrapper(self, scene, *args, **kwargs):- allow_write = not config["skip_animations"]- self.file_writer.begin_animation(allow_write)+ self.file_writer.begin_animation(not self.skip_animations)func(self, scene, *args, **kwargs)- self.file_writer.end_animation(allow_write)+ self.file_writer.end_animation(not self.skip_animations)self.num_plays += 1return wrapper@@ -53,7 +52,7 @@ class CairoRenderer:time: time elapsed since initialisation of scene."""- def __init__(self, camera_class=None, **kwargs):+ def __init__(self, camera_class=None, skip_animations=False, **kwargs):# All of the following are set to EITHER the value passed via kwargs,# OR the value stored in the global config dict at the time of# _instance construction_. Before, they were in the CONFIG dict, which@@ -72,7 +71,8 @@ class CairoRenderer:self.video_quality_config[attr] = kwargs.get(attr, config[attr])camera_cls = camera_class if camera_class is not None else Cameraself.camera = camera_cls(self.video_quality_config)- self.original_skipping_status = config["skip_animations"]+ self.original_skipping_status = skip_animations+ self.skip_animations = skip_animationsself.animations_hashes = []self.num_plays = 0self.time = 0@@ -116,7 +116,7 @@ class CairoRenderer:**kwargs"""- if config["skip_animations"] and not ignore_skipping:+ if self.skip_animations and not ignore_skipping:returnif mobjects is None:mobjects = list_update(@@ -156,7 +156,7 @@ class CairoRenderer:"""dt = 1 / self.camera.frame_rateself.time += num_frames * dt- if config["skip_animations"]:+ if self.skip_animations:returnfor _ in range(num_frames):self.file_writer.write_frame(frame)@@ -179,29 +179,14 @@ class CairoRenderer:"""if config["from_animation_number"]:if self.num_plays < config["from_animation_number"]:- config["skip_animations"] = True+ self.skip_animations = Trueif config["upto_animation_number"]:if self.num_plays > config["upto_animation_number"]:- config["skip_animations"] = True+ self.skip_animations = Trueraise EndSceneEarlyException()- def revert_to_original_skipping_status(self):- """- Forces the scene to go back to its original skipping status,- by setting skip_animations to whatever it reads- from original_skipping_status.-- Returns- -------- Scene- The Scene, with the original skipping status.- """- if hasattr(self, "original_skipping_status"):- config["skip_animations"] = self.original_skipping_status- return self-def finish(self, scene):- config["skip_animations"] = False+ self.skip_animations = self.original_skipping_statusself.file_writer.finish()if config["save_last_frame"]:self.update_frame(scene, ignore_skipping=False)diff --git a/manim/scene/scene.py b/manim/scene/scene.pyindex 53c668ec7..7887c8fc0 100644--- a/manim/scene/scene.py+++ b/manim/scene/scene.py@@ -58,7 +58,6 @@ class Scene(Container):CONFIG = {"camera_class": Camera,- "skip_animations": False,"always_update_mobjects": False,"random_seed": 0,}@@ -66,7 +65,10 @@ class Scene(Container):def __init__(self, renderer=None, **kwargs):Container.__init__(self, **kwargs)if renderer is None:- self.renderer = CairoRenderer(camera_class=self.camera_class)+ self.renderer = CairoRenderer(+ camera_class=self.camera_class,+ skip_animations=kwargs.get("skip_animations", False),+ )else:self.renderer = rendererself.renderer.init(self)@@ -78,8 +80,6 @@ class Scene(Container):random.seed(self.random_seed)np.random.seed(self.random_seed)- self.setup()-@propertydef camera(self):return self.renderer.camera@@ -88,14 +88,12 @@ class Scene(Container):"""Render this Scene."""- self.original_skipping_status = config["skip_animations"]+ self.setup()try:self.construct()except EndSceneEarlyException:passself.tear_down()- # We have to reset these settings in case of multiple renders.- config["skip_animations"] = self.original_skipping_statusself.renderer.finish(self)logger.info(f"Rendered {str(self)}\nPlayed {self.renderer.num_plays} animations"@@ -642,7 +640,7 @@ class Scene(Container):ProgressDisplayThe CommandLine Progress Bar."""- if config["skip_animations"] and not override_skip_animations:+ if self.renderer.skip_animations and not override_skip_animations:times = [run_time]else:step = 1 / self.renderer.camera.frame_rate@@ -858,7 +856,7 @@ class Scene(Container):gain :"""- if config["skip_animations"]:+ if self.renderer.skip_animations:returntime = self.time + time_offsetself.renderer.file_writer.add_sound(sound_file, time, gain, **kwargs)diff --git a/manim/utils/caching.py b/manim/utils/caching.pyindex 6e864125e..29f66506a 100644--- a/manim/utils/caching.py+++ b/manim/utils/caching.py@@ -19,11 +19,11 @@ def handle_caching_play(func):"""def wrapper(self, scene, *args, **kwargs):- self.revert_to_original_skipping_status()+ self.skip_animations = self.original_skipping_statusself.update_skipping_status()animations = scene.compile_play_args_to_animation_list(*args, **kwargs)scene.add_mobjects_from_animations(animations)- if config["skip_animations"]:+ if self.skip_animations:logger.debug(f"Skipping animation {self.num_plays}")func(self, scene, *args, **kwargs)# If the animation is skipped, we mark its hash as None.@@ -41,7 +41,7 @@ def handle_caching_play(func):f"Animation {self.num_plays} : Using cached data (hash : %(hash_play)s)",{"hash_play": hash_play},)- config["skip_animations"] = True+ self.skip_animations = Trueelse:hash_play = "uncached_{:05}".format(self.num_plays)self.animations_hashes.append(hash_play)diff --git a/tests/helpers/graphical_units.py b/tests/helpers/graphical_units.pyindex 2ed89f209..47e7c6859 100644--- a/tests/helpers/graphical_units.py+++ b/tests/helpers/graphical_units.py@@ -23,11 +23,9 @@ def set_test_scene(scene_object, module_name):Examples--------Normal usage::-set_test_scene(DotTest, "geometry")- """- config["skip_animations"] = True+ """config["write_to_movie"] = Falseconfig["disable_caching"] = Trueconfig["save_last_frame"] = True@@ -39,7 +37,7 @@ def set_test_scene(scene_object, module_name):os.makedirs(os.path.join(tmpdir, "tex"))config["text_dir"] = os.path.join(tmpdir, "text")config["tex_dir"] = os.path.join(tmpdir, "tex")- scene = scene_object()+ scene = scene_object(skip_animations=True)scene.render()data = scene.renderer.get_frame()diff --git a/tests/utils/GraphicalUnitTester.py b/tests/utils/GraphicalUnitTester.pyindex 8b46bffb5..e5680b9b1 100644--- a/tests/utils/GraphicalUnitTester.py+++ b/tests/utils/GraphicalUnitTester.py@@ -53,7 +53,6 @@ class GraphicalUnitTester:config["text_dir"] = os.path.join(self.path_tests_medias_cache, "Text")config["tex_dir"] = os.path.join(self.path_tests_medias_cache, "Tex")- config["skip_animations"] = Trueconfig["disable_caching"] = Trueconfig["quality"] = "low_quality"@@ -65,7 +64,7 @@ class GraphicalUnitTester:os.makedirs(dir_temp)with tempconfig({"dry_run": True}):- self.scene = scene_object()+ self.scene = scene_object(skip_animations=True)self.scene.render()def _load_data(self):