Run Python in the middle of mel?


#1

Hi,

I really need help on how to run few lines of python in the middle of mel. I know there is that for mel there is “python” command but how to use it? Couldn’t find any solution around on internet.
Do i need to save python script separately then call it using mel? Or?

So I have:

PolygonBooleanDifference;
select -r “transform*”;
.
.
.
import maya.cmds as mc
sel_objs = mc.ls(sl = True)
mc.select(sel_objs[:-1], tgl = True)

.
.
.
few more lines of mel.

thanks!


#2

the python() command takes a string as python command. So in your case:

string $cmd = "import maya.cmds as mc;sel_obj = mc.ls(sl=True);mc.select(sel_obj[:-1], tgl=True)";
python($cmd);

Should work. Are these only example commands? You can easily do the same in mel without any python, or better everything in python without mel :wink:


#3

That was fast! Thanks a ton! It works!

Ah, i wish i could do that python part only with mel but i am completely new in this and just starting to learn a bit of mel to automate few things i using constantly. I couldn’t figure it out with mel so i managed to find part i need by google it around. :slight_smile:


#4

For reference, the python snippet converted to MEL would look like this:

Python:


import maya.cmds as mc                    
  sel_objs = mc.ls(sl = True)                 
  mc.select(sel_objs[:-1], tgl = True)


MEL:


 string $sel_objs[] = `ls -sl`;
 int $sel_size = size($sel_objs);
 // sel_objs[:-1] is popping the the last element off the list.
 // MEL arrays don't support index range operations, so we explicitly pop the last elem.
 stringArrayRemoveAtIndex( $sel_size-1, $sel_objs );
 select -tgl $sel_objs;

 

#5

Thanks Keilun!

I would not be able to figure it out on my own that’s for sure but i’ll get there eventually! :wink: