Maya - cmds.button command duplicated

Hey there,
For an export tool, I need to create a maya window.
The window creates a button for each successfull export to copy the output path.

Problem is, after the window is copied, all the buttons point to the same path.
When printing the output path at button creation, they are correct: the args seem to become wrong once the window is actually created.

Does anyone have a solution?

targets = { 'A':{'abc_export_path':'L:/', 'namespace':'abc_A'},
            'B':{'abc_export_path':'L:/', 'namespace':'abc_B'},}

import clipboard
from maya import cmds

# create window
myWin = cmds.window(title='Abc Export', widthHeight=(150, 100), resizeToFitChildren=True, toolbox=True)
cmds.columnLayout( adjustableColumn=True )

# logg frame range
for root, data in targets.items():
    # logg success
    print ('creating UI button: ' + data.get('abc_export_path'))

    cmds.text(label = root )

    cmds.button( label='Copy Path' + root, align='center',
                command= lambda x: clipboard.copy( data.get('abc_export_path') ) )
# show window

So the problem is with loop variables get captured by the lambda.
What is happening is that data is being looked up when the lambda function is called (the button is clicked) not when the lambda function is defined.

One option would be to use functools.partial instead, or another option would be to pass data into the lambda at definition time:

command = lambda x, _data=data: clipboard.copy(_data.get('abc_export_path')))

1 Like

Hey there,

Thanks for the answer!