2014-10-26 47 views
0

我想弄清楚如何注册一个ui创建事件。我想要实现的是在renderViewWindow打开时运行脚本。 Arvid玛雅窗口UI创建事件?

+0

我不确定如何完全按照要求进行操作,但如果您在渲染开始时尝试运行脚本,可以考虑使用渲染设置中的预渲染梅尔? [这是一个相关的帖子](http://stackoverflow.com/questions/21488519/maya-querying-previous-render-information/21488609#21488609),可能是有用的 – mhlester 2014-10-26 21:06:21

+0

没有,不会工作,因为即使renderView是openend并没有呈现我想要一个脚本运行。或者,例如,如果我想在scriptEditor打开时运行脚本,我希望发生一些事情。 – arvidurs 2014-10-26 23:35:24

回答

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

即使被警告,使用“空闲”事件并不总是建议,因为该方法将被称为每玛雅闲置时间。这是要谨慎使用。

[编辑]你可以尝试检查maya.OpenMayaUI.MQtUtil.findWindow(self.window_name),而不是在cmds.lsUI(窗口= TRUE)检查self.window_name。

+0

感谢您的好代码!我工作非常好。 因为它始终称为CPU使用率始终以20%的速度运行。这可能太多了。但它是使用__call__方法的好方法。我仍然必须弄清楚它是如何工作的^^ – arvidurs 2014-10-27 19:32:40

+0

很高兴帮助!如果您觉得它有用,请接受答案。谢谢你,欢呼! – kartikg3 2014-10-27 19:34:21