rf_jointLocCreateUI.mel
/*Author:Rick Fronek (aka melvin3D)
Author : rickfronek@gmail.com
Description: Simple utility that creates joints or locators in given selection
Only works on transform nodes. No underworld or component selection will
work in the current version. Update soon.
warning:
that code may destroy any of your project. Don't be blaming me. :)
notes:
I tested that code should work any of maya version 2008 and after.
Did not check for earlier.
usage : Select a any transform node and choose your option and run the tool.
Next version will work with component selection to create and align
the creation object type accordingly.
*/
//====================================================
//====================================================
//====================================================
proc string[] rf_createLocatorJoint(int $type , string $g_locGrpName , float $locSize , int $orientON_OFF)
{
string $ctrl = "";
string $con[] = `ls -sl -type "transform"`;
string $matchLoc = "";
// store new joints
string $newJoints[0];
// store the loc
string $jointName = "";
// store the created locator ---> $locTmp[0] = transform node
// locTmp[1] = shape node
string $locTmp[0];
// these are locs that match pos and ori of all face joint.
// return these to work on them later.
string $returnAllLocs[0];
string $returnAllJoints[0];
string $Shape[0]; // shape of teh locator to adjust its scale
string $joint = ""; // store the creation of the joint
// number of selected joints
int $iSizeLocs = `size($con)`;
// get the color from the locar slider UI
int $colorQ = `colorIndexSliderGrp -q -v COLORSlider`;
// check if there is any selection
if(`size($con)` == 0)
{
error ("No selection made !");
}
// check if the node "g_locGrpName" exists
if(`objExists $g_locGrpName` == 0)
{
group -em -w -name $g_locGrpName;
select -cl;
}
// loop start
$i = 0;
for($ctrl in $con)
{
// get/print info about the listed nodes.
// coordinates , names , how many in the selection
// =============================
$i += 1;
print($ctrl + " " + "\[" + $i + "\]\n");
// get the world coordinates
float $pos[] = `xform -query -translation $ctrl`;
print("X: " + $pos[0] + " , " + "Y : " + $pos[1] + " , " + "Z : " + $pos[2] + "\n");
// =============================
switch($type)
{
case 1: // when create type is joint , orientation (WORLD - LOCAL)
// create a locator at 0 0 0
$locTmp = `spaceLocator -a -p 0 0 0 -n ($ctrl + "_mc")`;
// store the result of the loc into a single string .
$matchLoc = $locTmp[0];
// get the shape of the locator first
$Shape = `listRelatives -s $matchLoc`;
// if you do create locator confirm the shape node and change the color.
if(`size($Shape)` > 0)
{
// change the color on creation so i can easily view it.
setAttr ($matchLoc + ".overrideEnabled") 1;
setAttr ($matchLoc + ".overrideColor") ($colorQ - 1);
}
if ($orientON_OFF) // if orient is True !
{
// create a group that orients based on the selected object
string $TmpGrp = `group -em -w -n ($matchLoc + "_ori")`;
// point and orient snap it to the jaw joint
// using pont/orient instead of xform cmd . I dont care if it is in
// local or world coordinate system. I just want to match the
// position and orientation.
string $TmpPointConst[] = `pointConstraint -w 1 $ctrl $TmpGrp`;
string $TmpOrientConst[] = `orientConstraint -w 1 $ctrl $TmpGrp`;
// delete the constraints
delete $TmpPointConst;
delete $TmpOrientConst;
// parent the loc under the jawGrp
parent $matchLoc $TmpGrp;
// zero out the channels t match the jaw transformations
setAttr ($matchLoc + ".translate") 0 0 0;
setAttr ($matchLoc + ".rotate") 0 0 0;
// Eventually parent the jaw group under the $g_locGrpName node whatever it is.
if(`objExists $g_locGrpName`)
{
rename $g_locGrpName ($g_locGrpName + "_tmp");
parent $TmpGrp ($g_locGrpName + "_tmp");
rename ($g_locGrpName + "_tmp") $g_locGrpName;
}
// adjust the size of the jaw locator
setAttr ($Shape[0] + ".localScale") $locSize $locSize $locSize;
// add the jaw loc to my return list as well
$returnAllLocs[size($returnAllLocs)] = $matchLoc;
}
if(!$orientON_OFF)
{
// parent it to the controller
parent $matchLoc $ctrl;
// zero out the translations and rotations to match the transformation of the parent
setAttr ($matchLoc + ".tx") 0;
setAttr ($matchLoc + ".ty") 0;
setAttr ($matchLoc + ".tz") 0;
setAttr ($matchLoc + ".rx") 0;
setAttr ($matchLoc + ".rx") 0;
setAttr ($matchLoc + ".rx") 0;
// once you match the pos and ori unparent it again.
parent -w $matchLoc;
// re-parent it back into matchingLocGrp node.
if(`objExists $g_locGrpName`)
{
rename $g_locGrpName ($g_locGrpName + "_tmp");
parent $matchLoc ($g_locGrpName + "_tmp");
rename ($g_locGrpName + "_tmp") $g_locGrpName;
}
// freeze transformations on rotations
// keep the trans available to match joints based on XYZ
makeIdentity -a true -r true $matchLoc;
// change the color on creation so i can easily view it.
setAttr ($Shape[0] + ".overrideEnabled") 1;
setAttr ($Shape[0] + ".overrideColor") ($colorQ - 1);
// adjust the scale of the locator based on the argument
setAttr ($Shape[0] + ".localScale") $locSize $locSize $locSize;
// store each elem into my listso we can call it.
$returnAllLocs[size($returnAllLocs)] = $matchLoc;
}
// once youre done with the loop clean the arrays .
clear $newJoints;
clear $locTmp;
clear $returnAllLocs;
clear $returnAllJoints;
clear $Shape;
break;
case 2: // when create type is joint , orientation (WORLD)
//print($ctrl + "\n");
// the creation of joints ..you have to have only one node
// selected before you create a joint. Loop still apply here.
select -cl;
// get the location in XYZ of the selected source node.
float $pos[] = `xform -q -t -ws $ctrl`;
// create joint
$joint = `joint -n ($ctrl + "_jnt")`;
// match the position of the source node
joint -e -a -p $pos[0] $pos[1] $pos[2] $joint;
// change the color of the joint based on the colorIndex
setAttr ($joint + ".overrideEnabled") 1;
setAttr ($joint + ".overrideColor") ($colorQ - 1);
// parent the joint under the master grp
parent $joint $g_locGrpName;
select -cl;
// store each elem into my listso we can call it.
$returnAllJoints[size($returnAllJoints)] = $joint;
// afain clean if you deal with joints .
clear $newJoints;
clear $locTmp;
clear $returnAllLocs;
clear $returnAllJoints;
clear $Shape;
break;
}
} // loop end
// select the master loc group when finished.
select -r $g_locGrpName;
// let me know we're done...
print "Done working .....";
print "\n";
//return the list of all locators created during the process
return $returnAllLocs;
} // end proc.
//====================================================
global proc rf_alingObjectLogic()
{
// Query the infor from the UI
// joints or locators
int $creationType = `radioButtonGrp -q -sl "RC_0"`;
// This is either default or custom parent group
int $parentName = `radioButtonGrp -q -select "RC_1"`;
// grab the name of the custom name IF specified
string $customName = `textField -q -tx "TxtField1"`;
// world or local
int $orientation = `radioButtonGrp -q -sl "RC_2"`;
string $node = "";
//================
// Execute the procedures based on settings the user chooses.
if($creationType == 1)
{
if(($parentName == 1) && ($orientation == 1))
{
string $returnLocators[] = rf_createLocatorJoint(1,"g_loc_Guiders", 1.0 , 0);
for($node in $returnLocators)
{
print ($node + "\n");
}
}
}
if($creationType == 2)
{
if(($parentName == 1) && ($orientation == 1))
{
string $returnJoints[] = rf_createLocatorJoint(2,"g_jointGrp", 1.0 , 0);
for($joint in $returnJoints)
{
print ($node + "\n");
}
}
}
//=================================================================
if($creationType == 1)
{
if (($parentName == 1) && ($orientation == 2))
{
string $returnLocators[] = rf_createLocatorJoint(1,"g_Loc_Guiders", 1.0 , 1);
for($node in $returnLocators)
{
print ($node + "\n");
}
}
}
/*
if($creationType == 2)
{
if (($parentName == 1) && ($orientation == 2))
{
string $returnJoints[] = rf_createLocatorJoint(2,"g_jointGrp", 1.0 , 0);
for($joint in $returnJoints)
{
print ($node + "\n");
}
}
}
*/
//=================================================================
if($creationType == 1)
{
if(($parentName == 2) && ($orientation == 1))
{
string $returnLocators[] = rf_createLocatorJoint(1,$customName, 1.0 , 0);
for($node in $returnLocators)
{
print ($node + "\n");
}
}
}
if($creationType == 2)
{
if(($parentName == 2) && ($orientation == 1))
{
string $returnJoints[] = rf_createLocatorJoint(2,$customName, 1.0 , 0);
for($joint in $returnJoints)
{
print ($node + "\n");
}
}
}
//=================================================================
if($creationType == 1)
{
if (($parentName == 2) && ($orientation == 2))
{
string $returnLocators[] = rf_createLocatorJoint(1,$customName, 1.0 , 1);
for($node in $returnLocators)
{
print ($node + "\n");
}
}
}
if($creationType == 2)
{
if (($parentName == 2) && ($orientation == 2))
{
string $returnJoints[] = rf_createLocatorJoint(2,"g_jointGrp", 1.0 , 0);
for($joint in $returnJoints)
{
print ($node + "\n");
}
}
}
} // end proc
// ===================================================
global proc rf_jointLocCreateToolUI()
{
if(`window -q -ex "locWindow"`) deleteUI -wnd "locWindow";
window -t "CreateObjectUI" -rtf true locWindow;
//====================================================
columnLayout - adj true;
//====================================================
//==========\\
// panel #1
//==========\\
frameLayout -lv false
-bs "etchedIn"
-cl false
-cll false;
rowColumnLayout -nc 3
-cw 1 130
-co 1 "left" 4
-cw 2 125
-cw 3 105
-rat 2 "both" 3; // aligns the row in nice line
// elem #1
text -l "Choose object type :" -fn "plainLabelFont" -ann "Choose what you want to create locators/joints";
// elem #2
radioButtonGrp -nrb 2
-la2 "Locators" "Joints"
-cw 1 65
-select 1
-cw 2 80
-on1 "radioButtonGrp -e -en2 true RC_2;"
-of1 "radioButtonGrp -e -en2 false RC_2; radioButtonGrp -e -select true RC_2;" RC_0;
setParent ..; setParent ..;
//==========\\
// panel #2
//==========\\
frameLayout -lv false
-bs "etchedIn"
-cl false
-cll false;
rowColumnLayout -nc 3
-cw 1 130
-co 1 "left" 4
-cw 2 125
-cw 3 105
-rat 2 "both" 3; // aligns the row in nice line
// elem #1
text -l "Choose name [Group] :" -fn "plainLabelFont" -ann "Name of the parent grp for all your locators";
// elem #2
radioButtonGrp -nrb 2
-la2 "Default" "Custom"
-cw 1 65
-select 1
-cw 2 80
-on1 "textField -e -ed false TxtField1; textField -e -text \"\" TxtField1;"
-of1 "textField -e -ed true TxtField1;" RC_1;
// elem #3
textField -h 22 -ed false TxtField1;
setParent ..; setParent ..;
//====================================================
//==========\\
// panel #3
//==========\\
frameLayout -lv false
-bs "etchedIn"
-cl false
-cll false;
rowColumnLayout -nc 2
-cw 1 130
-co 1 "left" 4
-cw 2 170
-rat 2 "both" 1; // // aligns the row in nice line
// elem #1
text -l "Choose Orientation :" -fn "plainLabelFont";
// elem #2
radioButtonGrp -nrb 2
-la2 "World" "Local"
-cw 1 65
-cw 2 60
-select 1
-ann "Choose how you align the locators" RC_2;
setParent ..; setParent ..; // all for the parent layout
//====================================================
//==========\\
// panel #4
//==========\\
frameLayout -lv false
-bs "etchedIn"
-cl false
-cll false;
colorIndexSliderGrp -l "Locator color :"
-cw 1 132
-columnAttach 1 "left" 4
-cw 2 70
-min 0
-max 20
-value 10 COLORSlider;
setParent ..; setParent ..; // all for the parent layout
//==========\\
// panel #5
//==========\\
frameLayout -lv false
-bs "etchedIn"
-cl false
-cll false;
columnLayout -adj true;
text -l "TD - [rickfronek@gmail.com]" -fn "smallBoldLabelFont";
setParent ..; setParent ..; // all for the parent layout
//====================================================
//==========\\
// Execute panel
//==========\\
frameLayout -lv false
-bs "etchedIn"
-cl false
-cll false
-h 30;
rowColumnLayout -nc 3
-cw 1 120
-cw 2 120
-cw 3 121;
button -l "Reset Tool" -h 26 -ann "Reset to default" -c "radioButtonGrp -e -sl true RC_0;radioButtonGrp -e -sl true RC_1; radioButtonGrp -e -sl 1 RC_2; textField -e -tx \" \" TxtField1; textField -e -ed false TxtField1; colorIndexSliderGrp -e -value 10 COLORSlider;";
button -l "Cancel / Close" -ann "Cancel Cmd" -c "deleteUI -wnd locWindow;";
button -l "RUN" -ann "Run script" -c "{rf_alingObjectLogic();}";
// edit the size
window -e -wh 373 186 locWindow;
// show window
showWindow locWindow;
} // end proc
rf_jointLocCreateToolUI();
/* NOTES :*/
Subscribe to:
Posts (Atom)