How Can We Help?

Maya Script Clips

You are here:
< Back to Wiki

Variables

Global vs Local

global int $varname = 3; – This is a global variable and can be seen by other scripts

Defining a Variable

int $varname; – Integer value. Example int $varname = 10;

float $varname; – Float value. Example float $varname = 1.2345;

string $varname; – Creating a string. Example string $varname = “Hello World”;

vector $varname; – Creates a 3 digit vector. Example vector $varname = <<1,0,0>> or = <<255,128,0>>;

$varname[]; – This is for arrays

matrix $varname[3][4]; – Creates a Matrix


Int and Floats math

int $varA = 5;
float $varB = 5.123;
$varA + $varB = 10; < – You can get a float out of a int
$varB + $varA = 10.123;

Strings

string $varname; – Creating a string. Example string $varname = “Hello World”;

Line Options
"\n" = New line
"\t" = Tab
"\\" = Backslash

Examples:

string $varname = "Hello World";
// Result: “Hello world” //

print ($varname + ", Hello computer" ;
// Result: “Hello world, Hello computer” //

 

Vectors

Vectors are always three floats values

Examples:
vector $varname = <<255,128,0>>;
// Result: <<255, 128, 0>> //

print ($varname.x)
// Result: 255 //

Set a value
$varname = <<123,$varname.y,$varname.z>>;
// Result: <<123, 128, 0>> //

Arrays

int $varname[] = {1,2,3};
float $varname[] = {1.25,2.8,3.0};
string $varname[] = {"one","two","three"};

clear($varname); – Will clear an array out

Examples:

int $varname[] = {1,2,3};
$varname[0] = 9
// Result: 9 //

int $varname[] = {1,2,3};
$varname[0] = 9;
print ($varname);
// Result: 9 //
// Result: 2 //
// Result: 3 //

Looking for a value in a array. This returns a True (1) or False (0).
string $Lists[] = {"one","two","three"};
string $UserInput = "three";

print (stringArrayContains($UserInput, $Lists));
// Result: 1 //

 

 

Matrix

matrix $matrixname[Row#][Column#] = << 1, 2, 3, 4 ; 5, 6, 7, 8 ; 9, 10, 11, 12>>;
// Result: << 1 2 3 4; 5 6 7 8; 9 10 11 12 >> //

1 2 3 4
5 6 7 8
9 10 11 12

 

Script Clips

Get current render
GetCurrentRenderer();
// Result: arnold //

Get list of all render layers
string $GetRenderLayers[]=`ls -type renderLayer`;
// Result: defaultRenderLayer //

getAttr ($isRenderLayerEnabled + “.renderable “) == 1)

Gets frame padding via a number
setAttr ("defaultRenderGlobals.extensionPadding",4);
getAttr ("defaultRenderGlobals.extensionPadding");

// Result: 4 // myrender.####.exr

Gets format type via a number
setAttr( "defaultRenderGlobals.outFormatControl",1)
getAttr( "defaultRenderGlobals.outFormatControl")
  • 1 = name (Single Frame)
  • 2 = name.ext (Single Frame)

Pixel Aspect
setAttr( "defaultResolution.pixelAspect",1.25)
getAttr( "defaultResolution.pixelAspect")

Gets a list of all cameras in the scene
string $all_cameras[] = `ls -type camera`;
// Result: frontShape perspShape sideShape topShape //

 

EXR compression
getAttr( "defaultArnoldDriver.exrCompression" );
// Result: 3 //

32 or 16 bit EXR
getAttr( "defaultArnoldDriver.halfPrecision" );
// Result: 0 //

EXR tiled On/Off
getAttr( "defaultArnoldDriver.exrTiled" );
// Result: 1 //

Auto Crop On/Off
getAttr( "defaultArnoldDriver.autocrop" ) 
// Result: 0 //

MultiPart EXR On/Off
getAttr( "defaultArnoldDriver.mergeAOVs" );
// Result: 0 //

Finding Version name
import os
print "PATH : " + cmds.file(q=True, sn=True)
print "BASENAME : " + os.path.basename(cmds.file(q=True, sn=True).split('.')[0])
print "VERSION : _v"+ os.path.basename(cmds.file(q=True, sn=True)).split('.')[1].split('mb')[0]

Random Rotate:
vector $rndRot = << 0,0,90 >>;
for ($object in `ls -sl -tr`)
rotate `rand ($rndRot.x)` `rand ($rndRot.y)` `rand ($rndRot.z)` $object;

Leave a Reply

Table of Contents