Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a suspended_updating context manager to Mobject #4101

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions manim/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,19 +1122,39 @@ def match_updaters(self, mobject: Mobject) -> Self:
self.add_updater(updater)
return self

def suspend_updating(self, recursive: bool = True) -> Self:
def suspend_updating(self, recursive: bool = True):
"""Disable updating from updaters and animations.

This can also be used as a context manager. If so, :meth:`resume_updating` is called when leaving the context.


Parameters
----------
recursive
Whether to recursively suspend updating on all submobjects.

Returns
-------
:class:`Mobject`
``self``

Examples
--------

.. manim:: SuspendContext

class SuspendContext(Scene):
def construct(self):
s = Square().shift(DL)
c = Circle().add_updater(lambda m: m.next_to(s, UP, buff=1.0))
self.add(c)

self.play(s.animate.shift(2*RIGHT))
c.suspend_updating() # used a function call
self.play(Indicate(s))
c.resume_updating() # updating must be resumed manually
self.play(s.animate.shift(2*LEFT))

with c.suspend_updating(): # used as a context manager, updating is resumed automatically
self.play(Indicate(s))
self.play(Indicate(s))


See also
--------
Expand All @@ -1146,20 +1166,17 @@ def suspend_updating(self, recursive: bool = True) -> Self:
if recursive:
for submob in self.submobjects:
submob.suspend_updating(recursive)
return self

def resume_updating(self, recursive: bool = True) -> Self:
return _UpdateManager(self)

def resume_updating(self, recursive: bool = True):
"""Enable updating from updaters and animations.

Parameters
----------
recursive
Whether to recursively enable updating on all submobjects.

Returns
-------
:class:`Mobject`
``self``

See also
--------
Expand All @@ -1172,7 +1189,6 @@ def resume_updating(self, recursive: bool = True) -> Self:
for submob in self.submobjects:
submob.resume_updating(recursive)
self.update(dt=0, recursive=recursive)
return self

# Transforming operations

Expand Down Expand Up @@ -3108,6 +3124,17 @@ def build(self) -> Animation:
return anim


class _UpdateManager:
def __init__(self, parent: Mobject):
self.parent: Mobject = parent

def __enter__(self) -> Mobject:
return self.parent

def __exit__(self, exc_type, exc_value, traceback):
self.parent.resume_updating()


def override_animate(method) -> types.FunctionType:
r"""Decorator for overriding method animations.

Expand Down
Loading