python - 访问和修改 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 或其他事情。
推荐阅读
- string - ESP-12e 和 ESP32-CAM 之间的串行通信数据
- sparql - 为什么 DISTINCT 关键字导致这两个查询的实体不同?
- javascript - 为什么提交表单时只执行部分代码?
- qt - MSYS2/MinGW 中 Qt hello world 应用程序的简单静态构建中的运行时错误
- javascript - How can I create my animation to be responsive?
- python - 使用列表理解从 pandas 列名和值创建元组
- ldap - 我的 IBM Directory Server P2P 复制通过 pwdpolicy 机制阻止添加新条目和更改操作属性。我该如何避免这种情况?
- c# - 尝试在 CellEndEdit 事件处理程序中重新绑定时出现 DataGridView 重入错误
- mongodb - 如何根据mongodb中另一个集合中的公共字段值查找文档
- javascript - 如何在javascript中打印嵌套对象的值