From 1d67768a13e17a89e780ed6ca4ccd961f7f190c4 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Tue, 10 Dec 2024 14:34:46 -0600 Subject: [PATCH] Move reload out of Scene, instead have it directly update the global run configuration --- manimlib/reload_manager.py | 10 ---------- manimlib/scene/scene.py | 31 ------------------------------- manimlib/scene/scene_embed.py | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/manimlib/reload_manager.py b/manimlib/reload_manager.py index ad582c1e..84256d2a 100644 --- a/manimlib/reload_manager.py +++ b/manimlib/reload_manager.py @@ -27,13 +27,6 @@ class ReloadManager: window = None is_reload = False - embed_line = None - - def set_new_start_at_line(self, start_at_line): - """ - Sets/Updates the line number to load the scene from when reloading. - """ - self.embed_line = start_at_line def run(self): """ @@ -65,10 +58,7 @@ class ReloadManager: scene_config = global_config["scene"] run_config = global_config["run"] - scene_config.update(reload_manager=self) run_config.update(is_reload=self.is_reload) - if self.embed_line: - run_config.update(embed_line=self.embed_line) # Create or reuse window if run_config["show_in_window"] and not self.window: diff --git a/manimlib/scene/scene.py b/manimlib/scene/scene.py index 6ef3a66f..4a4c67f2 100644 --- a/manimlib/scene/scene.py +++ b/manimlib/scene/scene.py @@ -47,7 +47,6 @@ if TYPE_CHECKING: from PIL.Image import Image - from manimlib.reload_manager import ReloadManager from manimlib.animation.animation import Animation @@ -72,7 +71,6 @@ class Scene(object): def __init__( self, window: Optional[Window] = None, - reload_manager: Optional[ReloadManager] = None, camera_config: dict = dict(), file_writer_config: dict = dict(), skip_animations: bool = False, @@ -90,7 +88,6 @@ class Scene(object): self.leave_progress_bars = leave_progress_bars self.presenter_mode = presenter_mode self.show_animation_progress = show_animation_progress - self.reload_manager = reload_manager self.camera_config = merge_dicts_recursively( get_camera_config(), # Global default @@ -861,34 +858,6 @@ class Scene(object): def on_close(self) -> None: pass - def reload(self, start_at_line: int | None = None) -> None: - """ - Reloads the scene just like the `manimgl` command would do with the - same arguments that were provided for the initial startup. This allows - for quick iteration during scene development since we don't have to exit - the IPython kernel and re-run the `manimgl` command again. The GUI stays - open during the reload. - - If `start_at_line` is provided, the scene will be reloaded at that line - number. This corresponds to the `linemarker` param of the - `extract_scene.insert_embed_line_to_module()` method. - - Before reload, the scene is cleared and the entire state is reset, such - that we can start from a clean slate. This is taken care of by the - ReloadManager, which will catch the error raised by the `exit_raise` - magic command that we invoke here. - - Note that we cannot define a custom exception class for this error, - since the IPython kernel will swallow any exception. While we can catch - such an exception in our custom exception handler registered with the - `set_custom_exc` method, we cannot break out of the IPython shell by - this means. - """ - self.reload_manager.set_new_start_at_line(start_at_line) - shell = get_ipython() - if shell: - shell.run_line_magic("exit_raise", "") - def focus(self) -> None: """ Puts focus on the ManimGL window. diff --git a/manimlib/scene/scene_embed.py b/manimlib/scene/scene_embed.py index 1fd4d76c..d8aa0183 100644 --- a/manimlib/scene/scene_embed.py +++ b/manimlib/scene/scene_embed.py @@ -61,12 +61,12 @@ def get_shortcuts(scene): clear=scene.clear, focus=scene.focus, save_state=scene.save_state, - reload=scene.reload, undo=scene.undo, redo=scene.redo, i2g=scene.i2g, i2m=scene.i2m, checkpoint_paste=scene.checkpoint_paste, + reload=reload_scene # Defined below ) @@ -104,6 +104,38 @@ def ensure_flash_on_error(shell, scene): shell.set_custom_exc((Exception,), custom_exc) +def reload_scene(embed_line: int | None = None) -> None: + """ + Reloads the scene just like the `manimgl` command would do with the + same arguments that were provided for the initial startup. This allows + for quick iteration during scene development since we don't have to exit + the IPython kernel and re-run the `manimgl` command again. The GUI stays + open during the reload. + + If `embed_line` is provided, the scene will be reloaded at that line + number. This corresponds to the `linemarker` param of the + `extract_scene.insert_embed_line_to_module()` method. + + Before reload, the scene is cleared and the entire state is reset, such + that we can start from a clean slate. This is taken care of by the + ReloadManager, which will catch the error raised by the `exit_raise` + magic command that we invoke here. + + Note that we cannot define a custom exception class for this error, + since the IPython kernel will swallow any exception. While we can catch + such an exception in our custom exception handler registered with the + `set_custom_exc` method, we cannot break out of the IPython shell by + this means. + """ + if embed_line: + global_config = get_global_config() + global_config["run"]["embed_line"] = embed_line + + shell = get_ipython() + if shell: + shell.run_line_magic("exit_raise", "") + + class CheckpointManager: checkpoint_states: dict[str, list[tuple[Mobject, Mobject]]] = dict()