def one_undo(func):
"""Decorator - guarantee close chunk.
type: (function) -> function
"""
@wraps(func)
def wrap(*args, **kwargs):
# type: (*str, **str) -> None
try:
cmds.undoInfo(openChunk=True)
return func(*args, **kwargs)
except Exception as e:
raise e
finally:
cmds.undoInfo(closeChunk=True)
return wrap
python类undoInfo()的实例源码
def buttonCommand(func, *args, **kwargs):
"""
Return a function that can be called which will execute
the given function with proper undo chunk handling.
"""
def wrapper():
cmds.undoInfo(openChunk=True)
try:
func(*args, **kwargs)
except Exception as e:
print(e)
finally:
cmds.undoInfo(closeChunk=True)
return wrapper
def undo(function):
'''A decorator that will make commands undoable in maya'''
def decoratorCode(*args, **kwargs):
cmds.undoInfo(openChunk=True)
functionReturn = None
try:
functionReturn = function(*args, **kwargs)
except:
print sys.exc_info()[1]
finally:
cmds.undoInfo(closeChunk=True)
cmds.undoInfo(printQueue=True)
return functionReturn
return decoratorCode
def undoable(function):
'''A decorator that will make commands undoable in maya'''
def decoratorCode(*args, **kwargs):
cmds.undoInfo(openChunk=True)
functionReturn = None
try:
functionReturn = function(*args, **kwargs)
except:
print sys.exc_info()[1]
finally:
cmds.undoInfo(closeChunk=True)
return functionReturn
return decoratorCode
def no_undo():
"""Disable undo during the context"""
try:
cmds.undoInfo(stateWithoutFlush=False)
yield
finally:
cmds.undoInfo(stateWithoutFlush=True)
def __enter__(self):
if self.result == 0:
cmds.undoInfo(stateWithoutFlush=0)
else:
cmds.undoInfo(openChunk=1)
def __exit__(self, *exc_info):
if self.result == 0:
cmds.undoInfo(stateWithoutFlush=1)
else:
cmds.undoInfo(closeChunk=1)
# -------------------------------------------------------------------------------- #
# RPARTIAL() : Subclass of partial used instead to set maya's Undo/Redo output appropriately
def press(self, *args):
'''
Be careful overwriting the press method in child classes, because of the undoInfo openChunk
'''
self.anchorPoint = mc.draggerContext(self.draggerContext, query=True, anchorPoint=True)
self.button = mc.draggerContext(self.draggerContext, query=True, button=True)
# This turns off the undo queue until we're done dragging, so we can undo it.
mc.undoInfo(openChunk=True)
def release(self, *args):
'''
Be careful overwriting the release method in child classes. Not closing the undo chunk leaves maya in a sorry state.
'''
# close undo chunk and turn cycle check back on
mc.undoInfo(closeChunk=True)
#mc.cycleCheck(evaluation=self.cycleCheck)
mm.eval('SelectTool')
def __enter__(self):
'''
Turn off undo
'''
mc.undoInfo(stateWithoutFlush=False)
def __exit__(self,*args):
'''
Turn on undo
'''
mc.undoInfo(stateWithoutFlush=True)
def __enter__(self):
'''open the undo chunk'''
if self.force or MAYA_VERSION < 2011:
self.force = True
mc.undoInfo(openChunk=True)