smallarrows.GIF
titlebar_image

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


//COLOR MANAGEMENT RAW
colorManagementPrefs -edit -cmEnabled 1;
colorManagementPrefs -edit -viewTransformName "Raw (sRGB)";
colorManagementPrefs -edit -outputTransformEnabled 1;  //Apply Output Transform to Renderer
colorManagementFileRules -edit -colorSpace "Raw" "Default";
colorManagementPrefs -colorManageAllNodes; //Reapply Rules to scene


//RENDER SETTINGS  Arnold
setAttr "defaultRenderGlobals.currentRenderer" -type "string" "arnold";
  setAttr "defaultArnoldRenderOptions.AASamples" 4;
  setAttr "defaultArnoldRenderOptions.GISpecularSamples" 1;
  setAttr "defaultArnoldRenderOptions.GITransmissionSamples" 0;
  setAttr "defaultArnoldRenderOptions.GISssSamples" 0;
  setAttr "defaultArnoldRenderOptions.GIVolumeSamples" 0;
setAttr "defaultArnoldDriver.ai_translator" -type "string" "tif";  //may need to reset defaults
setAttr "defaultRenderGlobals.animation" 1;
setAttr "defaultRenderGlobals.putFrameBeforeExt" 1;
setAttr "defaultRenderGlobals.extensionPadding" 4;
setAttr "defaultRenderGlobals.startFrame" 1;
setAttr "defaultRenderGlobals.endFrame" 1500;
setAttr "defaultArnoldDriver.dither" 0;
setAttr "defaultResolution.width" 1920;
setAttr "defaultResolution.height" 1200;
setAttr "defaultResolution.deviceAspectRatio" 1.6;

//RENDER SETTINGS  Maya Software Render
setAttr "defaultRenderGlobals.currentRenderer" -type "string" "mayaSoftware";
  setAttr "defaultRenderQuality.edgeAntiAliasing" 0;
  setAttr "defaultRenderQuality.shadingSamples" 2;
  setAttr "defaultRenderQuality.maxShadingSamples" 8;
  setAttr "defaultRenderGlobals.jitterFinalColor" 0;  //test
setAttr "defaultRenderQuality.enableRaytracing" 1;
  setAttr "defaultRenderQuality.reflections" 0;
  setAttr "defaultRenderQuality.refractions" 0;
  setAttr "defaultRenderQuality.rayTraceBias" 0.02;  //helps shadows
  setAttr "defaultRenderGlobals.enableStrokeRender" 0; //turn off paint effects
setAttr "defaultResolution.width" 1920;
setAttr "defaultResolution.height" 1200;
setAttr "defaultResolution.deviceAspectRatio" 1.6;
setAttr "defaultRenderGlobals.imageFormat" 3;  //tiff
setAttr "defaultRenderGlobals.animation" 1;
setAttr "defaultRenderGlobals.putFrameBeforeExt" 1;
setAttr "defaultRenderGlobals.extensionPadding" 4;
setAttr "defaultRenderGlobals.startFrame" 1;
setAttr "defaultRenderGlobals.endFrame" 1500;

//PERSP CAMERA
camera -e -displayResolution on persp;  //turn on Resolution Gate
camera -e -displayGateMask off persp;   //Turn off Mask
setAttr "persp.translateX" 0;
setAttr "persp.translateY" 0;
setAttr "persp.translateZ" 10;
setAttr "persp.rotateX" 0;
setAttr "persp.rotateY" 0;
setAttr "persp.rotateZ" 0;
setAttr "perspShape.focalLength" 35;
setAttr "perspShape.horizontalFilmAperture" 1.417;
setAttr "perspShape.verticalFilmAperture" .945;
setAttr "perspShape.centerOfInterest" 45;
setAttr "perspShape.filmFit" 1;  //horizontal

//TIMELINE
playbackOptions -minTime 1 -maxTime 1500 -animationStartTime 1 -animationEndTime 1500;
currentTime 1 ;
currentUnit -t film; //24fps  ntsc 30 fps




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;
     //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



More Shelf Commands
//some default arnold lights
cmdSkydomeLight;
cmdArnoldAreaLights;
setAttr "aiSkyDomeLightShape1.aiSamples" 2;
setAttr "aiSkyDomeLightShape1.aiCastVolumetricShadows" 0;
setAttr "aiAreaLightShape1.exposure" 8;
setAttr "aiAreaLightShape1.aiSamples" 2;
setAttr "aiAreaLightShape1.aiCastVolumetricShadows" 0;
setAttr "aiAreaLight1.rotateY" -45;
setAttr "aiAreaLight1.rotateX" -45;
setAttr "aiAreaLight1.translateX" -5;
setAttr "aiAreaLight1.translateY" 5;
setAttr "aiAreaLight1.translateZ" 5;

