访问和修改 Maya 撤消队列



有什么方法可以访问/编辑撤消队列吗?

问的原因是,在我当前的工具中,我在我的一个重命名函数中创建了以下内容(双击 QListWidgetItem,输入新名称,cmds.rename 将使用新的输入名称(:

cmds.undoInfo(chunkName='renameChunk', openChunk=True)
# do cmds.rename operations
cmds.undoInfo(chunkName='renameChunk', closeChunk=True)

但是,如果我尝试执行撤消功能 (ctrl+z( 以恢复命名,我需要按几次组合键而不是预期的 1 次。在打印撤消队列时,我注意到有很多"空白"条目可能是多个撤消的原因。

...
# 39:  # 
# 40:  # 
# 41:  # 
# 42:  # 
# 43: renameChunk # 
# 44:  # 
# 45:  # 
# 46:  # 
# 47:  # 
# 48:  # 
# 49:  #

我将提供一个答案,因为您正在做的事情有点风险。现在你假设cmds.undoInfo(chunkName='renameChunk', closeChunk=True)会运行,但如果中间发生错误,该行将永远不会被执行,你将留下一个打开的撤消块。

更安全的方法是打开撤消块,然后将代码包装在try finally中。这样,无论发生什么情况,您都可以确保块将在finally块中关闭:

cmds.undoInfo(chunkName='renameChunk', openChunk=True)
try:
    raise RuntimeError("Oops!")
finally:
    cmds.undoInfo(closeChunk=True)  # This will still execute.

或者,您可以更花哨一点,创建自己的撤消类并利用其__enter____exit__特殊方法:

class UndoStack(object):
    def __init__(self, name="actionName"):
        self.name = name
    def __enter__(self):
        cmds.undoInfo(openChunk=True, chunkName=self.name, infinity=True)
    def __exit__(self, typ, val, tb):
        cmds.undoInfo(closeChunk=True)
with UndoStack("renameChunk"):  # Opens undo chunk.
    raise RunTimeError("Oops!")  # Fails
# At this point 'with' ends and will auto-close the undo chunk.

只要你这样做,你就不应该有所有这些空白的撤消调用(至少我没有!虽然尽量保持紧凑,但请打开一个撤消块,完成工作,然后立即关闭它。避免偏离去做其他事情,比如管理你的 gui 或其他东西。

最新更新