Maya Python - Using menuItems

Hello, I am having a slight issue and was hoping someone could help me out. I’m trying to have a textField where if the user right clicks the field a popupMenu will show revealing several menuItems attached and if the user selects any of the menu items a string is inputted into the textField. My issue is that no matter the menuItem being clicked it always returns the same string into the textField but I want it to return the string that is designated for the proper menuItem. I think my problem is because I am querying the command but im not sure what else to query to see if the menuItem has been selected, I appreciate any advice.

The Code that runs the Function:

def getControllerTextValues():
	cc_foot = cmds.menuItem('cc_FootText', q=True, command=True)
	cc_heel = cmds.menuItem('cc_HeelText', q=True, command=True)
	cc_FootPivot = cmds.menuItem('cc_FootPivotText', q=True, command=True)

	nameInput = [cc_foot, cc_heel, cc_FootPivot]
	return nameInput
def inputTextForControllerFunction():
	nameInput = getControllerTextValues()

	if nameInput[0]:
		cmds.textField('inputControllerName_TF', edit=True, text="cc_L_Foot01")
	if nameInput[1]:
		cmds.textField('inputControllerName_TF', edit=True, text="cc_L_Heel01")
	if nameInput[2]:
		cmds.textField('inputControllerName_TF', edit=True, text="cc_L_bkLt_FootPivot01")	

You cannot query the command of a UI control in Maya, the result will always be None.

I would either create separate methods for each menuItem, or create a single method that sets the textField to the string you call it with. Using the 2nd method, you would call the same function from all three menuItems, but pass a different string to it.

Also when creating UIs in maya, I highly recommend that you make the whole UI a class. It greatly simplifies the access of UI shared data and controls.


class myWin(object):
    def __init__(self):
        self.Window = None
        self.buildUI()
    
    def buildUI(self):
        self.Window = cmds.window()
        cmds.columnLayout()
        self.tfg = cmds.textFieldGrp(l='Label', tx='Text')
        cmds.popupMenu()
        cmds.menuItem(l='Star Wars', c=lambda x: self.setMainTextFieldGroupText('Darth Vader'))
        cmds.menuItem(l='Star Trek', c=lambda x: self.setMainTextFieldGroupText('Picard'))
        cmds.showWindow( self.Window )
        
    def setMainTextFieldGroupText(self, value):
        cmds.textFieldGrp(self.tfg, e=True, tx=value)

getControllerTextValues is querying the commands associated with the menu items, not the labels

What about :


from functools import partial # to combine values and arguments

def set_text_value(*args):  
   # the *args is because menu callbacks always add an extra bool on the end
   cmds.textField('inputControllerName_TF', edit=True, text=args[0])

# in the menu:
menuItem('Foot', c = partial(set_text_value, "foot"))
menuItem('Hand', c = partial(set_text_value, "hand"))

Thanks for your assistance capper and Theodox, as for Classes Im not really familiar with them so im going to probably study that more and then attempt to use it. I also enjoyed your star wars and star trek references lol

There are several good threads here on Maya GUI programming and callbacks . Definitely search around and youll find some useful ideas