//camera film gate
camera -e -displayFilmGate off -displayResolution on -overscan 1.3 persp;
camera -e -displayGateMask off persp; updateCameraToggleMenu("MainPane|viewPanes|modelPanel4|modelPanel4|View|menuItem5173","modelPanel4");



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 $selection[] = `ls -sl`;
for ($s in $selection) {
if(`objExists $s`) {
print($s+"\n");
select -r ($s);
float $cx[0] = `xform -q -ws -t $s`;
float $cy[1] = `xform -q -ws -t $s`;
float $cz[2] = `xform -q -ws -t $s`;
float $posx = rand(-.1, .1);
float $posy = rand(-.1, .1);
float $posz = rand(-.1, .1);
setAttr ($s+".translateX") ($posx + $cx[0]);
//setAttr ($s+".translateY") ($posy + $cy[1]);
setAttr ($s+".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 $s[] = `ls -sl`;
for($i=0;$i<size($s);$i++) {
print ($s[$i]+"\n");
polySmooth  -mth 0 -sdt 2 -ovb 1 -ofb 3 -ofc 0 -ost 1 -ocr 0 -dv 1 -bnr 1 -c 1 -kb 1 -ksb 1 -khe 0 -kt 1 -kmb 1 -suv 1 -peh 0 -s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aleX") (rand(50,300));
setAttr ($s[$i]+".scaleY") (rand(50,300));
softSelect -e -softSelectEnabled true;
softSelect -e -softSelectDistance 100;
select -r ($s[$i]+".vtx[348]") ;
move -r -1.479345 -12.173582 -7.263036 ;
select -r ($s[$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 $s[] = `ls -sl`;
for($i=0;$i<size($s);$i++) {
print ($s[$i]+"\n");
setAttr ($s[$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 $s="ss";
if(`objExists ($s+"_standard")`) {rename ($s+"_standard") ($s+"_standard"+"x");};
if(`objExists ($s+"_c")`) {rename ($s+"_c") ($s+"_c"+"x");};
if(`objExists ($s+"_place2dTexture")`) {rename ($s+"_place2dTexture") ($s+"_place2dTexture"+"x");};
shadingNode -n ($s+"_standard") -asShader standardSurface;
shadingNode -n ($s+"_c") -asTexture -isColorManaged file;
shadingNode -n ($s+"_place2dTexture") -asUtility place2dTexture;
connectAttr -f ($s+"_place2dTexture"+".coverage") ($s+"_c"+".coverage");
connectAttr -f ($s+"_place2dTexture"+".translateFrame") ($s+"_c"+".translateFrame");
connectAttr -f ($s+"_place2dTexture"+".rotateFrame") ($s+"_c"+".rotateFrame");
connectAttr -f ($s+"_place2dTexture"+".mirrorU") ($s+"_c"+".mirrorU");
connectAttr -f ($s+"_place2dTexture"+".mirrorV") ($s+"_c"+".mirrorV");
connectAttr -f ($s+"_place2dTexture"+".stagger") ($s+"_c"+".stagger");
connectAttr -f ($s+"_place2dTexture"+".wrapU") ($s+"_c"+".wrapU");
connectAttr -f ($s+"_place2dTexture"+".wrapV") ($s+"_c"+".wrapV");
connectAttr -f ($s+"_place2dTexture"+".repeatUV") ($s+"_c"+".repeatUV");
connectAttr -f ($s+"_place2dTexture"+".offset") ($s+"_c"+".offset");
connectAttr -f ($s+"_place2dTexture"+".rotateUV") ($s+"_c"+".rotateUV");
connectAttr -f ($s+"_place2dTexture"+".noiseUV") ($s+"_c"+".noiseUV");
connectAttr -f ($s+"_place2dTexture"+".vertexUvOne") ($s+"_c"+".vertexUvOne");
connectAttr -f ($s+"_place2dTexture"+".vertexUvTwo") ($s+"_c"+".vertexUvTwo");
connectAttr -f ($s+"_place2dTexture"+".vertexUvThree") ($s+"_c"+".vertexUvThree");
connectAttr -f ($s+"_place2dTexture"+".vertexCameraOne") ($s+"_c"+".vertexCameraOne");
connectAttr ($s+"_place2dTexture"+".outUV") ($s+"_c"+".uv");
connectAttr ($s+"_place2dTexture"+".outUvFilterSize")($s+"_c"+".uvFilterSize");
defaultNavigation -ce -source ($s+"_c") -destination ($s+"_standard"+".baseColor");
connectAttr -f ($s+"_c"+".outAlpha") ($s+"_standard"+".opacityR");
connectAttr -f ($s+"_c"+".outAlpha") ($s+"_standard"+".opacityG");
connectAttr -f ($s+"_c"+".outAlpha") ($s+"_standard"+".opacityB");
setAttr ($s+"_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 $selection[] = `ls -sl`;
for ($s in $selection) {
if(`objExists $s`) {
print($s+"\n");
$i++;
select -r $s ;
if(`objExists ($s+"_standard"+$i)`) {rename ($s+"_standard"+$i) ($s+"_standard"+$i+"x");};
if(`objExists ($s+"_c"+$i)`) {rename ($s+"_c"+$i) ($s+"_c"+$i+"x");};
if(`objExists ($s+"_place2dTexture")`) {rename ($s+"_place2dTexture") ($s+"_place2dTexture"+"x");};
shadingNode -n ($s+"_standard"+$i) -asShader standardSurface;
assignCreatedShader "standardSurface" "" ($s+"_standard"+$i) $s;
shadingNode -n ($s+"_c"+$i) -asTexture -isColorManaged file;
shadingNode -n ($s+"_place2dTexture"+$i) -asUtility place2dTexture;
connectAttr -f ($s+"_place2dTexture"+$i+".coverage") ($s+"_c"+$i+".coverage");
connectAttr -f ($s+"_place2dTexture"+$i+".translateFrame") ($s+"_c"+$i+".translateFrame");
connectAttr -f ($s+"_place2dTexture"+$i+".rotateFrame") ($s+"_c"+$i+".rotateFrame");
connectAttr -f ($s+"_place2dTexture"+$i+".mirrorU") ($s+"_c"+$i+".mirrorU");
connectAttr -f ($s+"_place2dTexture"+$i+".mirrorV") ($s+"_c"+$i+".mirrorV");
connectAttr -f ($s+"_place2dTexture"+$i+".stagger") ($s+"_c"+$i+".stagger");
connectAttr -f ($s+"_place2dTexture"+$i+".wrapU") ($s+"_c"+$i+".wrapU");
connectAttr -f ($s+"_place2dTexture"+$i+".wrapV") ($s+"_c"+$i+".wrapV");
connectAttr -f ($s+"_place2dTexture"+$i+".repeatUV") ($s+"_c"+$i+".repeatUV");
connectAttr -f ($s+"_place2dTexture"+$i+".offset") ($s+"_c"+$i+".offset");
connectAttr -f ($s+"_place2dTexture"+$i+".rotateUV") ($s+"_c"+$i+".rotateUV");
connectAttr -f ($s+"_place2dTexture"+$i+".noiseUV") ($s+"_c"+$i+".noiseUV");
connectAttr -f ($s+"_place2dTexture"+$i+".vertexUvOne") ($s+"_c"+$i+".vertexUvOne");
connectAttr -f ($s+"_place2dTexture"+$i+".vertexUvTwo") ($s+"_c"+$i+".vertexUvTwo");
connectAttr -f ($s+"_place2dTexture"+$i+".vertexUvThree") ($s+"_c"+$i+".vertexUvThree");
connectAttr -f ($s+"_place2dTexture"+$i+".vertexCameraOne") ($s+"_c"+$i+".vertexCameraOne");
connectAttr ($s+"_place2dTexture"+$i+".outUV") ($s+"_c"+$i+".uv");
connectAttr ($s+"_place2dTexture"+$i+".outUvFilterSize")($s+"_c"+$i+".uvFilterSize");
defaultNavigation -ce -source ($s+"_c"+$i) -destination ($s+"_standard"+$i+".baseColor");
connectAttr -f ($s+"_c"+$i+".outAlpha") ($s+"_standard"+$i+".opacityR");
connectAttr -f ($s+"_c"+$i+".outAlpha") ($s+"_standard"+$i+".opacityG");
connectAttr -f ($s+"_c"+$i+".outAlpha") ($s+"_standard"+$i+".opacityB");
setAttr ($s+"_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 $selection[] = `ls -sl`;
for ($s in $selection) {
if(`objExists $s`) {
print($s+"\n");
$i++;
rename ($s) ("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-2024 36 years