Tutorial for context sensitive mel script/hotkeys in Maya? NEW REPLY


Hey Guys,
Trying to get into scripting to speed up my workflow with modeling.
Are there any examples/tutorials out there to set up simple context dependent hotkeys in maya, based on selection type and selection count

for example if faces are selected execute selectadjacentface.mel, or selectadjacentvertex.mel for vertex selection, etc?

or if 2 verticies are selected it will connect, if 1 vertex is selected the same hotkey would run bevel…

It’s a simple querey seleciton type and count and run a command i guess.


Teacher used to have filterExpand all over his code for this purpose. Hadn’t come across this before but it worked for his tools.

import maya.cmds as mc

selection = mc.ls(sl=True)

#Check if verts, edges, faces or objects are selected
verts = mc.filterExpand(sm=31)
edges = mc.filterExpand(sm=32)
faces = mc.filterExpand(sm=34)
objects = mc.filterExpand(sm=12)

if verts:


There was another similar one but can’t remember now where you could check the current selection mask and then know if you were to act on edges or faces.


Thanks for the reply Robberyman,
This is awesome. I was able to get this to work by adding mc.eval (mymelscript.mel) to the end of this awesome template! Thanks a bunch.

If you don’t mind, I had couple more questions:

Apart from assigning macros to component selection count, is it possible to script context dependent hotkeys based on active tool? for example if soft selection is active my + key would change radius, if not active + would increase manipulator size?

it’s not entirely necessary with maya’s tool dependent marking menu’s to access these options but I was curious.

import pymel.core as pm

# Get current active tool,
ctx = pm.currentCtx()

# Check if soft select is enabled or not,
ss = pm.softSelect(q=True, sse=True)

Found first answer just by running a google “Maya Current Active Tool” which landed me on this CGTalk Post and second checking if softSelect could be queried in the documentation, would recommend whenever you’re attempting something with a tool to check the in documentation flags which can be queried to give you an idea how to make the code more “context aware”.


thanks Roberryman, I very much appreciate the help!


I’ll just throw out there that you may also find the polyListComponentConversion MEL/Python command handy. It can, e.g., convert a pair of vertices into the edge that connects them, etc.


ah thank you gfixler. I have managed to go through a hundred or so functions in maya and condense context :slight_smile:

this python function is interesting, I was using the “to vertex” command and such prior but that changes your current selection.

This scripting stuff is fun!