Search code examples
pythonuser-interfacemaya

Maya 2015 Python - textField input, radioButton selection not working in UI


I'm studying python for developing tools for maya (at the moment 2015) and can't understand exactly how I should define variables or ui objects to be used inside functions. A lot of different errors pop up when I try running my script (e.g. for this script: # Error: NameError: file line 1: name 'inputTextA' is not defined )

I run into the same problem over and over again, so I would like someone to explain to me what kind of mistake I'm making and what to study to solve this kind of troubles in the future. (Please only Python and Maya answers! :) )

The next script is saved in the maya's script path, called from the shelf as follows:

import uiTestA
reload (uiTestA)
uiTestA

In the script, depending on the radioButton you set (A, B) a function should run using the textField information after pressing the button "Execute". The script itself:

# -*- coding: utf-8 -*-

import maya.cmds as cmds

def printStuff (fieldID):
    selObjs=cmds.ls(selection=True)
    print "\n###############################################"
    if ( cmds.radioButtonGrp( radioButtonsA, q=True, select=True )==1 ):
        if len(selObjs)>0:
            typeObj = cmds.textField( fieldID, query=True, text=True )
            if typeObj=="exception":
                print "EXCEPTION [%s] CASE A" % typeObj
            elif typeObj=="":
                cmds.error( "Please input something" )
            else:
                print "NORMAL [%s] CASE A" % typeObj
        else:
            cmds.error( "Please select a group or object in the scene." )
    elif ( cmds.radioButtonGrp( radioButtonsA, q=True, select=True )==2 ):
        typeObj = cmds.textField( fieldID, query=True, text=True )
        if typeObj=="exception":
            print "EXCEPTION [%s] CASE B" % typeObj
        elif typeObj=="":
            cmds.error( "Please input something" )
        else:
            print "NORMAL [%s] CASE B" % typeObj
    print "\n###############################################\n"

winID = "uiTestA"
if cmds.window( winID, exists=True ):
    cmds.deleteUI( winID )
cmds.window ( winID, sizeable=False, width=325, resizeToFitChildren=True )
formLay = cmds.formLayout( numberOfDivisions=100 )

radioButtonsA = cmds.radioButtonGrp( width=325, label='Search in:  ', columnWidth3=(80,158,100), labelArray2=["A", "B"], select=1, numberOfRadioButtons=2 )
explanationA = cmds.text( width=330, label="Input:" )
inputTextA = cmds.textField( width=100 )
selectButton = cmds.button( width=150, label="Execute", command="%s.printStuff(inputTextA)"%__name__)

cmds.formLayout( formLay, edit=True, attachForm=[(radioButtonsA, 'top', 5), (radioButtonsA, 'left', 0), (explanationA, 'top', 30), (inputTextA, 'top', 45), (inputTextA, 'left', 117), (selectButton, 'top', 70), (selectButton, 'left', 92)] )

cmds.showWindow( winID )

I tried defining a function to create the ui like:

# -*- coding: utf-8 -*-

import maya.cmds as cmds

def printStuff (fieldID):
    selObjs=cmds.ls(selection=True)
    print "\n###############################################"
    if ( cmds.radioButtonGrp( radioButtonsA, q=True, select=True )==1 ):
        if len(selObjs)>0:
            typeObj = cmds.textField( fieldID, query=True, text=True )
            if typeObj=="exception":
                print "EXCEPTION [%s] CASE A" % typeObj
            elif typeObj=="":
                cmds.error( "Please input something" )
            else:
                print "NORMAL [%s] CASE A" % typeObj
        else:
            cmds.error( "Please select a group or object in the scene." )
    elif ( cmds.radioButtonGrp( radioButtonsA, q=True, select=True )==2 ):
        typeObj = cmds.textField( fieldID, query=True, text=True )
        if typeObj=="exception":
            print "EXCEPTION [%s] CASE B" % typeObj
        elif typeObj=="":
            cmds.error( "Please input something" )
        else:
            print "NORMAL [%s] CASE B" % typeObj
    print "\n###############################################\n"

def ui()
    winID = "uiTestA"
    if cmds.window( winID, exists=True ):
        cmds.deleteUI( winID )
    cmds.window ( winID, sizeable=False, width=325, resizeToFitChildren=True )
    formLay = cmds.formLayout( numberOfDivisions=100 )

    radioButtonsA = cmds.radioButtonGrp( width=325, label='Search in:  ', columnWidth3=(80,158,100), labelArray2=["A", "B"], select=1, numberOfRadioButtons=2 )
    explanationA = cmds.text( width=330, label="Input:" )
    inputTextA = cmds.textField( width=100 )
    selectButton = cmds.button( width=150, label="Execute", command="%s.printStuff(inputTextA)"%__name__)

    cmds.formLayout( formLay, edit=True, attachForm=[(radioButtonsA, 'top', 5), (radioButtonsA, 'left', 0), (explanationA, 'top', 30), (inputTextA, 'top', 45), (inputTextA, 'left', 117), (selectButton, 'top', 70), (selectButton, 'left', 92)] )

    cmds.showWindow( winID )

And then calling it properly from maya's shelf; but no results. I'm fairly new to this so please be gentle! :) Thank you in advance for your time and help! Have a nice day! :)


