Skip to content

Instantly share code, notes, and snippets.

@Kif11
Forked from schworer/undo_dec.py
Last active March 2, 2024 09:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kif11/a111c69c4031c9b7e18e to your computer and use it in GitHub Desktop.
Save Kif11/a111c69c4031c9b7e18e to your computer and use it in GitHub Desktop.
quick and dirty Maya Python undo decorator
from functools import wraps
from maya import cmds
def undo(func):
""" Puts the wrapped `func` into a single Maya Undo action, then
undoes it when the function enters the finally: block """
@wraps(func)
def _undofunc(*args, **kwargs):
try:
# start an undo chunk
cmds.undoInfo(ock=True)
return func(*args, **kwargs)
finally:
# after calling the func, end the undo chunk and undo
cmds.undoInfo(cck=True)
cmds.undo()
return _undofunc
from undo_dec import undo
from maya import cmds
@undo
def weird_func():
""" Creates a bunch of spheres and then gets rolled back """
rad = 0
for i in xrange(1000):
print i
rad += 0.2 * i
cmds.sphere(r=rad)
return rad
print weird_func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment