Search code examples
pythonmaya

partial, getting some data from textField


import maya.cmds as cmds
from functools import partial

def export(txtField):
    print "hello"
    #print cmds.textField( txtField, q=1 )

if cmds.window( 'exporter', q=1, ex=1 ):
    cmds.deleteUI( 'exporter' )
window = cmds.window( 'exporter' )
cmds.columnLayout( adjustableColumn=True )
name = cmds.textField( text='testing...' )
press = cmds.button( 'Export...', c=partial( export, name) )
cmds.showWindow( 'exporter' )

So Im getting error:

# Error: export() takes exactly 1 argument (2 given) #

So Im new to partial and I dont understand what they do and how they work. But I know it's possible to do what I want with partial. So just print out whatever I have in textField


Solution

  • In this case partial is probably overkill. @mapofemergence's answer will work fine, but you can just do this:

    from maya import cmds    
    
    if cmds.window('exporter', q=1, ex=1):
        cmds.deleteUI('exporter')
    
    window = cmds.window('exporter')
    cmds.columnLayout(adjustableColumn=True)
    tf = cmds.textField(text='testing...')
    def export(*_):
        print "textfield says" , cmds.textField(tf, q=1, text=1)
    
    press = cmds.button('Export...', c=export)
    cmds.showWindow('exporter')
    

    since export is defined after the textfield is created, it captures the variable value at creation time.