Ask about textField with python


#1

Hello guys,

I have started studying maya python for a week and I am trying to create easy save mesh file ui. but I have a problem with getting text from textField. Here is my code.

#UI Function
def main():

    #before adding window
    if (cmds.window('EasySave', exists = True)):    #delete window if exist
        cmds.deleteUI('EasySave', window = True)

    cmds.windowPref('EasySave', remove = True)    #delete previous setting
    
    
    #Add window
    windowwidth = 400    #width variable
    window = cmds.window('EasySave', title ='EasySave', iconName='ES', widthHeight=(windowwidth, 100), sizeable = False)    #window variable
    cmds.columnLayout( adjustableColumn = True )
    
    #First column ( text)
    cmds.text('Put the file path for the selected mesh', align = 'center', font = 'boldLabelFont')
    
    #Second column
    tmpRowWidth = [ windowwidth * 0.1 , windowwidth * 0.7 , windowwidth * 0.2 ]
    cmds.rowLayout(numberOfColumns = 3, columnWidth3 = tmpRowWidth, adjustableColumn = True)
    cmds.text('path')    #add text
    filepath = cmds.textField('filepath', width = tmpRowWidth[1])		#textfield
    cmds.button('browse', label = 'browse', width = tmpRowWidth[2], command = "browseFunction()")		#button
    cmds.setParent('..')
    
    
    #Third column
    tmpRowWidth = [ windowwidth*0.5 , windowwidth*0.5 ]
    cmds.rowLayout(numberOfColumns = 2, columnWidth2 = tmpRowWidth, adjustableColumn = True)
    cmds.button('exportFBX', label = 'Export FBX', width = tmpRowWidth[0], align = 'center' , command = 'exportFunction()')
    cmds.button('exportOBJ', label = 'Export OBJ', width = tmpRowWidth[1], align = 'center' , command = 'exportFunction()')
    cmds.setParent('..')
    
    #Show window
    cmds.showWindow(window)

    

#browse Function
def browseFunction():
        
    chooseFile = cmds.fileDialog2(caption = 'filepath', fileMode = 0)		# add filepath info
    filepath = cmds.textField('filepath', edit = True , text = chooseFile[0])		#return chooseFile string

#export Function
def exportFunction():

    #get FBX export save
    print(cmds.textField(filepath, edit = True))     #error

So far I made the code works with getting file browse, but I had a hard time getting info from textField. Is there a way to get textField info from a different function?


#2

You need to query the textField command using the query flag and the text flag. I added a button to show an example of how you can call a function from a button that provides the value in the text field.

def checkTextField(*args):
    textFromField = cmds.textField('filepath', query=True, text=True)

cmds.textField('filepath')
cmds.button(c=checkTextField)

#3

Thank you for the help. It works now :smile: