BLOGGER TEMPLATES AND TWITTER BACKGROUNDS »

Thursday, August 12, 2010

Fix persp camera

Sometimes your persp will begin to tumble on it's axis rather than rotate around the focused object. Quick fix is to just delete it and replace it, but you cannot delete it by default because it's set to be a startupCamera. This will change that attribute and replace it with a new one.

string $camera = findStartUpCamera( "persp" );
string $camera2[] = `camera -n persp -hc "viewSet -p %camera"`;
camera -e -sc 0 $camera;
delete "persp";
rename $camera2[0] "persp";
camera -e -sc 1 "persp";

Sunday, July 18, 2010

Code based on maya version

With the implementation of 2011's fileDialog2 command (which by the way is amazing) you run into the problem of not being able to get it to work on older versions because the command doesn't exist in the version.

Even writing a simple if statement won't work because the command will still get evaluated when you initially source the script. You can use the eval command to fix this though by putting your commands in a string, then evaluating that string.

Mel Version:

float $v = `about -version`;
string $eval;
if ($v < 2011) $eval = "fileDialog";
else $eval = "fileDialog2";
eval($eval);
Python Version:
import maya.cmds as cmds
import maya.mel as mel

myVersion=cmds.about(v=True)
myEval = ""
if myVersion != 2011:
myEval=("fileDialog")
else:
myEval=("fileDialog2")
fullPath=mel.eval(myEval)

Tuesday, June 22, 2010

Make a perfect circle of objects in MEL

You can use this to duplicate a perfect circle of objects, draw lines or all sorts of stuff.

{
// Make a perfect circle of objects!
proc float magicTrigFunctionX(float $pointRatio){
return cos($pointRatio*2*3.14);
}
proc float magicTrigFunctionZ(float $pointRatio){
return sin($pointRatio*2*3.14);
}

proc drawCircle(float $centerX, float $centerZ, float $radius, float $sides){
// Move the original to the first point on the circle.
// Replace newCube with your object below
string $newCube[] = `polyCube`;
string $orig = $newCube[0];
xform -t ($centerX + $radius) 0 $centerZ $newCube[0];
// Run the loop to draw the circle
for($i=0; $i < $sides ; $i++){
float $pointRatio = $i/$sides;
float $xSteps = magicTrigFunctionX($pointRatio);
float $zSteps = magicTrigFunctionZ($pointRatio);
float $pointX = $centerX + $xSteps * $radius;
float $pointZ = $centerZ + $zSteps * $radius;
$newCube = `duplicate $newCube[0]`;
xform -t $pointX 0 $pointZ -ro 0 ($pointRatio*-360) 0 $newCube[0];
}
delete $orig;
}
// Arguments
// centerX, centerZ, radius, number of objects
drawCircle(0, 0, 200, 100);

}

Thursday, June 3, 2010

IK/FK Switch Ratio Expressions

Rather than using a constraint to two joints then changing the contraint values, sometimes I'll use and expression to drive the roll joints in arms/legs or whatever.

//roll joint  |wrist IK joint|ratio|switch controller   |wrist FK joint|ratio|switch controller
roll_jnt.rx = ((wristIK_jnt.rx * .3)*switch.IKFK) + ((wristFK_jnt.rx * .3)*abs(switch.IKFK - 1));

In this expression I am multiplying the wrists rotation by a ratio (0.3) to get the amount of rotation I want on the roll joint. For the IK I am then multiplying it by the value of the switch which goes from 0-1. That way if I'm in FK mode it doesn't get any influence. For the FK you do the same thing but subtract 1 and take the absolute value to get 0-1 but reversed from the attribute.

So:
IK MODE
x = ((30 * 0.3) * 1) + ((60 * 0.3) * abs(1 - 1))
x = (9 * 1) + (18 * abs(0))
x = 9 + 0
x = 9

FK MODE
x = ((30 * 0.3) * 0) + ((60 * 0.3) * abs(0 - 1))
x = (9 * 0) + (18 * abs(-1))
x = 0 + 18
x = 18

Not really MEL but still a handy trick.

Wednesday, June 2, 2010

Delete unused materials Only

This spawned off of this thread about deteremining if a material is in use or not.

"Delete Unused Nodes" in the hypershade is great, but what if you don't want to get rid of all those textures and only materials?

for ($c in `ls -materials`) {
string $outputs[];
string $SG[] = {};
$outputs = stringArrayRemoveDuplicates(`listConnections -destination 1 $c`);
for ($c2 in $outputs) {
string $type = nodeType($c2);
if ($type == "shadingEngine") $SG[size($SG)] = $c2;
}
if (size(`listConnections -source 1 ($SG[size($SG)-1] + ".dagSetMembers")`) == 0) delete $c;
}

Tuesday, June 1, 2010

MEL Scope

An excellent post was made about MEL scripting and scope of variables, check it out: HERE

Monday, May 24, 2010

Swap object names

Okay so I didn't make any handy scripts at work today so I'm sharing one that I have on my custom marking menu. It just simply swaps the name of two objects. Handy if you need to remake a joint chain and don't want to rename them all.

string $s[] = `ls -sl`;
if ($s[0] == "") error "Make a selection!";
else {
string $num1 = $s[0];
string $num2 = $s[1];
rename $num1 ($num1 + "TEMP");
rename $num2 $num1;
rename ($num1 + "TEMP") $num2;
}