Skip to content

Commit

Permalink
Handle quitting during scene more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
3b1b committed Apr 27, 2022
1 parent 52259af commit e83ad78
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions manimlib/scene/scene.py
Expand Up @@ -100,6 +100,7 @@ def __init__(self, **kwargs):
self.mouse_drag_point = Point()
self.hold_on_wait = self.presenter_mode
self.inside_embed = False
self.quit_interaction = False

# Much nicer to work with deterministic scenes
if self.random_seed is not None:
Expand All @@ -117,8 +118,8 @@ def run(self) -> None:
self.setup()
try:
self.construct()
except EndSceneEarlyException:
pass
except (EndSceneEarlyException, KeyboardInterrupt):
self.linger_after_completion = False
self.tear_down()

def setup(self) -> None:
Expand All @@ -137,8 +138,12 @@ def construct(self) -> None:
def tear_down(self) -> None:
self.stop_skipping()
self.file_writer.finish()
if self.window and self.linger_after_completion:
self.interact()
if self.window:
if self.linger_after_completion:
self.interact()
else:
self.window.destroy()
self.window = None

def interact(self) -> None:
# If there is a window, enter a loop
Expand All @@ -149,12 +154,12 @@ def interact(self) -> None:
" and the mouse to interact with the scene. Just press `command + q` or `esc`"
" if you want to quit."
)
self.quit_interaction = False
self.refresh_static_mobjects()
while not (self.window.is_closing or self.quit_interaction):
self.update_frame(1 / self.camera.frame_rate)
if self.window.is_closing:
self.window.destroy()
try:
while True:
self.update_frame(1 / self.camera.frame_rate)
except (EndSceneEarlyException, KeyboardInterrupt):
return

def embed(self, close_scene_on_exit: bool = True) -> None:
if not self.preview:
Expand Down Expand Up @@ -251,6 +256,9 @@ def update_frame(self, dt: float = 0, ignore_skipping: bool = False) -> None:
if self.skip_animations and not ignore_skipping:
return

if self.window.is_closing or self.quit_interaction:
raise EndSceneEarlyException()

if self.window:
self.window.clear()
self.camera.clear()
Expand Down

0 comments on commit e83ad78

Please sign in to comment.