Solution

  • Big questions come up always with maya ui. Here is a link where there is all details about it : Maya Python - Using data from UI

    To sum up, there is multiple way to go :

    • create dictionnary (easiest)
    • pass variables throught with partial or lambda
    • create a class (more advanced and not necessary for simple ui)

    ===========================================

    First of all, you should not wright :

    selectButton = cmds.button( width=150, label="Execute", command="%s.printStuff(inputTextA)"%__name__)
    

    command flag should not be a string, you have to use partial or lambda, i.e :

    from functools import partial
    
        selectButton = cmds.button( width=150, label="Execute", command=partial(printStuff, name))
    

    ===========================================

    To query radiobutton outside the def, use dic like this :

    uiDic = {}
    uiDic['radioButtonsA'] = cmds.radioButtonGrp( width=325, label='Search in:  ', columnWidth3=(80,158,100), labelArray2=["A", "B"], select=1, numberOfRadioButtons=2 )
    
    # query ===
    
    if ( cmds.radioButtonGrp(uiDic['radioButtonsA'], q=True, select=True )==1 ):
    

    -------- EDIT ----------- Here is a working example of your script :

    import maya.cmds as cmds
    from functools import partial
    
    dicUI = {}
    
    def printStuff (fieldID, *args):
        selObjs=cmds.ls(selection=True)
        print "\n###############################################"
        if ( cmds.radioButtonGrp( dicUI['radioButtonsA'], q=True, select=True )==1 ):
            if len(selObjs)>0:
                typeObj = cmds.textField( fieldID, query=True, text=True )
                if typeObj=="exception":
                    print "EXCEPTION [%s] CASE A" % typeObj
                elif typeObj=="":
                    cmds.error( "Please input something" )
                else:
                    print "NORMAL [%s] CASE A" % typeObj
            else:
                cmds.error( "Please select a group or object in the scene." )
        elif ( cmds.radioButtonGrp( dicUI['radioButtonsA'], q=True, select=True )==2 ):
            typeObj = cmds.textField( fieldID, query=True, text=True )
            if typeObj=="exception":
                print "EXCEPTION [%s] CASE B" % typeObj
            elif typeObj=="":
                cmds.error( "Please input something" )
            else:
                print "NORMAL [%s] CASE B" % typeObj
        print "\n###############################################\n"
    
    def ui():
        dicUI['winID'] = "uiTestA"
        if cmds.window( dicUI['winID'], exists=True ):
            cmds.deleteUI( dicUI['winID'] )
        cmds.window ( dicUI['winID'], sizeable=False, width=325, resizeToFitChildren=True )
        formLay = cmds.formLayout( numberOfDivisions=100 )
    
        dicUI['radioButtonsA'] = cmds.radioButtonGrp( width=325, label='Search in:  ', columnWidth3=(80,158,100), labelArray2=["A", "B"], select=1, numberOfRadioButtons=2 )
        explanationA = cmds.text( width=330, label="Input:" )
        inputTextA = cmds.textField( width=100 )
        selectButton = cmds.button( width=150, label="Execute", command=partial(printStuff, inputTextA))
    
        cmds.formLayout( formLay, edit=True, attachForm=[(dicUI['radioButtonsA'], 'top', 5), (dicUI['radioButtonsA'], 'left', 0), (explanationA, 'top', 30), (inputTextA, 'top', 45), (inputTextA, 'left', 117), (selectButton, 'top', 70), (selectButton, 'left', 92)] )
    
        cmds.showWindow( dicUI['winID'] )