Maya QT interfaces in a class

Source: Internet
Author: User

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 woshould 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 cmdsclass 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 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 cmdsdef 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 returnDictclass 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 maid, 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 wowould 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.