0
votes

I'm trying to write a small script that would create 'n' buttons on a UI, depending on how many nCloth nodes exist on a scene, so the user would be able to select the node by pressing the corresponding button (for testing I just need them to select the node for now). This is what I've got so far:

import maya.cmds as cmds

def fillTemp(*args):
    theElements = cmds.rowColumnLayout ('listColumn', query=True, childArray=True)           
    if theElements > 0:           
        for element in theElements:           
            cmds.deleteUI (element)           
    objectList = cmds.ls(type='nCloth')     
    for item in objectList:                                                                         
        cmds.setParent ('listColumn')           
        cmds.text ((item+'labelText'), label=item, align='left',width= 220)       
        cmds.button ((item+'_select'), label='Select', width= 50)

if (cmds.window('testWin', exists=True)):      
    cmds.deleteUI('testWin')

theWindow = cmds.window('testWin', title='Test Window', widthHeight=(270, 390), sizeable=True)
cmds.columnLayout ('mainColumn', adjustableColumn=False)
cmds.button ('FillList', label= "nCloth in Scene", width= 270, command= fillTemp)
cmds.scrollLayout ('nClothScrollList', w=270 , h=150, backgroundColor=(.15,.15,.15) )
cmds.rowColumnLayout ('listColumn', numberOfColumns=2, columnWidth=[(1, 220), (2, 50)])
cmds.setParent('..')
cmds.showWindow (theWindow)

So far, I've been able to fill the RowColumnLayout with the name of the nodes and a button to select them.. however, I'm not sure how to get those buttons to work.. Any tips or ideas would be greatly appreciated.

1

1 Answers

0
votes

I didn't test this code, but it should work. You need to use either lambda or partial, I prefer partial.

import maya.cmds as cmds
from functools import partial

def fillTemp(*args):
    theElements = cmds.rowColumnLayout ('listColumn', query=True, childArray=True)           
    if theElements > 0:           
        for element in theElements:           
            cmds.deleteUI (element)           
    objectList = cmds.ls(type='nCloth')     
    for item in objectList:                                                                         
        cmds.setParent ('listColumn')           
        cmds.text ((item+'labelText'), label=item, align='left',width= 220)       
        cmds.button ((item+'_select'), label='Select', width= 50, command=partial(selectNode, item))

def selectNode(node, *args):
    cmds.select(cl=True)
    cmds.select(node)

if (cmds.window('testWin', exists=True)):      
    cmds.deleteUI('testWin')

theWindow = cmds.window('testWin', title='Test Window', widthHeight=(270, 390), sizeable=True)
cmds.columnLayout ('mainColumn', adjustableColumn=False)
cmds.button ('FillList', label= "nCloth in Scene", width= 270, command= fillTemp)
cmds.scrollLayout ('nClothScrollList', w=270 , h=150, backgroundColor=(.15,.15,.15) )
cmds.rowColumnLayout ('listColumn', numberOfColumns=2, columnWidth=[(1, 220), (2, 50)])
cmds.setParent('..')
cmds.showWindow (theWindow)