Skip to content

Instantly share code, notes, and snippets.

@JustinPedersen
Created March 9, 2022 06:49
Show Gist options
  • Save JustinPedersen/75af5087f7370d6aa95ff6dc17e37690 to your computer and use it in GitHub Desktop.
Save JustinPedersen/75af5087f7370d6aa95ff6dc17e37690 to your computer and use it in GitHub Desktop.
Simple undo decorator for creating an undo chunk around a function within Maya
from functools import wraps
import maya.cmds as cmds
def one_undo(func):
"""
Decorator - guarantee close chunk.
type: (function) -> function
"""
@wraps(func)
def wrap(*args, **kwargs):
try:
cmds.undoInfo(openChunk=True)
return func(*args, **kwargs)
except Exception as e:
raise e
finally:
cmds.undoInfo(closeChunk=True)
return wrap
@one_undo
def weird_func():
"""
Creates a bunch of spheres and then gets rolled back
"""
rad = 0
for i in range(10):
print(i)
rad += 0.2 * i
cmds.sphere(r=rad)
return rad
if __name__ == '__main__':
weird_func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment