Passing mel scripts as python in Maya | Tech Art Bites
Updated: May 21
Have you ever tried reverse-engineering a MEL command in Maya only to find the python equivalent doesn't exist? That happens to me sometimes and it makes me so sad y'all. :(
There is a way to make it work, at the cost of making your script slower. So I'd avoid it unless you can't work around it.
We start by importing the mel library at the very top of our script:
import maya.mel as mel
And whenever we want to use a mel command we pass it as a string to mel.eval()
mel.eval('searchReplaceNames "left" "right" "selected";')
Note: if your mel arguments are being passed as strings inbetween " " (like above), you should use ' ' to define the beginning and end of your command string so Maya doesn't get confused.
You can still modify your arguments by using string concatenation. Just make sure the arguments are always passed as strings or make them into strings with the str() method.
mel.eval("rotate -r -rotate" + axis + " -ocp -os -fo " + amount + ";")
Anyways, remember that mel makes the script lower, so avoid it if you can! Specially if you run it in a loop, oh boy......