2024-12-09 16:14:27 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-26 19:09:43 +01:00
|
|
|
from typing import Any
|
|
|
|
from IPython.terminal.embed import KillEmbedded
|
|
|
|
|
2024-12-09 16:14:27 -06:00
|
|
|
|
|
|
|
import manimlib.config
|
|
|
|
import manimlib.extract_scene
|
|
|
|
|
2024-12-06 09:39:12 -06:00
|
|
|
from manimlib.window import Window
|
|
|
|
|
2024-11-26 19:09:43 +01:00
|
|
|
|
2024-12-09 16:14:27 -06:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from argparse import Namespace
|
|
|
|
|
|
|
|
|
2024-11-26 19:09:43 +01:00
|
|
|
class ReloadManager:
|
|
|
|
"""
|
|
|
|
Manages the loading and running of scenes and is called directly from the
|
|
|
|
main entry point of ManimGL.
|
|
|
|
|
|
|
|
The name "reload" comes from the fact that this class handles the
|
|
|
|
reinitialization of scenes when requested by the user via the `reload()`
|
|
|
|
command in the IPython shell.
|
|
|
|
"""
|
|
|
|
|
|
|
|
window = None
|
2024-12-06 01:18:10 +01:00
|
|
|
is_reload = False
|
|
|
|
|
2024-12-09 16:14:27 -06:00
|
|
|
def __init__(self, cli_args: Namespace):
|
|
|
|
self.args = cli_args
|
|
|
|
|
2024-11-26 19:09:43 +01:00
|
|
|
def set_new_start_at_line(self, start_at_line):
|
|
|
|
"""
|
|
|
|
Sets/Updates the line number to load the scene from when reloading.
|
|
|
|
"""
|
2024-12-09 16:43:08 -06:00
|
|
|
self.args.embed = str(start_at_line)
|
2024-11-26 19:09:43 +01:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
Runs the scenes in a loop and detects when a scene reload is requested.
|
|
|
|
"""
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
# blocking call since a scene will init an IPython shell()
|
2024-12-09 16:14:27 -06:00
|
|
|
self.retrieve_scenes_and_run()
|
2024-11-26 19:09:43 +01:00
|
|
|
return
|
|
|
|
except KillEmbedded:
|
|
|
|
# Requested via the `exit_raise` IPython runline magic
|
|
|
|
# by means of our scene.reload() command
|
2024-12-09 16:43:08 -06:00
|
|
|
self.note_reload()
|
2024-11-26 19:09:43 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
|
|
|
|
2024-12-09 16:43:08 -06:00
|
|
|
def note_reload(self):
|
|
|
|
self.is_reload = True
|
|
|
|
print(" ".join([
|
|
|
|
"Reloading interactive session for",
|
|
|
|
f"\033[96m{self.args.scene_names[0]}\033[0m",
|
|
|
|
f"at line \033[96m{self.args.embed}\033[0m"
|
|
|
|
]))
|
|
|
|
|
2024-12-09 16:14:27 -06:00
|
|
|
def retrieve_scenes_and_run(self):
|
2024-11-26 19:09:43 +01:00
|
|
|
"""
|
|
|
|
Creates a new configuration based on the CLI args and runs the scenes.
|
|
|
|
"""
|
|
|
|
# Args to Config
|
2024-12-05 16:51:14 -06:00
|
|
|
scene_config = manimlib.config.get_scene_config(self.args)
|
2024-12-09 15:54:16 -06:00
|
|
|
scene_config.update(reload_manager=self)
|
|
|
|
|
2024-12-05 16:51:14 -06:00
|
|
|
run_config = manimlib.config.get_run_config(self.args)
|
2024-12-06 12:24:16 -07:00
|
|
|
run_config.update(is_reload=self.is_reload)
|
2024-11-26 19:09:43 +01:00
|
|
|
|
2024-12-06 09:39:12 -06:00
|
|
|
# Create or reuse window
|
2024-12-06 09:54:14 -06:00
|
|
|
if run_config["show_in_window"] and not self.window:
|
2024-12-06 09:39:12 -06:00
|
|
|
self.window = Window(**run_config["window_config"])
|
2024-12-06 12:24:16 -07:00
|
|
|
scene_config.update(window=self.window)
|
2024-12-06 09:39:12 -06:00
|
|
|
|
2024-11-26 19:09:43 +01:00
|
|
|
# Scenes
|
2024-12-09 16:46:13 -06:00
|
|
|
scenes = manimlib.extract_scene.main(scene_config, run_config)
|
|
|
|
if len(scenes) == 0:
|
2024-11-26 19:09:43 +01:00
|
|
|
print("No scenes found to run")
|
|
|
|
|
2024-12-09 16:46:13 -06:00
|
|
|
for scene in scenes:
|
2024-11-26 19:09:43 +01:00
|
|
|
scene.run()
|