Search code examples
pythonmaya

popup menu and menuItem to display list of items


I have created a button, when it is click, it will displays a popup menu showing the list of cameras created along with persp camera (top, front, side camera will not be displayed)

While it seems to work, but as soon as I create another new camera, I got this error citing # TypeError: Too many objects or values. # and it is pointing towards the cmds.popupMenu(a)

Is there a better way to rectify it?

class orientCameraUI(QDialog):
    def __init__(self, parent=None):
        super(orientCameraUI, self).__init__(parent)
        self.resize(300,225)
        self.initUI()
        self.createConnections()

    def initUI(self):
        self.setWindowTitle('OrientControl UI')

        self.getCurrentCamBtn = QPushButton('Get current CAM')
        gridLayout = QGridLayout()
        gridLayout.addWidget(self.getCurrentCamBtn, 0, 1)
        self.setLayout(gridLayout)

    def createConnections(self):
        self.connect(self.getCurrentCamBtn, SIGNAL('clicked()'), self.getCurrentCam)

    def getCurrentCam(self):
        # createdCams - camera1, camera2 etc.
        createdCams = cmds.ls(cameras = True, visible = True)
        getPersp = cmds.ls("persp")
        cmds.popupMenu( button=1 )
        cmds.popupMenu(createdCams)
        cmds.popupMenu(getPersp)

Solution

  • Take a look at this snippet of your code here:

    cmds.popupMenu( button=1 )
    cmds.popupMenu(createdCams)
    cmds.popupMenu(getPersp)
    

    The first line creates a popup menu. (button=1 tells it to open when you click with the left mouse button instead of the default right mouse button.)

    You haven't added any menu items to the menu. Calling popupMenu again with a list of items is trying to make a new menu, not new items.

    Instead, what you want is a menuItem. But you need a different menu item for each created cam. You can't pass a list of cameras into one menuItem command. Use a for loop to make as many as you need:

    cmds.popupMenu(button=1) # creates the menu
    for cam in createdCams:  # loop over createdCams assigning to cam one at a time
        cmds.menuItem(cam)   # creates one menu item for cam
    cmds.menuItem(getPersp)  # create one menu item for persp
    

    See the documentation on popupMenu and menuItem