Search code examples
pythonmaya

How can I return a textField text as a string value?


So I'm creating a script and I need to input a name in a text field so that it can be applied to a filename. I created a side script to test out how to do this, however, when I press the button, even though there are no errors, empty space is printed

import maya.cmds as cmds

def pritest(*args):
    print(namepull)
#    Create a window with a some fields for entering text.
#
window = cmds.window()
cmds.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'right', 0), columnWidth=[(1, 100), (2, 250)] )
cmds.text( label='Name' )
#
name = cmds.textField()
cmds.textField(name, e=True,ed=True)
namepull = cmds.textField(name,q=True,tx=True)
#
cmds.button('Go',en=True,c=pritest)

cmds.showWindow( window )

I would like for this code to print whatever is typed into de fieldText. Thanks in advance for any help


Solution

  • also an elegant way is to use partial from functools :

    import maya.cmds as cmds
    from functools import partial
    
    def pritest(fieldname, *args):
        text = cmds.textField(fieldname,q=True,tx=True)
        print(text)
    
    window = cmds.window()
    cmds.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'right', 0), columnWidth=[(1, 100), (2, 250)] )
    cmds.text( label='Name' )
    name = cmds.textField()
    cmds.button('Go',en=True,c=partial(pritest, name))
    cmds.showWindow( window ) 
    

    you can see an exemple here too : Update textField value connected to a setAttr