Maya Python: UI blendShape Target optionList Issue

Hi all!

I’m currently making a tool in Maya using Python and I’m running into some issues with returning the name of targets in a blendShape.
My UI is currently laid out so that the user can type in the name of a blendShape and then press a button which loads all the targets under that blendShape node in an optionMenu. Every time the button is pressed, the list in the optionMenu is cleared and updated to reflect the targets under the node in the text field.

What I’ve found through trial and error is that if I create a blendShape AFTER opening my UI, I’ll get a RuntimeError:Menu Item’s menu not found.
However, if I create the blendShape and only then open up the UI, the targets load just fine.

I have no clue why this might be. I’m wondering if it’s because I’m using aliasAttr to find the target nodes and that’s somehow messing things up behind the scenes.

Here’s the relevant part of the code:



#open UI window
def poseTrackerWindow():
    if cmds.window( 'poseTrackerWindow2', exists = True ):
        cmds.deleteUI( 'poseTrackerWindow2')

    #window definition
    cmds.window( 'poseTrackerWindow2', widthHeight = ( 470, 200 ), title = 'Pose Tracker Beta', minimizeButton = False, maximizeButton = False, resizeToFitChildren = True, sizeable = True )
    
    cmds.rowColumnLayout(numberOfColumns = 2, columnWidth = [(1,100), (2, 200)])
    			
    #select an existing blendShape node
    selectedBlendShapeTextField = cmds.textField( 'selectedBlendShapeText' )
    
    #display corresponding targets
    cmds.text( label='' )
    cmds.button( label = 'Load Targets', command = 'loadTargetList()' )    
    targetList = cmds.optionMenu( 'targetObjectMenu', label='Target' )
    cmds.menuItem(label='please select blendShape' )
        
    cmds.showWindow( 'poseTrackerWindow2' )
    
poseTrackerWindow()

#function that loads target list to the UI

def loadTargetList():
    allTargetShapesList = []
    selectedBlend = cmds.textField('selectedBlendShapeText', q=True, tx=True )
    targetInfo = cmds.aliasAttr(selectedBlend, q=True)
    
    existingItems = cmds.optionMenu( 'targetObjectMenu', q=True, itemListLong=True )
    if existingItems != None and existingItems != []:
        cmds.deleteUI(existingItems)
    
    for index in range(len(targetInfo)):
        if index % 2 == 0:
            allTargetShapesList.append(targetInfo[index])
    
    for target in allTargetShapesList:
        cmds.menuItem('targetObjectMenu', label='%s' %(target) )


(The code above is causing exactly the same bug as my full code, so if you’d like to see for yourself you’re of course welcome to)

Anyone have any clue what might be happening?

If you enable line numbers in the error message you’ll see it’s erroring on this line:

cmds.menuItem('targetObjectMenu', label='%s' %(target))

The error tells you that it doesn’t know what menu it is supposed to add the menuItem to. You have to specify the menu to attach the menuItem to using the parent flag. In addition to this, the first argument of any maya UI element tells maya what to name the element being created. Because you’re giving each menuItem the same name as the menu group, it no longer knows what object ‘targetObjectMenu’ refers to. The quickest solution here is:

cmds.menuItem(label='%s' %(target), parent='targetObjectMenu')