smallarrows.GIF

156 syllabus      site index      3D artists      past student work        DMA

MEL Scripting

List of Mel commands
Go to Help->Maya Scripting Reference->MEL Command Reference



Run the Script Editor
Open the Windows->General Editors->Script Editor...  or hit the {;} icon in the lower right.
     Paste your script in the lower MEL area
     Use Command->Execute from the editor



Comment a line
// comments a line
always end a command line with a ;   semi colon



Select with a wild card
select -r "nurbsPlane\*";



Select 10 NURBS planes
int $i;
for ($i = 1; $i <= 10; $i++) {        // The variable $i is equal to between 1 and 10
select -tgl ("nurbsPlane"+$i);}



Set up Some Preferences for yourself
Copy and paste these commands into the Windows->General Editors->Script Editor
    //sets animation timing to 30fps
     currentUnit -time ntsc;
     // 0 = play every frame, 1 =change playback to realtime 30fps
     playbackOptions -ps 0;
     //Sets playback bar to 1
     playbackOptions -e -min 1 -max 1000;
     //sets playback to frame start
     playButtonStart;
    //turns off color management
     colorManagementPrefs -edit -cmEnabled 0;
     //turn off the pivot manipulator on the orientation handle.
     prefWndToggleShowPivotManipHandle false;

In the Script Editor run Command->Execute
script_editor_prefs


If you would like this as a command on the shelf:
     Select the Custom shelf
     Highlight the text in the Script Editor
     Drag and drop the text on to the Custom shelf with the middle mouse key



Flush Arnold Textures
Copy and paste these commands into the Windows->General Editors->Script Editor
     cmdArnoldFlushTexture;

If you would like this as a command on the shelf:
     Select the Custom
shelf
     Highlight the text in the Script Editor
     Drag and drop the text on to the Custom shelf with the middle mouse key



Use BevelPlus to make a planar surface
bevelPlus -constructionHistory true  -normalsOutwards true  -range false  -polygon 1 -tolerance 0.01 -numberOfSides 2 -js true  -width 0 -depth 0 -extrudeDepth 0 -capSides 2 -bevelInside 0 -outerStyle 4 -innerStyle 4 -polyOutMethod 2 -polyOutCount 200 -polyOutExtrusionType 3 -polyOutExtrusionSamples 1 -polyOutCurveType 3 -polyOutCurveSamples 1 -polyOutUseChordHeightRatio 0;
expandPolyGroupSelection; polyCleanupArgList 4 { "0","1","1","0","0","0","0","0","0","1e-05","0","1e-05","0","1e-05","0","-1","0","0" };
polyMergeVertex  -d 0.01 -am 1 -ch 1 bevelPolygon1;



Select Every Other Selected Object
//Select more than 1 object
jooFilterSelectionToEveryNthObject 2;
global proc jooFilterSelectionToEveryNthObject(int $n)
{
 string  $selection[], $newSelection[];
 $selection = `ls -sl`;
 for ($i = 0; $i < size($selection); $i += $n)
{
$newSelection[$i/$n] = $selection[$i];
 }
 select -r $newSelection;
};



Select small objects in the scene
//Select small objects in the scene
//Save your scene
global proc GetSmallObjects(    float $xLimit,
                                float $yLimit,
                                float $zLimit ){
        string $sceneObjs[] = ` ls -l `;
select -cl;
        for ($obj in $sceneObjs){
        float $bbox[] = ` exactWorldBoundingBox $obj`;
                float $xSize = $bbox[3] - $bbox[0];
        float $ySize = $bbox[4] - $bbox[1];
        float $zSize = $bbox[5] - $bbox[2];
                if (( $xSize < $xLimit) && ( $zSize < $zLimit) && ( $zSize < $zLimit)){
                        select -add $obj;
        }}}
GetSmallObjects( .01, .01, .01);   //change this value



Randomize a poly sphere
//Use Display->Heads Up Display->Poly Count to see how many vertices are on your sphere
int $i;
for ($i = 1; $i <= 381; $i++) {
select -r pSphere1.vtx[$i] ;
move -r (rand(-.1,.1)) (rand(-.1,.1)) (rand(-.1,.1)) ;}



Randomly Change the Position of Selections
string $selections[] = `ls -sl`;
for ($sel in $selections) {
if(`objExists $sel`) {
print($sel+"\n");
select -r ($sel);
float $cx[0] = `xform -q -ws -t $sel`;
float $cy[1] = `xform -q -ws -t $sel`;
float $cz[2] = `xform -q -ws -t $sel`;
float $posx = rand(-.1, .1);
float $posy = rand(-.1, .1);
float $posz = rand(-.1, .1);
setAttr ($sel+".translateX") ($posx + $cx[0]);
//setAttr ($sel+".translateY") ($posy + $cy[1]);
setAttr ($sel+".translateZ") ($posz + $cz[2]);
};
};



