How do you query an object from the text field group?

I would like to know how to query a selection entered into a text field group, so I can do something with it. I have created a window to just translate an object that I loaded in the text field. The error is that cont is not defined.

import maya.cmds as cmds
import maya.mel as ml

def set_selected_name (text_field):
    cont = cmds.ls (selection = True)
    text_field = cmds.textFieldButtonGrp (text_field, edit = True,
                               text = ''.join (cont),
                               buttonLabel = '<<<<',
                               backgroundColor = [0.5098039215686274,
                                                  0.5098039215686274,
                                                  0.5098039215686274])
    return text_field

def translate_x(cont):
    cmds.setAttr( cont[0] + '.translateX', 10) 
       
def translate_y():
    cmds.setAttr( cont[0] + '.translateY', 10) 
       
def translate_z(*Args):        
    cmds.setAttr( cont[0] + '.translateZ', 10)




if cmds.window ('window1', q = 1, ex = 1):
    cmds.deleteUI ('window1')

cmds.window ('window1',
             title = 'Translate Attr',
             sizeable = 0,
             resizeToFitChildren = True,
             menuBar = 1)

cmds.rowLayout (numberOfColumns = 3)


cmds.separator (style = 'double',
                height = 6)

cmds.rowLayout (parent = 'window1',
                numberOfColumns = 4)

ddd = cmds.textFieldButtonGrp (editable = False,
                               text = 'Obj',
                               backgroundColor = [0.029495689326314183,
                                                  0.5488975356679637,
                                                  0.5488975356679637],
                               buttonLabel = '<<<<')

cmds.textFieldButtonGrp (ddd, edit = True,
                         buttonCommand = 'set_selected_name (ddd)')


cmds.separator (style = 'double',
                height = 6)

cmds.rowLayout (parent = 'window1',
                numberOfColumns = 6)

cmds.separator (style = 'double',
                height = 6)

cmds.button (command = 'translate_y()',
             backgroundColor = [1.0,
                                0.7300068665598535,
                                0.7300068665598535],
             label = 'Translate Y')

cmds.separator (style = 'double',
                height = 6)

cmds.button (command = 'translate_x(cont)',
             backgroundColor = [1.0,
                                0.9733272297245746,
                                0.7333333333333333],
             label = 'Translate X')

cmds.separator (style = 'double',
                height = 6)

cmds.button (command = 'translate_z()',
             backgroundColor = [0.7333333333333333,
                                1.0,
                                0.7600061036087586],
             label = 'Translate Z')

cmds.columnLayout (parent = 'window1')

cmds.separator (style = 'double',
                height = 3)

cmds.showWindow ('window1')

# ~ BABLAAM ~

Create any object you like, loaded into the text field and then try to translate with buttons.

command = 'translate_z()' should be command=translate_z

then

def translate_z(*args):
    print(args)

Give that a shot.

You’ll need to handle your selection, and translation in the callback function.

I couldn’t comment without also suggesting Qt instead of the older MEL stuff.

1.)What @ michaeltrainor said. In python, functions are first class objects which means you can pass them around like any other object. In a maya .cmds UI context, this means you can pass the function object as value to the command= key word arg of the UI control’s creation call. The command argument does not need to be a quoted string (unlike in mel) and it should have no parentheses, because it is an object value not an actual function call:

cmds.button( label='Translate Z',command=translate_x )

2.) The string value stored in the textFieldButtonGrp text must be queried with maya.cmds.textFieldButtonGrp() running in Query mode.

def translate_x(*args):
    object_name_string= cmds.textFieldButtonGrp(ddd, query=True, text=True)
    print "object = " ,object_name_string
    cmds.setAttr(object_name_string + '.translateX', 10)

3.) I prefer pymel to maya.cmds for UI stuff, the pymel class for textFieldButtonGrp has a .getText() function which makes it easier (sadly pymel accsessor functions are poorly documented)

import pymel.core as pm

ddd = pm.textFieldButtonGrp( text='Obj', buttonLabel='<<<<')

def translate_x(*args):
    object_name_string = ddd.getText()
    print "object = " ,object_name_string
    cmds.setAttr(object_name_string + '.translateX', 10)

4.) functions called by ui commands are always passed a value, which causes an error if we don’t handle the value passed in our callback function. That’s why there is the *args value in def translate_x(*args). this value is typically ignored.

and finally: start looking into Class based UIs in Maya Python. It’s a lot to learn but worth it. Rob Galankis’ Practical Maya Programming is a good resource.

1 Like

Thank you guys so much, your explanation is clear and simple. I ordered the book from amazon. One last question.

I don’t understand the "object = ". In the print statement. How does that come in to play?

the line
print "object = " ,object_name_string

Is completely unnecessary, and can be removed.
I just put it there to verify the return value of ddd.getText() .
A quick and dirty debug log.

originally it was
print "object = " ,object_name_string, type(object_name_string)
to verify that I was in fact receiving a string and not some other type of value.
I do that sort of thing often when dealing with return values I am not certain about.
such print statements generally get removed, commented out , or replaced with real logging in the final code

PS Welcome to TAO!