Maya窗口UI创建事件



我正试图弄清楚如何注册ui创建事件。我试图实现的是在renderViewWindow打开时运行脚本。Arvid

实现这一点的一种方法是使用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)])

但请注意,并不总是建议使用"空闲"事件,因为每次Maya处于空闲状态时都会调用该方法。这要小心使用。

[编辑]您可以尝试检查maya。OpenMayaUI.MQtTil.findWindow(self.window_name),而不是在cmds.lsUI(windows=True).中检查self.window_name

最新更新