有什么方法可以访问/编辑撤消队列吗?
我问的原因是,在我当前的工具中,我在一个重命名函数中创建了以下内容(双击 QListWidgetItem,输入新名称,cmds.rename 将使用新的输入名称):
cmds.undoInfo(chunkName='renameChunk', openChunk=True)
# do cmds.rename operations
cmds.undoInfo(chunkName='renameChunk', closeChunk=True)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试执行撤消功能 (ctrl+z) 来恢复命名,则需要按组合键几次,而不是预期的 1 次。在打印撤消队列时,我注意到有很多“空白”条目,这可能是多次撤消的原因。
...
# 39: #
# 40: #
# 41: #
# 42: #
# 43: renameChunk #
# 44: #
# 45: #
# 46: #
# 47: #
# 48: #
# 49: #
Run Code Online (Sandbox Code Playgroud)
我将提供一个答案,因为你所做的事情有点冒险。现在您假设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.
Run Code Online (Sandbox Code Playgroud)
或者,您可以更花哨一点,创建自己的撤消类并利用它的__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.
Run Code Online (Sandbox Code Playgroud)
只要你这样做,你就不应该有所有这些空白的撤消调用(至少我没有!)。尽管要尽量保持紧凑,但请打开一个撤消块,完成工作,然后立即关闭它。避免偏离去做其他事情,比如管理你的图形用户界面或其他事情。
| 归档时间: |
|
| 查看次数: |
1746 次 |
| 最近记录: |