Maya scripting 101 / by Mauricio Ricaldi

One useful skill to learn in Maya is doing some basic script, it makes repetitive task easier.

Maya has two script languages to work, one is MEL that is the easier one for small tasks and short scripts, this is because you can copy/paste the command from the script editor. The other one is Python that is very similar to MEL, but you can build more complicated tools with it. But don't think MEL is less powerfull, a lot of tools I made was using mel first and then translated to pyhton, and some tools is works better in mel.

As example, we have this scene that have a x number of globes, and we need to select them several times for different tasks.

We open the script editor and make any change to the mesh:

copy/paste this to a MEL window below and it will select that object again.

We need to select all balloons at once, so we enclose the object in "quotes" and add * to any number that we find.

select -r "KJN*Anim_BD:KJN_JungleC_EXT_Auditorium_SD:KJN_BalloonA*:Balloon_Geo" ;

Now we have all selected, we can create a loop to change settings to all of them at once, we need first to store in a container called array, this will be $allObjs[]  and 'ls-sl' will fill the array with the selection.

string $allObjs[] = `ls-sl`;

The next thing is to do a loop, for this we add a variable called $x that will go to each object one by one till runs to the end of the selection that is defined by the size($allObjs).

for($x=0;$x<size($allObjs);$x++) {

As example I'm changing all the object to hold-out, the easier way is change one ballon and check the script editor.

check the last world that the attribute is called ".holdOut", so we need to do this to all the objects.

setAttr ($allObjs[$x] +".holdOut") 1;

setAttr is the command to change things in maya,  we add $allObj[$x] so $x will run from the first object to the last in a loop and then +".holdOut" 1 will change that attribute while looping.

At the end the entire script is:

select -r "KJN*Anim_BD:KJN_JungleC_EXT_Auditorium_SD:KJN_BalloonA*:Balloon_Geo" ;
string $allObjs[] = `ls-sl`;
for($x=0;$x<size($allObjs);$x++) {
 setAttr ($allObjs[$x] +".holdOut") 1;
}

You can change thie ".holdOut" with any other attribute like ".castsShadows", ".primaryVisibility", ".image" etc.