MEL scripts


Connects a bokeh node with distance and parents one locator to the camera.

global proc bokeh(){
	string $sel[] = `ls -sl`;
	float $childPos[] = `xform -q -t $sel[0]`;
	float $camPos[] = `xform -q -t $sel[1]`;
	string $cShape[] = `listRelatives -s $sel[1]`;
	string $bokehNode = ($sel[1]+"_bokeh");
	string $ddShape = ($cShape[0]+"_ddm");
	string $dd = `distanceDimension -sp $childPos[0] $childPos[1] $childPos[2] -ep $camPos[0]
	$camPos[1] $camPos[2]`; 
	rename $dd $ddShape;
	string $con[] = `listConnections $ddShape`;
	parent $con[1] $sel[1];
	string $bokeh = `mrCreateCustomNode -asUtility "" mia_lens_bokeh`;
	rename $bokeh $bokehNode;
	connectAttr -f ($bokehNode+".message") ($cShape[0]+".miLensShader");
	connectAttr -f ($ddShape+".distance") ($bokehNode+".plane");



Not sure how many people are going to use something like this, but I thought I’d share just in case.

Considering the network for the mip_ shaders (in this case for compositing) is ridiculous, this helps it along. This network is based on what’s seen here:

Select camera and run.

global proc setMipBG(){
	string $sel[] = `ls -sl`;
	string $selShapes[] = `listRelatives -f $sel[0]`;
	string $camPath = $selShapes[0];
	string $emptyAlpha = `mrCreateCustomNode -asUtility "" mib_color_alpha`;
	setAttr ($emptyAlpha+".factor") 0;
	string $cEnv = `mrCreateCustomNode -asUtility "" mip_rayswitch_environment`;
	string $cHdr = `mrCreateCustomNode -asUtility "" mip_mirrorball`;
	setAttr ($cHdr+".degamma") 2.2;
	string $mEnv = `mrCreateCustomNode -asUtility "" mip_rayswitch_environment`;
	string $mCmap = `mrCreateCustomNode -asUtility "" mip_cameramap`;
	setAttr ($mCmap+".degamma") 2.2;
	string $ms = `mrCreateCustomNode -asShader "" mip_matteshadow`;
	connectAttr -f ($cEnv+".message") ($camPath+".miEnvironmentShader");
	connectAttr -f ($emptyAlpha+".outValue") ($cEnv+".background");
	connectAttr -f ($emptyAlpha+".outValueA") ($cEnv+".backgroundA");
	connectAttr -f ($cHdr+".outValue") ($cEnv+".environment");
	connectAttr -f ($cHdr+".outValueA") ($cEnv+".environmentA");
	connectAttr($emptyAlpha+".outValue") ($mEnv+".background");
	connectAttr($emptyAlpha+".outValueA") ($mEnv+".backgroundA");
	connectAttr($mCmap+".outValue") ($mEnv+".environment");
	connectAttr($mCmap+".outValueA") ($mEnv+".environmentA");
	connectAttr ($mEnv+".outValue") ($ms+".background");
	connectAttr ($mEnv+".outValueA") ($ms+".backgroundA");

Feel free to tear it up, but for me I run this script when I’m ready to start the rendering-for-compositing home stretch. Gets the nodes connected up (sans textures/shading groups) and I can apply it to different cameras/export with ease.


Hi folks, I’m new here, just thought I’d post a neat little script to change the value of a checkbox according to whether or not it was checked before (i.e. in a window that was later closed)

window testWindow;
  	-onCommand $onValue
  	-offCommand $offValue
  	-value $testValue
  showWindow testWindow;
  int $testValue = `checkBox -q -v testBox`;
  string $onValue = "$testValue = 1";
  string $offValue = "$testValue = 0";

It’s nothing special but I think it’s pretty cool :thumbsup:


Hi Camaro!

If you thought thats cool. Check this out:
you can have that with an optionVar much cleaner and straightforward!

			-v (`optionVar -q myTextCheckBoxOptionVar`)
			-cc ("optionVar -iv myTextCheckBoxOptionVar #1");

plus: with the secret #1 placeholder you automatically put in the current value of the control. Works with almost all controls :thumbsup:


Hey thanks, I’ll have to test that out! :bowdown:


Ok, I have to say, I went through the command reference, and I don’t understand it one bit. Could someone explain how to use optionVar in detail?


He Camaro!

optionVar is the one command Maya uses itself to save all the preferences. And you can utilize it yourself very easily! Its clean, fast and the best: persistent!

Even if the optionVar-name does not exist

optionVar -q "yourOptionVarName";

returns a proper integer 0.
So you can use it in your checkBox and create/toggle the optionVar on -changeCommand with:

optionVar -intValue "yourOptionVarName" $yourIntValue;

for instance.

in the same style you can have floats and strings saved as well.
What else is unclear?


So you use the -intValue flag when its a 1 or 0 like a checkbox?


The -value flag awaits an integer. So you can feed it with anything that returns one.

checkBox -v 1;
checkBox -v $myIntVar;
checkBox -v ((int) $aFloatVariable);
checkBox -v (`optionVar -q myTextCheckBoxOptionVar`);

IF “myTextCheckBoxOptionVar” is defined as an -intValue optionVar of course…


Any reason MEL can’t interpret a decimal value, such as (35 / 100)? This could screw up my whole script if I can’t get something simple like that to work. All I need is a decimal/float value.

My only other alternative is to take a value like 35 and convert it to .35 somehow.



float $result = (35.0 / 100.0)


or in a call you can always cast variables to a certain type like I wrote in the example: ((int) $floatVar) or ((float) $intVar1 / $intVar2)


Is there any way to make it so you can specify the shape of a button other than a square/box? Or perhaps use an image as a button somehow (not a symbolButton, mind you)?

I’m trying to emulate this:

But obviously not as advanced, as I’m pretty sure he uses Python. I’m just trying to make it so the user can select parts of a rig from an image without simple boxes for the buttons.

Sorry for turning this into a question thread, I just don’t want to clog up the main forum with a bunch of different threads/questions.


the command iconTextButton should work for you, it’ll still be square but you can use an image instead.

u can also make the image your using transparent by making the background of your .bmp 0 254 0 rgb colors and indexing it to the first slot in your color index.


The functional shape of Mayas buttons will always be rectangular. Maybe you go pyQT? Don’t know of its capability now but should be huge ^^

At least you can have mouseover-images utilising the shelfButton images.

Thats exactly what its there for!! :]


sorry for helping to move this thread out of track. Some more helpful little melScripts:

I often animate lots of nodes. This simply copys the animation of 1 object to all other selected ones, no selection order to care about:

	string $sel[] = `ls -sl`;
	// get animcurves from selection and route back to animated transforms
	string $animated[] = stringArrayRemoveDuplicates(listConnections(`keyframe -q -name`));
	if (!size($animated))
		error"no animated obj!";
	if (size($animated) > 1)
		error"can only copy from 1 animated obj!";
	// so if there is only 1 animated obj we can go on:
	// remove the animated one from the selection array
	$sel = stringArrayRemove($animated,$sel);
	// copy from anmated, paste on all others:
	copyKey $animated[0];
	pasteKey $sel;


actually i want to know abt autosuspension ,means i want to do suspension in my scene my car is passing through hilly area,so the road has many up , downs & i dont want to creat suspension manually.i i want to do it through expression,is there any way to do my work throuh expression or script.plz help me.i m creating my demo real.


Hi ranjeetthecgartist!
What about posting your question in a new topic in the main thread?
This here is about posting/sharing and discussing existing scripts.


I made a little time saver for those cases that you want a reflection to appear at a specific place on your object. (see the screenshot and the render)

This script creates two straight lines, grouped together. You position the main one (the one with less expressions on it) pointing to the camera and the tip of the two lines should be on the surface.

 curve -d 3 -p 0.00509434 0 0.251053 -p 0.333085 0 0.256715 -p 0.661075 0 0.262377 -p 0.989066 0 0.268039 -k 0 -k 0 -k 0 -k 1 -k 1 -k 1 ;
 select -r[0] ;
 move 0 0 0 ;
 select -r[3] ;
 move 6 0 0 ;
 select -r[2] ;
 move 4 0 0 ;
 select -r[1] ;
 move 2 0 0 ;
 select -cl  ;
 select -r[0:3] ;
 hilite[0:3] ;
 select -d[0:3] ;
 hilite -u curve1 ;
 select -r curve1 ;
 duplicate -rr;
 // Result: curve2 //
 rotate -r -ws 0 -140.506888 0 ;
 rotate -r -ws 0 -39.628589 0 ;
 setAttr "curve2.rotateY" 180;
 makeIdentity -apply true -t 1 -r 1 -s 1 -n 2 -jointOrient;
 select -r curve1 ;
 makeIdentity -apply true -t 1 -r 1 -s 1 -n 2 -jointOrient;
 expressionEditor EE "curve1" "translateX";
 expression -s "curve1.scaleY = 1\r"  -o curve1 -ae 1 -uc all ;
 // Result: expression1 // 
 expression -s "curve1.scaleZ = 1"  -o curve1 -ae 1 -uc all ;
 // Result: expression2 // 
 select -r curve2 ;
 expression -s "curve2.scaleX = curve1.scaleX"  -o curve2 -ae 1 -uc all ;
 // Result: expression3 // 
 expression -s "curve2.scaleY = curve2.scaleY"  -o curve2 -ae 1 -uc all ;
 // Result: expression4 // 
 expression -s "curve2.scaleZ = curve1.scaleZ"  -o curve2 -ae 1 -uc all ;
 // Result: expression5 // 
 expression -e -s "curve2.scaleY = curve1.scaleY"  -o curve2 -ae 1 -uc all  expression4;
 // Result: expression4 // 
 expression -s "curve2.rotateX = curve1.rotateX"  -o curve2 -ae 1 -uc all ;
 // Result: expression6 // 
 expression -s "curve2.rotateY = -curve1.rotateY"  -o curve2 -ae 1 -uc all ;
 // Result: expression7 // 
 expression -s "curve2.rotateZ = curve1.rotateZ"  -o curve2 -ae 1 -uc all ;
 // Result: expression8 //  
 select -r curve1 curve2 ;
 group; xform -os -piv 0 0 0;


I’m new here so I really appreciate it