zoukankan      html  css  js  c++  java
  • Maya QT interfaces in a class

    Most tutorials online have suggested the way to fire commands inside QT interfaces launched n Maya (via cmds.loadUi – not involving pyQT) is to add a string property like:

    +command="myPythonInstance.pythonCommand()"
     

    Pretty craptastical – it can only fire commands inside a specific python module, the name of which has to be known when you’re laying out your interface.

    Ideally, we’d like to have our interface instanced from a python class. This would allow us, amongst other things, so have multiple, independant instances. However it seems that a “self” reference can’t get passed to an interface, which is what you’d reallly like to do.

    +command=self.pythonCommand

    A (reasonably hacky) solution is to create the widget callbacks from inside our python class, after we’ve loaded the interface via cmds.loadUi().

    import maya.cmds as cmds
    
    class myUi(object):
        def __init__(self, *args):
            uiFile = '/path/to/uiFile.ui'
            rlDialog = cmds.loadUI(f=uiFile)
            cmds.showWindow(rlDialog)
            cmds.button( "ovrAdd_btn", edit=True, command=self.overrideAdd )
    
        def overrideAdd(self, *args):
            print 'do something!'

    Which is slightly better, but we’re still referring to our widget by a string name. This is problematic if you have multiple instances of this class – targeting “ovrAdd_btn” is going to get confusing. We need to find out exactly which control, in amongst the whole widget mess, is the one that we’ve spawned when we’re called cmds.loadUI().

    Sadly, loadUI() doesn’t give us any help in this department. So, and here’s the hacky bit, we can trawl through the widgets to find those belonging to our class instance, store them in a dict and then we can refer back to them by a simplified name. Easy!

    import maya.cmds as cmds
    
    def widgetPath(windowName, widgetNames):
        """
        
        @param windowName: Window instance name to search
        @param widgetNames: list of names to search for
        """
        
        returnDict = {}
        mayaWidgetList = cmds.lsUI(dumpWidgets=True)
        
        for widget in widgetNames:
            for mayaWidge in mayaWidgetList:
                if windowName in mayaWidge:
                    if mayaWidge.endswith(widget):
                        returnDict[widget] = mayaWidge
                        
        return returnDict
    
    class myUi(object):
        def __init__(self, *args):
            uiWidgetList = ['ovrAdd_btn']
    
            uiFile = '/path/to/uiFile.ui'
            rlDialog = cmds.loadUI(f=uiFile)
            self.uiObjects = widgetPath(rlDialog, uiWidgetList)
            
            cmds.showWindow(rlDialog)
            
            cmds.button( self.uiObjects["ovrAdd_btn"], edit=True, command=self.overrideAdd )
    
        def overrideAdd(self, *args):
            print 'do something!'

    Trawling through Maya’s widget list isn’t particularly elegant, but you only have to do it once per class initialisation, so there isn’t too much of a cost. The advantage is that you can now have per-instance widget names.

    Why not just use pyQT proper, you ask? Installing external dependencies isn’t always an option for our less techy brethren, or easily done in a studio-wide fashion. Using pyQT would be far better (and give all sorts of other benefits), but if you’re constrained to using vanilla maya / python, this seems to do the trick.


    作者:jonn
    出处:http://www.cnblogs.com/jonn/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    该文章也同时发布在我的独立博客中-jonn

  • 相关阅读:
    tp5 url 线上访问 在nginx 上 出现404错误,解决办法(1.80nginx 配置 pathInfo)
    Windows下配置nginx+php(wnmp)
    windows向github提交代码
    改变图片尺寸
    mysql字符编码的设置以及mysql中文乱码的解决方法
    wamp升级php7
    C++Primer第五版——习题答案和解析
    Ubuntu16.04下安装显卡驱动记录
    Ubuntu环境下No module named '_tkinter'错误的解决
    TensorFlow:NameError: name ‘input_data’ is not defined
  • 原文地址:https://www.cnblogs.com/jonn/p/3820187.html
Copyright © 2011-2022 走看看