PDA

View Full Version : Useful Orient


phix314
11-15-2008, 07:45 PM
I think I'm digging myself deeper here.

What I need is this script to:

1) Create the constraint
2) Suck out the values of rotation that ensue
3) Delete the original constraint and reset the nurbs control object
4) Create a cluster to rotate the CVs so it's level (as drawn)
5) Group the cluster and Curve so it can be used as an orient constraint on the joint

Like I said, I think I'm digging myself deeper than I'd like. I just want to have a nurbs curve be useable as an orient constraint without the rotation values going all wacko.

If anything, this is a good exercise on coding in MEL, regardless of if I'm way off track.

BTW, this code works as is, just creates the group and the center pivot is off where it should.


global proc orient_Constrain(){

string $selection[] = `ls -sl`;
string $joint = $selection[0];
string $control = $selection[1];

//assign a random suffix to the orient name
int $random = `rand 0 1000`;

string $orientName = $joint + "_" + $control + "CUSTOMORIENT" + $random;

//print $orientName;
orientConstraint -offset 0 0 0 -weight 1 -name $orientName;

float $rotX[] = `getAttr $control.rotateX`;
float $rotY[] = `getAttr $control.rotateY`;
float $rotZ[] = `getAttr $control.rotateZ`;

/* DEBUG ROTATION */
//print ("Rotation X: " + $rotX[0] + " " + $rotX[1] + "\n");
//print ("Rotation Y: " + $rotY[0] + " " + $rotY[1] + "\n");
//print ("Rotation Z: " + $rotZ[0] + " " + $rotZ[1] + "\n");

vector $rotValues = <<$rotX[0], $rotY[0], $rotZ[0]>>;

delete $orientName;

setAttr ($control+".rotateX") ($rotValues.x);
setAttr ($control+".rotateY") ($rotValues.y);
setAttr ($control+".rotateZ") ($rotValues.z);

select -r $control;
makeIdentity -a true -t 1 -r 1 -s 1;


//code from http://ewertb.soundlinker.com/mel/mel.058.htm
string $curve = $control;

int $numSpans = `getAttr ( $curve + ".spans" )`;
int $degree = `getAttr ( $curve + ".degree" )`;
int $form = `getAttr ( $curve + ".form" )`;

int $numCVs = $numSpans + $degree;

// Adjust for periodic curve:
if ( $form == 2 ){
$numCVs -= $degree;
}

//print $numCVs;
//create the cluster
select -r $control.cv[0:$numCVs];

string $clusterName = "controlCurvesCluster" + $random;

cluster -n $clusterName -envelope 1;
string $fullClusterName = $clusterName + "Handle";

setAttr ($clusterName + "Handle.rotateX") ($rotValues.x);
setAttr ($clusterName + "Handle.rotateY") ($rotValues.y);
setAttr ($clusterName + "Handle.rotateZ") ($rotValues.z);

string $groupName = ($control + "_OrientGroup");

group -n $groupName $fullClusterName $control;

float $jointCenterX = `getAttr $joint.translateX`;
float $jointCenterY = `getAttr $joint.translateY`;
float $jointCenterZ = `getAttr $joint.translateZ`;

xform -ws -cp $groupName;
/*
string $clusterDeleteName = $clusterName + "Handle";
delete $clusterDeleteName;

string $finalOrientName = $control + "_" + $joint + "orientConstraint";

orientConstraint -name $finalOrientName -w 1 $control $joint;
*/
}
//orient_Constrain();

phix314
11-22-2008, 08:26 PM
Hm... well, close.




global proc orient_Constrain(){

string $selection[] = `ls -sl`;
string $joint = $selection[0];
string $control = $selection[1];

//assign a random suffix to the orient name
int $random = `rand 0 1000`;

string $orientName = $joint + "_" + $control + "CUSTOMORIENT" + $random;

//print $orientName;
orientConstraint -offset 0 0 0 -weight 1 -name $orientName;

float $rotX[] = `getAttr $control.rotateX`;
float $rotY[] = `getAttr $control.rotateY`;
float $rotZ[] = `getAttr $control.rotateZ`;

/* DEBUG ROTATION */
print ("Rotation X: " + $rotX[0] + " " + $rotX[1] + "\n");
print ("Rotation Y: " + $rotY[0] + " " + $rotY[1] + "\n");
print ("Rotation Z: " + $rotZ[0] + " " + $rotZ[1] + "\n");

//for whatever reason, maya returns 2 values
//for each single rotation value... the 1st
//one is what we're after
vector $rotValues = <<$rotX[0], $rotY[0], $rotZ[0]>>;

//delete the orient originally created
delete $orientName;

//makeIdentity -apply true -t 1 -r 1 -s 1 $control;

//set the rotation values for the control object
setAttr ($control+".rotateX") ($rotValues.x);
setAttr ($control+".rotateY") ($rotValues.y);
setAttr ($control+".rotateZ") ($rotValues.z);


/***** GRAB THE CONTROL CURVE CVs *****/
//code from http://ewertb.soundlinker.com/mel/mel.058.htm
string $curve = $control;

int $numSpans = `getAttr ( $curve + ".spans" )`;
int $degree = `getAttr ( $curve + ".degree" )`;
int $form = `getAttr ( $curve + ".form" )`;

int $numCVs = $numSpans + $degree;

// Adjust for periodic curve:
if ( $form == 2 ){
$numCVs -= $degree;
}

//print $numCVs;
//create the cluster
select -r $control.cv[0:$numCVs];

/***** END CONTROL CURVE CVs *****/
//

//
/***** CREATE CLUSTER FOR THE CONTROL CURVE *****/

string $clusterName = "controlCurvesCluster" + $random;

cluster -n $clusterName -envelope 1;
string $fullClusterName = $clusterName + "Handle";

//set the cluster rotation to counteract the orient
setAttr ($clusterName + "Handle.rotateX") ($rotValues.x);
setAttr ($clusterName + "Handle.rotateY") ($rotValues.y);
setAttr ($clusterName + "Handle.rotateZ") ($rotValues.z);

/***** END CLUSTER FOR THE CONTROL CURVE *****/


select -r $control;
delete -ch;

//float $jointCenterX = `getAttr $joint.translateX`;
//float $jointCenterY = `getAttr $joint.translateY`;
//float $jointCenterZ = `getAttr $joint.translateZ`;


string $finalOrientName = $control + "_" + $joint + "orientConstraint";

orientConstraint -name $finalOrientName -w 1 $control $joint;

}
orient_Constrain();




