Stretchy Spine

This is probably the most sought out theory for any advanced rig. The stretchy concept is popularly used for spine and limbs of a bi-ped character and sometimes in rigging machines. This script is generally aimed at creating stretchy spine and can also be used for machines which are similar in joint creation as a spine.
Stretchy spines are commonly created by using expressions but I've used render nodes instead as it works much faster than expressions. Well, expressions or render nodes.... for a end user i don't think it makes much difference.

Mel Window:


Script:

global proc rk_stretchySpine()
{

if(`window -exists rk_stretchySpineUI`)
deleteUI rk_stretchySpineUI;

window -title " Stretchy Spine " -maximizeButton 0 rk_stretchySpineUI;
menuBarLayout myMenubar;
menu -label "\nHelp";
menu -label "\nEdit" myResetmenu ;
menuItem -label "Reset" -command rk_stretchySpine myMenuitem;
formLayout myformLayout;

//-------------------------------------------------------------------------------
text -label " Joints " jointsText;
textScrollList -height 111 my_jointsTSL;


text -label " Spline Curve " splineCurveText;
textScrollList -height 25 my_SplineCurveTSL;

//--------------------------------------------

button -label " Attach Spline Curve " -command splineCurveProcedure splineCurve_button;

//-----------------------------------------------


text -label " Switch Control ( Optional ) " switchControlText;
textScrollList -height 25 switchControlTSL;

//-----------------------------------------------

button -label " Attach Switch Control " -command switchControlProcedure switchControl_button;

button -label " Attach Joints " -command joints_buttonProcedure joints_button;

//------------------------------------------------

textFieldGrp -label "Name of the Character {Optional) " -text "Character" -adjustableColumn2 1 -adjustableColumn 1 mySpinetextFieldGrp;

button -label " GO " -command go_procedure final_button;


text -label " Rakesh K Karkera " myName;
text -label " tylerdurtan@gmail.com " myid;
//-------------------------------------------------------------------------------
formLayout -edit

-attachForm jointsText left 5
-attachForm jointsText top 4
-attachPosition jointsText right 0 45

-attachForm my_jointsTSL left 5
-attachControl my_jointsTSL top 2 jointsText
-attachPosition my_jointsTSL right 0 45

-attachControl splineCurveText left 15 jointsText
-attachForm splineCurveText top 4
-attachForm splineCurveText right 8


-attachControl my_SplineCurveTSL left 15 my_jointsTSL
-attachControl my_SplineCurveTSL top 2 splineCurveText
-attachForm my_SplineCurveTSL right 8


-attachControl splineCurve_button left 25 jointsText
-attachControl splineCurve_button top 6 my_SplineCurveTSL
-attachForm splineCurve_button right 20

-attachControl switchControlText left 15 jointsText
-attachControl switchControlText top 15 splineCurve_button
-attachForm switchControlText right 8

-attachControl switchControlTSL left 15 jointsText
-attachControl switchControlTSL top 2 switchControlText
-attachForm switchControlTSL right 8


-attachControl switchControl_button left 25 jointsText
-attachControl switchControl_button top 6 switchControlTSL
-attachForm switchControl_button right 20

-attachForm joints_button left 15
-attachControl joints_button top 7 my_jointsTSL
-attachControl joints_button right 35 switchControl_button


-attachForm mySpinetextFieldGrp left 0
-attachControl mySpinetextFieldGrp top 15 joints_button
-attachForm mySpinetextFieldGrp right 5

-attachForm final_button left 40
-attachControl final_button top 15 mySpinetextFieldGrp
-attachForm final_button right 40


-attachPosition myName left 0 38
-attachControl myName top 18 final_button
-attachPosition myName right 0 95


-attachPosition myid left 1 36
-attachControl myid top 0 myName
-attachPosition myid right 0 95

//---------------------------------------------------------------------
myformLayout;

showWindow rk_stretchySpineUI;
}

global proc joints_buttonProcedure()

{
textScrollList -edit -removeAll my_jointsTSL;
string $selectedJoints[]= `ls -sl ` ;

for( $eachJoint in $selectedJoints)
{
textScrollList -edit -append $eachJoint my_jointsTSL;
}
}
global proc splineCurveProcedure()

{
textScrollList -edit -removeAll my_SplineCurveTSL;
string $selectedCurve[]= `ls -sl ` ;
int $size_selectedCurve= size($selectedCurve);

if( $size_selectedCurve>= 2)
{

confirmDialog -title "Alert" -message "U have selected more than 1 object"
-button "Redo" ;
}

else
{
for( $eachCurve in $selectedCurve)


{
textScrollList -edit -append $eachCurve my_SplineCurveTSL;
}

}
}
global proc switchControlProcedure()

