arvidurs
arvidurs

Reputation: 3043

Maya Window UI create Event?

I am trying to figure out how to register an ui create event. What I am trying to achieve is run a script when the renderViewWindow opens. Arvid

Upvotes: 0

Views: 1234

Answers (1)

kartikg3
kartikg3

Reputation: 2620

One way you could do this is by using the scriptJob command. In Python, you could do this using something like:

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)])

Be warned though, using the "idle" event isn't always recommended, as the method would be called every time Maya sits idle. This is to be used with caution.

[Edit] You can try checking for maya.OpenMayaUI.MQtUtil.findWindow(self.window_name) instead of checking for self.window_name in cmds.lsUI(windows=True).

Upvotes: 2

Related Questions