To reiterate, I'd just like to keep the orient control (curve) the same as drawn, with neither the joint or the control moving on creation of the orient.

RyanT
11-24-2008, 01:32 AM
Now if I understand it right you just want to make the joint rotate with the control with neither moving. This should only take one line of code:

//this will orient constrain one node to another with maintain offset on
//select the curve control then the joint, then execute this:
orientConstraint -mo -weight 1;

If you want a cluster also you could do that but your code deletes the cluster so I assume you dont want that. If you want the curves rotation to be zeroed out without losing its current orientation, you could parent the curve to a null but the null needs to be the exact same rotation as the control before parenting to it.

global proc zeroCtrl_and_orientConst()
{

string $selection[] = `ls -sl`;
string $control = $selection[0];
string $joint = $selection[1];


string $null = `group -em`;

delete`parentConstraint $control $null`;
parent $control $null;

orientConstraint -mo -weight 1 $control $joint;
}

//select the control first then the joint then run this:
zeroCtrl_and_orientConst()

Note that in general when using constrains it is most natural by maya standards to select the node that will drive then the node that will be driven by that constrain. You flipped this in your code which might confuse someone that is used to regular maya conventions.

Here is your code with me commenting on it, so you might see some issues with it.

global proc orient_Constrain(){

string $selection[] = `ls -sl`;
string $joint = $selection[0];
string $control = $selection[1];

// You dont need to do this, see alternate version below
//assign a random suffix to the orient name
//int $random = `rand 0 1000`;

//string $orientName = $joint + "_" + $control + "CUSTOMORIENT" + $random;

//If you create an orient constraint with maintain offset off, it will cause the curve to move
//string $orientName[] = orientConstraint -offset 0 0 0 -weight 1 -name $orientName;

// This is incorrect, this will not execute
// you are receiving one float value not a array
//float $rotX[] = `getAttr $control.rotateX`;
//float $rotY[] = `getAttr $control.rotateY`;
//float $rotZ[] = `getAttr $control.rotateZ`;

/* DEBUG ROTATION */
//print ("Rotation X: " + $rotX[0] + " " + $rotX[1] + "\n");
//print ("Rotation Y: " + $rotY[0] + " " + $rotY[1] + "\n");
//print ("Rotation Z: " + $rotZ[0] + " " + $rotZ[1] + "\n");

//no maya does not return 2 values
//and regardless you dont need to do this
//vector $rotValues = <<$rotX[0], $rotY[0], $rotZ[0]>>;

//delete the orient originally created
//delete $orientName;

//makeIdentity -apply true -t 1 -r 1 -s 1 $control;

//Doing this would have just undone everything you did above
//you my as well just not do anything at this point
//setAttr ($control+".rotateX") ($rotValues.x);
//setAttr ($control+".rotateY") ($rotValues.y);
//setAttr ($control+".rotateZ") ($rotValues.z);


/***** GRAB THE CONTROL CURVE CVs *****/

//You dont need to do this see example below

/*
//code from http://ewertb.soundlinker.com/mel/mel.058.htm
string $curve = $control;

int $numSpans = `getAttr ( $curve + ".spans" )`;
int $degree = `getAttr ( $curve + ".degree" )`;
int $form = `getAttr ( $curve + ".form" )`;

int $numCVs = $numSpans + $degree;

// Adjust for periodic curve:
if ( $form == 2 ){
$numCVs -= $degree;
}
*/

//select all the cvs on the curve control
select $control;
selectCurveCV("all");

/***** END CONTROL CURVE CVs *****/
//

//
/***** CREATE CLUSTER FOR THE CONTROL CURVE *****/

//string $clusterName = "controlCurvesCluster" + $random;

string $getCluster[] = `cluster -envelope 1`;
string $clusterName = $getCluster[0];
string $clusterNameHandle = $getCluster[1];

//this wont do anything if you end up deleting the cluster anyway with delete -ch
//setAttr ($clusterName + "Handle.rotateX") ($rotValues.x);
//setAttr ($clusterName + "Handle.rotateY") ($rotValues.y);
//setAttr ($clusterName + "Handle.rotateZ") ($rotValues.z);

/***** END CLUSTER FOR THE CONTROL CURVE *****/


//this deletes the cluster which means doing all the above code was meaningless
select -r $control;
delete -ch;

//float $jointCenterX = `getAttr $joint.translateX`;
//float $jointCenterY = `getAttr $joint.translateY`;
//float $jointCenterZ = `getAttr $joint.translateZ`;


string $finalOrientName = $control + "_" + $joint + "orientConstraint";

orientConstraint -name $finalOrientName -w 1 $control $joint;

}
orient_Constrain();

Hope that helps.

CGTalk Moderation
11-24-2008, 01:32 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.