Rock Maker
polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;
string $sel[] = `ls -sl`;
for($i=0;$i<size($sel);$i++) {
print ($sel[$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 1 -kb 1 -ksb 1 -khe 0 -kt 1 -kmb 1 -suv 1 -peh 0 -sl 1 -dpe 1 -ps 0.1 -ro 1 -ch 1 ($sel[$i]);
setAttr ($sel[$i]+".scaleZ") (rand(50,300));
setAttr ($sel[$i]+".scaleX") (rand(50,300));
setAttr ($sel[$i]+".scaleY") (rand(50,300));
softSelect -e -softSelectEnabled true;
softSelect -e -softSelectDistance 100;
select -r ($sel[$i]+".vtx[348]") ;
move -r -1.479345 -12.173582 -7.263036 ;
select -r ($sel[$i]+".vtx[302]") ;
move -r -8.425104 8.101759 4.151861 ;
select -cl  ;
};



Print a list from 1 to 10 with a period and line return
int $i;
for ($i = 1; $i <= 10; $i++) {
print($i+".\n");}



Concatonate
string $object = "cake";
print ($object + ".tx");       //Syntax to string words together for a  command



Select a bunch of texture maps, turn off Quadratic
string $sel[] = `ls -sl`;
for($i=0;$i<size($sel);$i++) {
print ($sel[$i]+"\n");
setAttr ($sel[$i]+".filterType") 0;
};



//Change all selected shaders to SurfaceShader
//This keeps the names
string $replaceType = "standardSurface";
string $sNode[] = `ls -sl`;   
for ($each in $sNode)
    {
    string $replaceWith = `createNode $replaceType`;
    replaceNode $each $replaceWith;
    delete $each;
    rename $replaceWith $each;
    }



//Create a Standard Surface shader, add the texture with alpha channel later.
//Mel script to create a StandardSurface shader using a file color map.
//This sets up a connection to opacity to an alpha channel in the color map.
string $sel="ss";
if(`objExists ($sel+"_standard")`) {rename ($sel+"_standard") ($sel+"_standard"+"x");};
if(`objExists ($sel+"_c")`) {rename ($sel+"_c") ($sel+"_c"+"x");};
if(`objExists ($sel+"_place2dTexture")`) {rename ($sel+"_place2dTexture") ($sel+"_place2dTexture"+"x");};
shadingNode -n ($sel+"_standard") -asShader standardSurface;
shadingNode -n ($sel+"_c") -asTexture -isColorManaged file;
shadingNode -n ($sel+"_place2dTexture") -asUtility place2dTexture;
connectAttr -f ($sel+"_place2dTexture"+".coverage") ($sel+"_c"+".coverage");
connectAttr -f ($sel+"_place2dTexture"+".translateFrame") ($sel+"_c"+".translateFrame");
connectAttr -f ($sel+"_place2dTexture"+".rotateFrame") ($sel+"_c"+".rotateFrame");
connectAttr -f ($sel+"_place2dTexture"+".mirrorU") ($sel+"_c"+".mirrorU");
connectAttr -f ($sel+"_place2dTexture"+".mirrorV") ($sel+"_c"+".mirrorV");
connectAttr -f ($sel+"_place2dTexture"+".stagger") ($sel+"_c"+".stagger");
connectAttr -f ($sel+"_place2dTexture"+".wrapU") ($sel+"_c"+".wrapU");
connectAttr -f ($sel+"_place2dTexture"+".wrapV") ($sel+"_c"+".wrapV");
connectAttr -f ($sel+"_place2dTexture"+".repeatUV") ($sel+"_c"+".repeatUV");
connectAttr -f ($sel+"_place2dTexture"+".offset") ($sel+"_c"+".offset");
connectAttr -f ($sel+"_place2dTexture"+".rotateUV") ($sel+"_c"+".rotateUV");
connectAttr -f ($sel+"_place2dTexture"+".noiseUV") ($sel+"_c"+".noiseUV");
connectAttr -f ($sel+"_place2dTexture"+".vertexUvOne") ($sel+"_c"+".vertexUvOne");
connectAttr -f ($sel+"_place2dTexture"+".vertexUvTwo") ($sel+"_c"+".vertexUvTwo");
connectAttr -f ($sel+"_place2dTexture"+".vertexUvThree") ($sel+"_c"+".vertexUvThree");
connectAttr -f ($sel+"_place2dTexture"+".vertexCameraOne") ($sel+"_c"+".vertexCameraOne");
connectAttr ($sel+"_place2dTexture"+".outUV") ($sel+"_c"+".uv");
connectAttr ($sel+"_place2dTexture"+".outUvFilterSize")($sel+"_c"+".uvFilterSize");
defaultNavigation -ce -source ($sel+"_c") -destination ($sel+"_standard"+".baseColor");
connectAttr -f ($sel+"_c"+".outAlpha") ($sel+"_standard"+".opacityR");
connectAttr -f ($sel+"_c"+".outAlpha") ($sel+"_standard"+".opacityG");
connectAttr -f ($sel+"_c"+".outAlpha") ($sel+"_standard"+".opacityB");
setAttr ($sel+"_c"+".filterType") 0;




//Create and assign a Standard Surface shader, add the texture with alpha channel later.
//Mel script to create a StandardSurface shader with a file color map and a connection to opacity with an alpha channel.
//This creates a new shader with every object selected.
int $i=0;
string $selections[] = `ls -sl`;
for ($sel in $selections) {
if(`objExists $sel`) {
print($sel+"\n");
$i++;
select -r $sel ;
if(`objExists ($sel+"_standard"+$i)`) {rename ($sel+"_standard"+$i) ($sel+"_standard"+$i+"x");};
if(`objExists ($sel+"_c"+$i)`) {rename ($sel+"_c"+$i) ($sel+"_c"+$i+"x");};
if(`objExists ($sel+"_place2dTexture")`) {rename ($sel+"_place2dTexture") ($sel+"_place2dTexture"+"x");};
shadingNode -n ($sel+"_standard"+$i) -asShader standardSurface;
assignCreatedShader "standardSurface" "" ($sel+"_standard"+$i) $sel;
shadingNode -n ($sel+"_c"+$i) -asTexture -isColorManaged file;
shadingNode -n ($sel+"_place2dTexture"+$i) -asUtility place2dTexture;
connectAttr -f ($sel+"_place2dTexture"+$i+".coverage") ($sel+"_c"+$i+".coverage");
connectAttr -f ($sel+"_place2dTexture"+$i+".translateFrame") ($sel+"_c"+$i+".translateFrame");
connectAttr -f ($sel+"_place2dTexture"+$i+".rotateFrame") ($sel+"_c"+$i+".rotateFrame");
connectAttr -f ($sel+"_place2dTexture"+$i+".mirrorU") ($sel+"_c"+$i+".mirrorU");
connectAttr -f ($sel+"_place2dTexture"+$i+".mirrorV") ($sel+"_c"+$i+".mirrorV");
connectAttr -f ($sel+"_place2dTexture"+$i+".stagger") ($sel+"_c"+$i+".stagger");
connectAttr -f ($sel+"_place2dTexture"+$i+".wrapU") ($sel+"_c"+$i+".wrapU");
connectAttr -f ($sel+"_place2dTexture"+$i+".wrapV") ($sel+"_c"+$i+".wrapV");
connectAttr -f ($sel+"_place2dTexture"+$i+".repeatUV") ($sel+"_c"+$i+".repeatUV");
connectAttr -f ($sel+"_place2dTexture"+$i+".offset") ($sel+"_c"+$i+".offset");
connectAttr -f ($sel+"_place2dTexture"+$i+".rotateUV") ($sel+"_c"+$i+".rotateUV");
connectAttr -f ($sel+"_place2dTexture"+$i+".noiseUV") ($sel+"_c"+$i+".noiseUV");
connectAttr -f ($sel+"_place2dTexture"+$i+".vertexUvOne") ($sel+"_c"+$i+".vertexUvOne");
connectAttr -f ($sel+"_place2dTexture"+$i+".vertexUvTwo") ($sel+"_c"+$i+".vertexUvTwo");
connectAttr -f ($sel+"_place2dTexture"+$i+".vertexUvThree") ($sel+"_c"+$i+".vertexUvThree");
connectAttr -f ($sel+"_place2dTexture"+$i+".vertexCameraOne") ($sel+"_c"+$i+".vertexCameraOne");
connectAttr ($sel+"_place2dTexture"+$i+".outUV") ($sel+"_c"+$i+".uv");
connectAttr ($sel+"_place2dTexture"+$i+".outUvFilterSize")($sel+"_c"+$i+".uvFilterSize");
defaultNavigation -ce -source ($sel+"_c"+$i) -destination ($sel+"_standard"+$i+".baseColor");
connectAttr -f ($sel+"_c"+$i+".outAlpha") ($sel+"_standard"+$i+".opacityR");
connectAttr -f ($sel+"_c"+$i+".outAlpha") ($sel+"_standard"+$i+".opacityG");
connectAttr -f ($sel+"_c"+$i+".outAlpha") ($sel+"_standard"+$i+".opacityB");
setAttr ($sel+"_c"+$i+".filterType") 0;
};};




Select a sphere with animation, copy it 10 times, each copy moves 3 units in x
int $i;
for ($i = 1; $i <= 10; $i++) {        // The variable $i is equal to a range between 1 and 10
select -r ("nurbsSphere"+$i);
duplicate -rr -un;
move -r 3 0 0 ;
}



Rename selected objects with a numerical sequence starting from 1
//RENAME
int $i=0;
string $selections[] = `ls -sl`;
for ($sel in $selections) {
if(`objExists $sel`) {
print($sel+"\n");
$i++;
rename ($sel) ("controller"+$i);
};};
};



A script to advance 1 frame up to 150, Select an animated poly sphere and copy it
int $f = 0;
while ($f <= 150) {
playButtonStepForward;
select -r pSphere1;
duplicate -rr;
$f += 1;
};


smallarrows.GIF

© 1988-2021 Jennifer Steinkamp