Skip to content

Instantly share code, notes, and snippets.

@Lirinis
Forked from vshotarov/markingMenu.py
Last active July 14, 2020 11:43
Show Gist options
  • Save Lirinis/46ddc8771034edb78a3565a7a5203107 to your computer and use it in GitHub Desktop.
Save Lirinis/46ddc8771034edb78a3565a7a5203107 to your computer and use it in GitHub Desktop.
Example of a custom marking menu in Maya, scripted in Python.
'''A simple example of a custom marking menu in Maya. The benefits of doing it this way with Python are that
it is very flexible and easily maintainable. Additionally, you can keep it in a version control system.
This file is used for demonstration purposes, to be followed along with in this blog post
http://bindpose.com/custom-marking-menu-maya-python/
'''
import maya.cmds as mc
import maya.mel as mel
MENU_NAME = "My_marking_menu_02"
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.'''
menu = mc.popupMenu(MENU_NAME, mm=1, b=1, aob=1, ctl=0, alt=0, sh=0, 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 _buildMarkingMenu(self, menu, parent):
'''This is where all the elements of the marking menu our built.'''
# Radial positioned
mc.menuItem(p=menu, l="Space", rp="N", c=switch_space_Func)
mc.menuItem(p=menu, l="Rotate", rp="NE", c=rotate_Func)
mc.menuItem(p=menu, l="Translate", rp="NW", c=translate_Func)
mc.menuItem(p=menu, l="Scale", rp="S", c=scale_Func)
mc.menuItem(p=menu, l="Auto tangets", rp="SE", c=auto_tangets_Func)
mc.menuItem(p=menu, l="Focus", rp="SW", c=focus_Func)
# subMenu = mc.menuItem(p=menu, l="North Sub Menu", rp="N", subMenu=1)
# mc.menuItem(p=subMenu, l="North Sub Menu Item 1")
# mc.menuItem(p=subMenu, l="North Sub Menu Item 2")0
# mc.menuItem(p=menu, ob=1, c="print 'South with Options'")
# List
# mc.menuItem(p=menu, l="First menu item")
# mc.menuItem(p=menu, l="Second menu item")
# mc.menuItem(p=menu, l="Third menu item")
# mc.menuItem(p=menu, l="Create poly cube", c="mc.polyCube()")
# Rebuild
# mc.menuItem(p=menu, l="Rebuild Marking Menu", c=rebuildMarkingMenu)
markingMenu()
def switch_space_Func(*args):
mel.eval("""
string $sel[] =`ls -sl`;
for ($thisObj in $sel){
int $contextAxisManip = `manipMoveContext -q -mode Move`;
if ($contextAxisManip == 0)
{
manipMoveContext -e -mode 2 Move;
}
if ($contextAxisManip == 2)
{
manipMoveContext -e -mode 0 Move;
}
else
{
manipMoveContext -e -mode 2 Move;
}
};
""")
def rotate_Func(*args):
mel.eval("""
setToolTo $gRotate;
""")
def translate_Func(*args):
mel.eval("""
setToolTo $gMove;
""")
def scale_Func(*args):
mel.eval("""
setToolTo $gScale;
""")
def auto_tangets_Func(*args):
mel.eval("""
keyTangent -e -itt auto -ott auto;
""")
def focus_Func(*args):
mel.eval("""
fitPanel -selected;
""")
def rebuildMarkingMenu(*args):
'''This function assumes that this file has been imported in the userSetup.py
and all it does is reload the module and initialize the markingMenu class which
rebuilds our marking menu'''
mc.evalDeferred("""
reload(markingMenu)
markingMenu.markingMenu()
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment