我想弄清楚如何注册ui创建事件。我想要实现的是在renderViewWindow打开时运行脚本。 阿尔维德
答案 0 :(得分:2)
您可以通过使用scriptJob命令来实现此目的。在Python中,您可以使用以下内容执行此操作:
import maya.cmds as cmds
import pymel.core as pm
class WindowWatcher():
""" A class to watch for a particular window in Maya """
def __init__(self, window_name, on_open_callback, on_close_callback=None):
self.window_name = window_name
self.on_open_callback = on_open_callback
self.on_close_callback = on_close_callback
self.window_opened = False
def check_for_window_open(self):
if not self.window_opened:
if self.window_name in cmds.lsUI(windows=True):
self.on_open_callback.__call__()
self.window_opened = True
else:
if not self.window_name in cmds.lsUI(windows=True):
self.window_opened = False
if self.on_close_callback:
self.on_close_callback.__call__()
if __name__ == "__main__":
# demo
render_window_name = "renderViewWindow"
def on_open_render_window(arg1, arg2):
# your on_window_open code here
print "Render Window opened!"
print "Arg1: %s Arg2: %s" % (arg1, arg2)
script_editor_name = "scriptEditorPanel1Window"
def on_open_script_editor():
# your on_window_open code here
print "Script Editor opened!"
render_window_watcher = WindowWatcher(render_window_name,
pm.windows.Callback(on_open_render_window, "Hello", "World")
)
script_editor_watcher = WindowWatcher(script_editor_name, on_open_script_editor)
cmds.scriptJob(event=["idle",
pm.windows.Callback(render_window_watcher.check_for_window_open)])
cmds.scriptJob(event=["idle",
pm.windows.Callback(script_editor_watcher.check_for_window_open)])
但请注意,并不总是建议使用“idle”事件,因为每次Maya处于空闲状态时都会调用该方法。这是谨慎使用的。
[编辑]您可以尝试检查maya.OpenMayaUI.MQtUtil.findWindow(self.window_name),而不是检查cmds.lsUI中的self.window_name(windows = True)。