Skip to content

Instantly share code, notes, and snippets.

@LiamHz
Last active January 26, 2025 16:15
Show Gist options
  • Save LiamHz/df3d088c980cf70dd79e8f2786dcd713 to your computer and use it in GitHub Desktop.
Save LiamHz/df3d088c980cf70dd79e8f2786dcd713 to your computer and use it in GitHub Desktop.
Creates a marking menu for switching between cameras (Maya script)
'''
Creates a marking menu for switching between cameras
Marking menu opens on Shift+MMB
Lists all cameras in a group named "Cameras"
Menu items will look through the selected camera
Menu item labels are created dynamically to match the order and names of your cameras
Marking menu code based on this blog post: http://bindpose.com/custom-marking-menu-maya-python/
'''
import maya.cmds as mc
MENU_NAME = "markingMenu"
class markingMenu():
'''The main class, which encapsulates everything we need to build and rebuild our marking menu. All
that is done in the constructor, so all we need to do in order to build/update our marking menu is
to initialize this class.'''
def __init__(self):
self._removeOld()
self._build()
def _build(self):
'''Creates the marking menu context and calls the _buildMarkingMenu() method to populate it with all items.'''
# Trigger on Shift+MMB
menu = mc.popupMenu(MENU_NAME, mm=1, b=2, aob=1, ctl=0, alt=0, sh=1, p="viewPanes", pmo=1, pmc=self._buildMarkingMenu)
def _removeOld(self):
'''Checks if there is a marking menu with the given name and if so deletes it to prepare for creating a new one.
We do this in order to be able to easily update our marking menus.'''
if mc.popupMenu(MENU_NAME, ex=1):
mc.deleteUI(MENU_NAME)
def _get_cameras_in_group(self, group_name):
group_members = mc.listRelatives(group_name, children=True)
# Filter out cameras
cameras = []
if group_members:
for member in group_members:
shapes = mc.listRelatives(member, shapes=True)
if shapes and mc.objectType(shapes[0]) == 'camera':
cameras.append(member)
return cameras
def _buildMarkingMenu(self, menu, parent):
'''This is where all the elements of the marking menu are built.'''
mc.menuItem(p=menu, label="persp", radialPosition="N", command="mc.lookThru('persp')")
cameras = self._get_cameras_in_group("Cameras")
if not cameras:
mc.warning('No cameras found in the group: {}'.format(group_name))
return
# Create marking menu and dynamically add camera names
radialPositions = ['NW', 'NE', 'W', 'E', 'SW', 'S', 'SE']
for i, camera in enumerate(cameras):
kwargs = {
"p": menu,
"label": camera,
"command": f"mc.lookThru('{camera}')"
}
# Only set radial position for the first 7 cameras
# Following cameras will end up in the list menu below the radial options
if i < len(radialPositions):
kwargs["radialPosition"] = radialPositions[i]
mc.menuItem(**kwargs)
# Rebuild
# mc.menuItem(p=menu, l="Rebuild Marking Menu", c=rebuildMarkingMenu)
markingMenu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment