winteralfs
winteralfs

Reputation: 519

right click a shelf button in Maya to launch a different script

I was wondering if it is possible to launch a different script in Maya when right clicking on a custom shelf button verses a left click. So a traditional left click of the button would launch one version of the script, but a right click ( or some other action ) would execute a different version of the script.

Upvotes: 0

Views: 1427

Answers (1)

Green Cell
Green Cell

Reputation: 4777

Yes it is possible. What you can do is add a shelf button with cmds.shelfButton, then attach a pop-up menu with cmds.popupMenu where you can put as many commands as you want. cmds.popupMenu has a parameter button where you can specify what mouse button triggers the pop-up to show up.

import maya.cmds as cmds

# Put in what shelf tab to add the new button to.
shelf = "Rigging"

# Throw an error if it can't find the shelf tab.
if not cmds.shelfLayout(shelf_name, q=True, exists=True):
    raise RuntimeError("Not able to find a shelf named '{}'".format(shelf_name))

# Create a new shelf button and add it to the shelf tab.
# Include `noDefaultPopup` to support a custom menu for right-click.
new_shelf_button = cmds.shelfButton(label="My shelf button", parent=shelf, noDefaultPopup=True)

# Create a new pop-up menu and attach it to the new shelf button.
# Use `button` to specify which mouse button triggers the pop-up, in this case right-click.
popup_menu = cmds.popupMenu(parent=new_shelf_button, button=3)

# Create commands and attach it to the pop-up menu.
menu_command_1 = cmds.menuItem(label="Select meshes", sourceType="python", parent=popup_menu, command='cmds.select(cmds.ls(type="mesh"))')
menu_command_2 = cmds.menuItem(label="Select joints", sourceType="python", parent=popup_menu, command='cmds.select(cmds.ls(type="joint"))')
menu_command_3 = cmds.menuItem(label="Select all", sourceType="python", parent=popup_menu, command='cmds.select("*")')

Upvotes: 3

Related Questions