Search code examples
pythonlistselectionmaya

Adding multiple selections to a main selection list to be used for a snapping script


setting up a snapping script by storing a list of control curves and have been having trouble getting my test script to use my selections in my snapping function(right now I want it to print the combined ik and fk control curves).

When I print my selection list, it prints what I have currently selected twice instead of my selections that I want to use

When I deselect anything on the viewport before hitting my button(after making selections in the tool) I get this error

 // Error: TypeError: file <maya console> line 243: unsupported operand type(s) for +: 'NoneType' and 'NoneType' //

when attempt to add my fk curve selections(selectJointLAFK) to my (combined_selection) I get this error

// Error: IndexError: file <maya console> line 204: list assignment index out of range //

I believe I get these errors due to when I press my button, it's running my selection functions again, but maybe I'm wrong?

For the button

###################################################################################
# Left Arm IK FK Snapping                               # Left Arm IK FK Snapping
cmds.button(label='FK 2 IK', command = 'Fk2Ik()', width=100)
cmds.button(label='IK 2 FK',  command = 'combined_selection()', width=100)
cmds.setParent('..')
cmds.separator(h=5, style = 'none')
cmds.separator(h=5)

for the selection list

###################################################################################
            #SELECTION LISTS#                                #SELECTION LISTS#  
###################################################################################

def selectJointLAFK():

    if cmds.ls(selection = True,type=("transform",'nurbsCurve')):
        sel = cmds.ls(sl=True)
        fkCtrls = cmds.listRelatives(sel, allDescendents=True, type=("transform",'nurbsCurve'))
        Fks = [nurbsCurve for nurbsCurve in fkCtrls if nurbsCurve.startswith('FK') & nurbsCurve.endswith('Ctrl')]
        cmds.textFieldButtonGrp(gtF0, edit = True, tx ='' .join(sel),buttonLabel='IK OK',backgroundColor = (.5,.8,.2))
        del Fks[1]
        del Fks[2]
        lAFKChain = Fks+sel
        print lAFKChain
        return lAFKChain
    else:
        text = cmds.confirmDialog( title='Error', message='Must select joint', button=['OK'], defaultButton='Ok', dismissString='No' )


def selectJointLwristIK():

    if cmds.ls(selection = True,type=("transform",'nurbsCurve')):
        ikwrist=cmds.ls(selection = True)
        cmds.textFieldButtonGrp(gtF1, edit = True, tx ='' .join(ikwrist),buttonLabel='IK OK',backgroundColor = (.5,.8,.2))
        lwristIKChain = ikwrist
        return lwristIKChain
    else:
        text = cmds.confirmDialog( title='Error', message='Must select joint', button=['OK'], defaultButton='Ok', dismissString='No' )

def selectJointLelbowIK():

    if cmds.ls(selection = True,type=("transform",'nurbsCurve')):
        iksel=cmds.ls(selection = True)
        cmds.textFieldButtonGrp(gtF2, edit = True, tx ='' .join(iksel),buttonLabel='IK OK',backgroundColor = (.5,.8,.2))
        lelbowIKChain = iksel
        return lelbowIKChain
    else:
        text = cmds.confirmDialog( title='Error', message='Must select joint', button=['OK'], defaultButton='Ok', dismissString='No' )



###################################################################################
            #IK FK SNAPPING #                                #IK FK SNAPPING #  
###################################################################################  
def combined_selection():
    fkCtrlsInfo = []
    lwristIKChain =selectJointLelbowIK()
    lwristIKChain =selectJointLwristIK()
    fkCtrlsInfo.append(lwristIKChain+lwristIKChain)
    print fkCtrlsInfo

###################################################################################
            #IK 2 FK SNAP #                                  #IK 2 FK SNAP #    
class Snapping(): 
    @staticmethod
    def Ik2Fk(self):
        print ("Snapped"+fkCtrlsInfo)

I expect my list to be printed as [FK_Shldr,FK_Elbow,Fk_Wrist,Ik_wrist,Ik_pv], but it currently prints

[[u'FK_Shdlr', u'FK_Shdlr']]

I'd like to understand what I'm doing wrong if possible


Solution

  • so first your button function should be parsed without commas :

    cmds.button(label='FK 2 IK', command = Fk2Ik, width=100) cmds.button(label='IK 2 FK', command = combined_selection, width=100)

    in this script you use twice the smae variable :

    def combined_selection():
        fkCtrlsInfo = []
        lwristIKChain =selectJointLelbowIK()
        lwristIKChain =selectJointLwristIK()
        fkCtrlsInfo.append(lwristIKChain+lwristIKChain)
        print fkCtrlsInfo
    

    lwristIKChain

    so you wont return selectJointLelbowIK

    Also : it print a nested list because append doesn't merge lists :

    fkCtrlsInfo = []
    fkCtrlsInfo.append(['bananaLeftIK', 'bananaRightIK'])
    # result : [['bananaLeftIK', 'bananaRightIK']]
    

    if you want to merge :

    fkCtrlsInfo += lwristIKChain
    

    or you can use chain from itertools to flatten your nested lists

    Also if you are not using return in your combined_selection, or if you are not using global statement, im not sure what fkCtrlsInfo will print in your class...

    I think you should write your script parts outisde function to find where you have data flow problems