{
textScrollList -edit -removeAll switchControlTSL;
string $selectedSwitch[]= `ls -sl ` ;
int $size_selectedSwitch= size($selectedSwitch);


if( $size_selectedSwitch>= 2)
{

confirmDialog -title "Alert" -message "U have selected more than 1 object"
-button "Redo" ;

}

else
{
for( $eachSwitch in $selectedSwitch)


{
textScrollList -edit -append $eachSwitch switchControlTSL;

select -r $eachSwitch;

addAttr -longName Stretchy_Spine_Switch
-defaultValue 0.0
-minValue 0
-maxValue 1
-keyable 1;

}

}

}
global proc go_procedure()
{
string $switch_TSL[] = ` textScrollList -q -allItems switchControlTSL`;
int $switch_size= size($switch_TSL);

if($switch_size <= 0)
{

string $text_charName = `textFieldGrp -q -text mySpinetextFieldGrp`;

string $char_Name = ($text_charName+"_Spine");

//----------------------------------------------------------
// puts the curve in the list
string $curve_inTSL[] = `textScrollList -q -allItems my_SplineCurveTSL`;
//select the curve
select -r $curve_inTSL;
// the arclength value sent to the float variable
float $arclength =` arclen $curve_inTSL `;
// run an arc length and store the name of the node in the string
string $curve_arcLength= `arclen -ch 1`;

//select the distane shape of the node( not sure i need it)
select -r $curve_arcLength ;

//-----------------------------------------------------------

string $stretchy_Mul_Div = `shadingNode -asUtility -name ($char_Name+"_mul_divide") multiplyDivide`;

//select -r $stretchy_Mul_Div ;

setAttr ($stretchy_Mul_Div+".operation") 2;

connectAttr -force ($curve_arcLength+".arcLength") ($stretchy_Mul_Div+".input1X");

setAttr ($stretchy_Mul_Div+".input2X") $arclength;

//----------------------------------------------------------

string $stretchy_condition = `shadingNode -asUtility -name ($char_Name+"_cndNode") condition`;
//select -r condition1 ;
setAttr ($stretchy_condition+".operation") 3;

connectAttr -force ($curve_arcLength+".arcLength") ($stretchy_condition+".firstTerm");

setAttr ($stretchy_condition+".secondTerm") $arclength;

connectAttr -force ($stretchy_Mul_Div+".outputX") ($stretchy_condition+".colorIfTrueR");

//----------------------------------------------------------

string $joints_in_TSL[] = ` textScrollList -q -allItems my_jointsTSL`;

for( $eachObject in $joints_in_TSL)
{
connectAttr -force ($stretchy_condition+".outColorR") ($eachObject+".scaleX");
}

}

else

{

string $text_charName = `textFieldGrp -q -text mySpinetextFieldGrp`;

string $char_Name = ($text_charName+"_Spine");

//----------------------------------------------------------
// puts the curve in the list
string $curve_inTSL[] = `textScrollList -q -allItems my_SplineCurveTSL`;
//select the curve
select -r $curve_inTSL;
// the arclength value sent to the float variable
float $arclength =` arclen $curve_inTSL `;
// run an arc length and store the name of the node in the string
string $curve_arcLength= `arclen -ch 1`;

//select the distane shape of the node( not sure i need it)
select -r $curve_arcLength ;

//-----------------------------------------------------------
string $stretchy_Mul_Div = `shadingNode -asUtility -name ($char_Name+"_mul_divide") multiplyDivide`;

//select -r $stretchy_Mul_Div ;

setAttr ($stretchy_Mul_Div+".operation") 2;

connectAttr -force ($curve_arcLength+".arcLength") ($stretchy_Mul_Div+".input1X");

setAttr ($stretchy_Mul_Div+".input2X") $arclength;

//----------------------------------------------------------

string $stretchy_condition = `shadingNode -asUtility -name ($char_Name+"_cndNode") condition`;
//select -r condition1 ;
setAttr ($stretchy_condition+".operation") 3;

connectAttr -force ($curve_arcLength+".arcLength") ($stretchy_condition+".firstTerm");

setAttr ($stretchy_condition+".secondTerm") $arclength;

connectAttr -force ($stretchy_Mul_Div+".outputX") ($stretchy_condition+".colorIfTrueR");

//----------------------------------------------------------

string $switch_condition = `shadingNode -asUtility -name ($char_Name+"_switch_cndNode") condition`;

//setAttr ($switch_condition+".operation") 3;

connectAttr -force ($switch_TSL[0]+".Stretchy_Spine_Switch") ($switch_condition+".firstTerm");

setAttr ($switch_condition+".secondTerm") 1;

connectAttr -force ($stretchy_condition+".outColorR") ($switch_condition+".colorIfTrueR");

string $joints_in_TSL[] = ` textScrollList -q -allItems my_jointsTSL`;

for( $eachObject in $joints_in_TSL)
{
connectAttr -force ($switch_condition+".outColorR") ($eachObject+".scaleX");
}
}
}

How To Use:

No comments:

Post a Comment