Skip to content

Commit

Permalink
Add checkpoints to Scene
Browse files Browse the repository at this point in the history
  • Loading branch information
3b1b committed Apr 27, 2022
1 parent ec9ed32 commit 1b589e3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions manimlib/scene/scene.py
Expand Up @@ -86,6 +86,7 @@ def __init__(self, **kwargs):
self.time: float = 0
self.skip_time: float = 0
self.original_skipping_status: bool = self.skip_animations
self.checkpoint_states: dict[str, list[tuple[Mobject, Mobject]]] = dict()
if self.start_at_animation_number is not None:
self.skip_animations = True

Expand Down Expand Up @@ -175,6 +176,28 @@ def embed(self, close_scene_on_exit: bool = True) -> None:
]
})

# This is useful if one wants to re-run a block of scene
# code, while developing, tweaking it each time.
# As long as the copied selection starts with a comment,
# this will revert to the state of the scene at the first
# point of running.
def checkpoint_paste(skip=False):
pasted = pyperclip.paste()
line0 = pasted.lstrip().split("\n")[0]
if line0.startswith("#"):
if line0 not in self.checkpoint_states:
self.checkpoint(line0)
else:
self.revert_to_checkpoint(line0)
if skip:
originally_skip = self.skip_animations
self.skip_animations = True
shell.run_line_magic("paste", "")
if skip:
self.skip_animations = originally_skip

local_ns['checkpoint_paste'] = checkpoint_paste

# Enables gui interactions during the embed
def inputhook(context):
while not context.input_is_ready():
Expand Down Expand Up @@ -699,6 +722,18 @@ def redo(self):
self.restore_state(self.redo_stack.pop())
self.refresh_static_mobjects()

def checkpoint(self, key: str):
self.checkpoint_states[key] = self.get_state()[0]

def revert_to_checkpoint(self, key: str):
if key not in self.checkpoint_states:
log.error(f"No checkpoint at {key}")
return
self.restore_state(self.checkpoint_states[key])

def clear_checkpoints(self):
self.checkpoint_states = dict()

def save_mobject_to_file(self, mobject: Mobject, file_path: str | None = None) -> None:
if file_path is None:
file_path = self.file_writer.get_saved_mobject_path(mobject)
Expand Down

0 comments on commit 1b589e3

Please sign in to comment.