Search code examples
python-2.7maya

intField does not display changes


I am writing a script to simplify a tedious task when using Vray, but I am stuck with the intFields that are supposed to allow the user to type in a int value that triggers an certain action when hitting the button. I simplified the code to only the necessary parts. No matter what I change the value to, it is always 0 in the Script Editor output.

import maya.cmds as cmds

idManagerUI = cmds.window(title='Vray ID Manager', s = False, wh = (300,500))

cmds.columnLayout(adj = True)

cmds.text (l = 'type in MultimatteID to select matching shaders \n or specify ObjectID to select matching objects \n __________________________________________ \n')

cmds.text (l = 'MultimatteID: \n')
cmds.intField( "MultimatteID", editable = True)
MultimatteIdButton = cmds.button(l = 'Go!', w = 30, h = 50, c = 'multimatteChecker()')
cmds.text (l = '\n')

cmds.showWindow(idManagerUI)

MultimatteIdInput = cmds.intField( "MultimatteID", q = True, v = True)


def multimatteChecker():
    print MultimatteIdInput

Solution

  • Three things:

    First, as written you can't be sure that the intField MultimatteID is actually getting the name you think it should have. Maya widget names are unique, like maya object names -- you may name it MultimatteID but actually get back a widget named MultimatteID2 because you have an undeleted window somewhere (visible or not) with a similarly named control.

    Second, the code you pasted queries the value of the control immediately after the window is created. It should always print out the value you gave it on creation.

    Finally -- don't use the string version of command assignment in your button. It's unreliable when you move from code in the listener to working scripts.

    This should do what you want:

        idManagerUI = cmds.window(title='Vray ID Manager', s = False, wh = (300,500))
        cmds.columnLayout(adj = True)
        cmds.text (l = 'type in MultimatteID to select matching shaders \n or specify ObjectID to select matching objects \n __________________________________________ \n')
        cmds.text (l = 'MultimatteID: \n')
        # store the intField name
        intfield = cmds.intField( "MultimatteID", editable = True)
        cmds.text (l = '\n')
    
        # define the function before assigning it. 
        # at this point in the code it knows what 'intfield' is....
        def multimatteChecker(_):
            print cmds.intField( intfield, q = True, v = True)
    
        #assign using the function object directly
        MultimatteIdButton = cmds.button(l = 'Go!', w = 30, h = 50, c = multimatteChecker)