[QUOTE=snolan;29255]A little while back I started a decorators library such as showing wait cursors and echoing the method being called.
This is the make any function undo-able decorator.
def d_undoable(f):
"""
A decorator that will make commands undoable in maya
"""
def func(*args, **kwargs):
cmds.undoInfo(openChunk=True)
functionReturn = None
try:
functionReturn = f(*args, **kwargs)
except:
print sys.exc_info()[1]
finally:
cmds.undoInfo(closeChunk=True)
return functionReturn
func.__name__ = f.__name__
func.__doc__ = f.__doc__
return func
Not a great example but you get the idea.
@d_undoable
def someFunction():
for i in range(10):
print "blah"
-Sean[/QUOTE]
Instead of
func.__name__ = f.__name__
func.__doc__ = f.__doc__
use functools.wraps, it handles all of the remapping of names, docs, etc…
Also, don’t use bare except blocks, this will block things like SystemExit, which you really don’t ever want to block. If you need to catch Maya’s exceptions, use RuntimeError. Which is still really broad, but well it is the best we’ve got, unless you’re using PyMel.
import functools
def d_undoable(f):
"""
A decorator that will make commands undoable in maya
"""
@functools.wraps(f)
def func(*args, **kwargs):
cmds.undoInfo(openChunk=True)
functionReturn = None
try:
functionReturn = f(*args, **kwargs)
except RuntimeError as e:
print e.message
finally:
cmds.undoInfo(closeChunk=True)
return functionReturn
return func