I am not sure what it is, but there is something incredibly appealing in optimizing our workflows. I think a lot of it comes from the frustration of repeating the same actions over and over again. When you find a way to optimize that, it feels great. One of the easiest way to improve our rigging workflow is to script a custom marking menu with python. Another one, I have already written about is creating a custom shelf.
tl;dr: I will walk you through scripting your own custom marking menu with python, which is going to be easily shareable, extendable and maintainable. The code can be found here.
Here is how my main marking menu looks.
I have found it is an immense help to have the commands I use most oftenly either in my marking menu or my shelf. It just saves so much time!
Okay, how do we go about creating one of these?
Well, we have two options – either build it with Maya’s native marking menu editor or script it with python.
The reasons I prefer scripting my marking menus in python are a few.
– The native editor does not give us all the available options for a marking menu, such as submenus.
– Updating from the editor is a pain in the butt.
– The editor does not scale nicely, if you want or need to support multiple marking menus.
– Doing it through the editor is boring.
Obviously, python fixes all these issues for us. Additionally, it is easy to share it with co-workers and keep it in a version control system. Okay, so you are sold now. Let us have a look at how to do it then.
The code that I will be going through is on this gist, but I will go through all of it, if you would rather write it yourself.
Custom marking menu with Python
import maya.cmds as mc
MENU_NAME = "markingMenu"
We start very simple with the import of
maya.cmds and giving a name to our custom marking menu. Now, the name is not very important because we do not ever see it, but maya does. So, in order for us to be able to update our custom marking menu, we need to be able to access it, and that is why we are giving it a name.
Then we have our
class. The main reason I went for a class is because we can encapsulate everything we need into it quite logically. I know that some people would prefer to have a function instead, which is absolutely fine, it is really a personal preference. Let’s have a look at the constructor.
As you can see, our constructor is very simple. We delete the old version of our custom marking menu if it exists and then we build are our new one in place.
Remove old marking menu
if mc.popupMenu(MENU_NAME, ex=1):
As I said, the reason we need to give a name to our marking menu is to be able to modify it after it is created. In our case, we are not really modifying it, but deleting it instead. We do this, so we have a clean slate for building our new marking menu.
The reason I have added this deleting and then building again functionality is just so we can painlessly make changes to our custom marking menu. For example, if I want to add a new button or change a label, I do not want to have to restart maya or do anything other than just running my code again. Or even easier, just importing my code again. That is why I said earlier, that this setup for a custom marking menu is very easily extendable and maintainable. Additionally, we can add a button to our marking menu which rebuilds it, so we only have to make our changes to the file, save it and then rebuild from within. We will have a look at that in the end of the post.
Building our custom marking menu
Now that we have had a look at preparing for our build let us have a look at the
menu = mc.popupMenu(MENU_NAME, mm = 1, b = 2, aob = 1, ctl = 1, alt=1, sh=0, p = "viewPanes", pmo=1, pmc = self._buildMarkingMenu)
Another very simple method. I was surprised that maya does not have a specific
markingMenu method. Instead, the
popupMenu() command is used, which is actually nice, since it is a familiar one if you have worked with menus or shelf popups. Let us look at the arguments.
- MENU_NAME – quite obviously this one sets the name of our custom marking menu.
- mm – this one defines this
popupMenu as a marking menu.
- b – this is the mouse button we would like to trigger the marking menu. 1 – left, 2 – middle, 3 – right.
- aob – allows option boxes.
- ctl – defines the Ctrl button as a needed modifier to trigger the marking menu.
- alt – defines the Alt button as a needed modifier to trigger the marking menu.
- sh – defines the Shift button as a needed modifier to trigger the marking menu.
- p – the parent ui element. For marking menus, that would usually be “viewPanes”, which refers to all of our view panels.
- pmo – this flag declares that the command that we pass to the
pmc flag should be executed only once and not everytime we invoke the marking menu. If this is
false, everytime we trigger our custom marking menu, we will see our menus growing as more and more
menuItems will be added.
- pmc – this is the command that gets called right before the
popupMenu is displayed. This is where we need to pass our method that actually builds all our buttons and menus –
It is important to notice that when we pass the
self._buildMarkingMenu we do not add brackets in the end as that would call the function instead of passing it as as reference.
Additionally, it is also important to think about the button flag and the modifiers. Obviously, Maya already has some marking menus and some functions related to mouse clicks and the ctrl, alt and shift modifiers. Therefore, we need to come up with a combination that does not destroy a function which we actually want to keep. That is why, for my marking menu I use the middle mouse button + alt + ctrl. There was some zooming function bound to that combination I believe, but I never used it so it was safe to override.
Actually building our custom marking menu
So, everything up to this point was to set us up for actually adding our commands to our menu. To be honest, it is as simple as everything we have already seen. Let us have a look at how we do this.
def _buildMarkingMenu(self, menu, parent):
## Radial positioned
mc.menuItem(p=menu, l="South West Button", rp="SW", c="print 'SouthWest'")
mc.menuItem(p=menu, l="South East Button", rp="SE", c=exampleFunction)
mc.menuItem(p=menu, l="North East Button", rp="NE", c="mc.circle()")
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")
mc.menuItem(p=menu, l="South", rp="S", c="print 'South'")
mc.menuItem(p=menu, ob=1, c="print 'South with Options'")
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()")
The first thing to note is that our method receives
parent as arguments. These are passed automatically from the
pmc flag on the
popupMenu function in the
Then we have the actual items in our custom marking menu. I like to split them logically in – radial and list blocks.
The radial positions are defined by the directions on a map – East, West, NorthEast, etc. Have a look at the following image.
You can have either commands in these slots or additional
subMenus like so.
My personal preference here is to have just single commands instead of additional popups, as to invoke a submenu you need to hover on a position and wait a little bit. And I find that small delay quite frustrating.
So the way we create these radial items is by using the
menuItem command. All we have to do is pass our
menu argument as the parent (
p), define a label (
l), a radial position (
rp) and the command (
c) we want to execute on click.
Notice that if passing functions as commands we pass them without brackets, as that would call them instead. Have a look at the
SE radial position for an example. Additionally, the functions need to be able to receive arguments as maya’s ui elements tend to pass some info about themselves to the commands they call. The way we do that is by just adding the
print "example function"
Additionally, we can call maya commands from our items. Have a look at the
NE item. It is important to note here that even though we have imported
maya.cmds as mc in this file, the commands we pass to our
menuItems are going to be called from maya’s python environment. That means, that in order for
mc.circle() to work, we need to have imported
maya.cmds as mc somewhere in our maya session. You could either run it yourself in the script editor or a better solution would be to add it to your
userSetup.py file. That is what I did, since I use a lot of python in maya I just do an
import maya.cmds as mc in my
As I said we can have
subMenu items, which essentially create another menu when you hover on them. The only thing we do is set the
subMenu flag to 1. Have a look at one of these in our
N radial position example. We store the
menuItem with the
subMenu flag in a variable, so we can use it as a parent for our following items. From then on, we just follow the same principle, we list
menuItems, with the
subMenu variable that we stored as a parent. Additionally, we can have deeper
subMenus as well, though I do not think that would be great to work with.
Another very useful addition to our
menuItems is adding option boxes. A lot of maya’s menus have those and they are a nice way to add additionall functionality without sacrificing space.
Usually, you would add them to commands that sometimes need their options to be changed, but also you can have different commands bound to them. For example, in my main marking menu I have the
joint tool in one of my radial positions. But since I never mess with the options for that tool, in the option box I have a command which creates a joint under every object I have selected. Additionally, it names it with the name of the selected object and assumes it’s transformations.
The way we add an option box is very simple. All we need to do is create another
menuItem after the one we want to add the option box to and we set the
ob flag to 1. Have a look at the
S radial position for an example.
In addition to our radial positioned items, marking menus have another menu beneath the
South radial position. What is cool about this one is that it is an actual menu instead of just a single radial position, so we can have multiple items in it.
The way we create those items is by just specifying the marking menu as a parent. Remember it is passed as the
menu argument to our method. So, if we do not specify a radial position the
menuItem gets added to that menu which is south of the
Again, as with all other menus, we can have
submenus in there, by just setting the
submenu flag to 1. My personal preference though is to have as few of these as possible, as I want everything to be easy to grab at first glance.
I have not added any icons in this example marking menu, but these can easily be added to every
menuItem by using the
i flag. All you need to do is place your custom icons somewhere in the
XBMLANGPATH environment variable. To see what that is on your machine run this MEL command
Additionally, you can use maya’s native icons, which you can browse through in the shelf editor. If you open that, pick any button on the right and next to the Icon Name field you can find a Browse Maya Icons button.
For example if I want to add maya’s standard icon to the
S radial position I would do this.
mc.menuItem(p=menu, l="South", rp="S", c="print 'South'", i="mayaIcon.png")
That results in the following image.
And of course you can also pass full paths to icons which are not on maya’s
Rebuilding our custom marking menu and loading it on startup
So, now that we know how to build our marking menus, let us have a look at how to actually initialize them with maya and how to rebuild them on the fly when we make changes. For building our marking menu we just initialize our
markingMenu() class. For rebuilding we need to do the same thing, but we have several options for how we call our class.
Option 1: Just run it
I would say this is the simplest solution. If you are working in an external editor you can easily copy and paste your modified code in maya’s script editor, run it and you’re done. Since, we have added the rebuilding functionality – delete old and then build new one – the marking menu is updated everytime we run our code and then call the
markingMenu() class in the end.
Even better if you have connected your external editor to maya via a
commandPort command you can just run the code from there and that’ll update your marking menu as well. (I use this plugin for Sublime to do that.
The downside with this option is that everytime we open maya we will need to run our code. Which means that at some point you will get annoyed with doing it.
Option 2: Add it to your scripts path
Even though the first solution is quite simple this one is a bit nicer to work with. It also takes care of loading our marking menu on startup.
So, what we do here is we add the file containing our code –
markingMenu.py – somewhere on our
MAYA_SCRIPT_PATH. Again, you can run
getenv MAYA_SCRIPT_PATH in MEL to get this path.
What this means is that now we can access our marking menu from within maya. Therefore we have a lot of options on how to build/rebuild our marking menu.
For building it on startup we just need to add the following code inside our
When rebuilding though we need to do a
reload statement. That is because when importing a module, python checks whether it is already imported and if so, just uses that instead. So if we make changes and then do
import, our changes are not going to be imported. That is why we need to do
So in the case of reloading our marking menu, we will need to do the following.
This will ensure that whatever changes we have made to our code will be implemented.
Now that we have this rebuilding code, we can add it wherever we find most appropriate. Generally, I would say either a button on a shelf or from within the marking menu itself. Since a button on a shelf is trivial let us look at adding it to the marking menu.
Reloading from within our custom marking menu
The way we would do this, is just add the following
mc.menuItem(p=menu, l="Rebuild Marking Menu", c=rebuildMarkingMenu)
rebuildMarkingMenu refers to the following function.
I really don’t like using
evalDeferred since it feels a bit dirty, but in this case we need it. The reason we need it is that we are rebuilding our marking menu from within. So if we do not use
evalDeferred but directly call our
markingMenu(), maya will have to delete the button which we have clicked, and that will error.
So, there we have it, we have built our own custom marking menu with Python. What is more, it is fully scripted, so making changes is very easy. Additionally, we can have that file in a version control system, so we can have a log of our changes. And of course, it is super easy to share with co-workers. The best thing about it, though, is how much time it saves in our day-to-day rigging tasks.