//////////////////////////////////////////////////////
//Rig IKFK
//////////////////////////
//Written by Kiel Figgins
//www.3dfiggins.com
//////////////////////////
//Small interface for matching the IK and FK in the KF Auto Rigs when referenced inside an animation file
//////////////////////////
//Version History
//////////////////////////
//2.02 (02-14-2013) - Added Dog Leg Support
//2.01 (02-01-2013) - Added Tentacle support
//1.06 (01-21-2013) - Fixed FK > IK naming error, added Foot matching, corrected pole matching
//1.05 (12-09-2011) - Conformed UI
//1.04 (09-20-2010) - fixed scalar naming issue
//1.03 (03-20-2010) - Determines which side the arm is
//1.02 (03-05-2010) - Added in matching for FK to IK with stretch
//1.01 (03-02-2010) - Added button annonotations
//1.00 (02-25-2010) - Orginal version
//////////////////////////

//////////////////////////
//Coming Soon
/*
-error check to verify that a name space exists (that the rig has been referenced)
-note if elbow is twisted more , backwards, or on more then one axis
*/

//Open on source
kfRigIKFK();


global proc kfRigIKFK()
{

if (`window -q -ex kfRigIKFKWin`)
    	{
    	showWindow kfRigIKFKWin ;
    	return ;
    	}

window -w 260 -h 210 -t "IKFK" -tlb true kfRigIKFKWin ;
formLayout mainSaveForm ;

//UI pieces
text -l "**Select IK Hand/Foot CTRL**" txIKFKInstr;
button -l "Match IK to FK" -w 150 -ann "Matches IK limb to FK Limb" -c ("matchIKtoFK();") btnRIik; 
button -l "Match FK to IK" -w 150 -ann "Matches FK limb to IK Limb" -c ("matchFKtoIK();") btnRIfk; 

text -l "**Select Tentacle Pinner**" txIKFKInstrTen;
button -l "Match IK to FK (tentacle)" -w 150 -ann "Matches IK limb to FK Limb" -c ("matchIKtoFKTen();") btnRIikTen; 
button -l "Match FK to IK (tentacle)" -w 150 -ann "Matches FK limb to IK Limb" -c ("matchFKtoIKTen();") btnRIfkTen; 



//UI FormLayout
formLayout -e

		-af txIKFKInstr "top" 4 
    	-an txIKFKInstr "bottom" 
    	-af txIKFKInstr "left" 5 
    	-an txIKFKInstr "right" 

        -ac btnRIik "top" 4 txIKFKInstr
    	-an btnRIik "bottom" 
    	-af btnRIik "left" 5 
    	-an btnRIik "right" 

        -ac btnRIfk "top" 4 btnRIik
    	-an btnRIfk "bottom" 
    	-af btnRIfk "left" 5 
    	-an btnRIfk "right" 

		-ac txIKFKInstrTen "top" 12 btnRIfk
    	-an txIKFKInstrTen "bottom" 
    	-af txIKFKInstrTen "left" 5 
    	-an txIKFKInstrTen "right" 

        -ac btnRIikTen "top" 4 txIKFKInstrTen
    	-an btnRIikTen "bottom" 
    	-af btnRIikTen "left" 5 
    	-an btnRIikTen "right" 

        -ac btnRIfkTen"top" 4 btnRIikTen
    	-an btnRIfkTen "bottom" 
    	-af btnRIfkTen "left" 5 
    	-an btnRIfkTen "right" 


    	mainSaveForm ;

    showWindow kfRigIKFKWin;
    
    //Resize the main window
    window -e -widthHeight 160 160 kfRigIKFKWin;

}//end of proc kfRigIKFK()







//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Match IK to FK
global proc matchIKtoFK()
{
	//select IK hand control
	string $userHand[] = `ls -sl`;

	///////////////////////////////////////////////////////////////////////////
	//Work
	if(`size($userHand)`)
	{
		///////////////////////////////////
		//Verify - hand control
		if(`match "_Hand" $userHand[0]` == "_Hand")
		{
			//Find out just the name space
			string $justName = `match "[^:]*$" $userHand[0]`;
			$nameSpace = `substituteAllString $userHand[0] (":" + $justName) ""`;

			//Define IK Control
			select $userHand;
			string $ikCtrl[] = `ls -sl`;

			//Determine Which Side
			string $justName = `match "[^:]*$" $userHand[0]`;
			string $sdd = `substituteAllString $userHand[0] ($nameSpace + ":CTRL_") ""`;
			string $sider = `substituteAllString $sdd "_Hand" ""`;

			//Define Pole Control
			select ($nameSpace + ":CTRL_" + $sider + "_ElbowPole");
			string $ikPole[] = `ls -sl`;

			//Define FK Controls
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Shoulder");
			string $fkShoulder[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Elbow");
			string $fkElbow[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Wrist");
			string $fkWrist[] = `ls -sl`;	

			//Define FK Wrist Position/Rotation, match IK ctrl to it
			float $fkwristPos[] = `xform -q -ws -rp $fkWrist[0]`;
			move -ws -rpr $fkwristPos[0] $fkwristPos[1] $fkwristPos[2] $ikCtrl;

			float $fkwristrot[]=`xform -q -ws -rotation $fkWrist[0]`;
			xform -ws -rotation $fkwristrot[0] $fkwristrot[1] $fkwristrot[2] $ikCtrl;

			float $posShoulder[] = `xform -q -ws -rp $fkShoulder`;
			float $posElbow[] = `xform -q -ws -rp $fkElbow`;
			float $posWrist[] = `xform -q -ws -rp $fkWrist`;

			//Pole Finder - Proper
			group -empty;
			string $hip[] = `ls -sl`;
			
			group -empty;
			string $kneeHip[] = `ls -sl`;
			
			group -empty;
			string $ankle[] = `ls -sl`;
			
			group -empty;
			string $ankleKnee[] = `ls -sl`;
			
			group -empty;
			string $poleFind[] = `ls -sl`;
			
			parent $kneeHip $hip;
			parent $ankleKnee $ankle;
			pointConstraint $kneeHip $ankleKnee $poleFind;
			
			parentConstraint $fkShoulder $hip;
			parentConstraint $fkElbow $ankle;
			
			string $delme[] = `pointConstraint $fkElbow $kneeHip`;
			delete $delme;
			
			setAttr ($kneeHip[0] + ".tx") ((`getAttr ($kneeHip[0] + ".tx")`) * 2);
			
			string $delme[] = `pointConstraint $fkWrist $ankleKnee`;
			delete $delme;
			
			setAttr ($ankleKnee[0] + ".tx") ((`getAttr ($ankleKnee[0] + ".tx")`) * -2);
			
			float $posVert[] = `xform -q -ws -rp $poleFind`;
			
			move -ws -rpr $posVert[0] $posVert[1] $posVert[2] $ikPole;
			
			delete $hip $ankle $poleFind;
			
			select $ikCtrl;

			print ("\nSUCCESS: IK matched to FK for " + $nameSpace + ".");
		}
		else
		{
			print ("\nERROR: Control not a *_Hand / *_Foot control.");
		}//end of verifying if _Hand is in name



		///////////////////////////////////
		//Verify - foot control
		if(`match "_Foot" $userHand[0]` == "_Foot" && `attributeExists "shinRoll" $userHand[0]` == 0)
		{
			//Find out just the name space
			string $justName = `match "[^:]*$" $userHand[0]`;
			$nameSpace = `substituteAllString $userHand[0] (":" + $justName) ""`;

			//Define IK Control
			select $userHand;
			string $ikCtrl[] = `ls -sl`;

			//Determine Which Side
			string $justName = `match "[^:]*$" $userHand[0]`;
			string $sdd = `substituteAllString $userHand[0] ($nameSpace + ":CTRL_") ""`;
			string $sider = `substituteAllString $sdd "_Foot" ""`;

			//Define Pole Control
			select ($nameSpace + ":CTRL_" + $sider + "_KneePole");
			string $ikPole[] = `ls -sl`;

			//Define FK Controls
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Hip");
			string $fkShoulder[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Knee");
			string $fkElbow[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Ankle");
			string $fkWrist[] = `ls -sl`;	
			
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Toe");
			string $fkToe[] = `ls -sl`;	

			//Define FK Wrist Position/Rotation, match IK ctrl to it
			float $fkwristPos[] = `xform -q -ws -rp $fkWrist[0]`;
			move -ws -rpr $fkwristPos[0] $fkwristPos[1] $fkwristPos[2] $ikCtrl;

			float $fkwristrot[]=`xform -q -ws -rotation $fkWrist[0]`;
			xform -ws -rotation $fkwristrot[0] $fkwristrot[1] $fkwristrot[2] $ikCtrl;

			float $posShoulder[] = `xform -q -ws -rp $fkShoulder`;
			float $posElbow[] = `xform -q -ws -rp $fkElbow`;
			float $posWrist[] = `xform -q -ws -rp $fkWrist`;
			float $posToe[] = `xform -q -ws -rp $fkToe`;

			//Pole Finder - Proper
			group -empty;
			string $hip[] = `ls -sl`;
			
			group -empty;
			string $kneeHip[] = `ls -sl`;
			
			
			group -empty;
			string $ankle[] = `ls -sl`;
			
			group -empty;
			string $ankleKnee[] = `ls -sl`;
			
			group -empty;
			string $poleFind[] = `ls -sl`;
			
			parent $kneeHip $hip;
			parent $ankleKnee $ankle;
			pointConstraint $kneeHip $ankleKnee $poleFind;
			
			parentConstraint $fkShoulder $hip;
			parentConstraint $fkElbow $ankle;
			
			string $delme[] = `pointConstraint $fkElbow $kneeHip`;
			delete $delme;
			
			setAttr ($kneeHip[0] + ".tx") ((`getAttr ($kneeHip[0] + ".tx")`) * 2);
			
			string $delme[] = `pointConstraint $fkWrist $ankleKnee`;
			delete $delme;
			
			setAttr ($ankleKnee[0] + ".tx") ((`getAttr ($ankleKnee[0] + ".tx")`) * -2);
			
			float $posVert[] = `xform -q -ws -rp $poleFind`;
			
			move -ws -rpr $posVert[0] $posVert[1] $posVert[2] $ikPole;
			
			delete $hip $ankle $poleFind;
			
			//Set All foot channels back to 0
			setAttr ($ikCtrl[0] + ".toeX") (`getAttr ($fkToe[0] + ".rx")`);
			setAttr ($ikCtrl[0] + ".toeY") (`getAttr ($fkToe[0] + ".ry")`);
			setAttr ($ikCtrl[0] + ".toeZ") (`getAttr ($fkToe[0] + ".rz")`);

			setAttr ($ikCtrl[0] + ".toeRoll") 0;

			setAttr ($ikCtrl[0] + ".ankleX") 0;
			setAttr ($ikCtrl[0] + ".ankleY") 0;
			setAttr ($ikCtrl[0] + ".ankleZ") 0;

			setAttr ($ikCtrl[0] + ".ballX") 0;
			setAttr ($ikCtrl[0] + ".ballY") 0;
			setAttr ($ikCtrl[0] + ".ballZ") 0;

			setAttr ($ikCtrl[0] + ".toeTipX") 0;
			setAttr ($ikCtrl[0] + ".toeTipY") 0;
			setAttr ($ikCtrl[0] + ".toeTipZ") 0;

			setAttr ($ikCtrl[0] + ".outerX") 0;
			setAttr ($ikCtrl[0] + ".outerY") 0;
			setAttr ($ikCtrl[0] + ".outerZ") 0;

			setAttr ($ikCtrl[0] + ".innerX") 0;
			setAttr ($ikCtrl[0] + ".innerY") 0;
			setAttr ($ikCtrl[0] + ".innerZ") 0;

			setAttr ($ikCtrl[0] + ".heelX") 0;
			setAttr ($ikCtrl[0] + ".heelY") 0;
			setAttr ($ikCtrl[0] + ".heelZ") 0;

			setAttr ($ikCtrl[0] + ".heelTipX") 0;
			setAttr ($ikCtrl[0] + ".heelTipY") 0;
			setAttr ($ikCtrl[0] + ".heelTipZ") 0;
			
			//Force Fix on R side
			if(
			`match "_R_" $userHand[0]` == "_R_" && 
			`match "_Foot" $userHand[0]` == "_Foot")
			{
				select $ikCtrl;
				rotate -r -os -180 0 0 ;
			}//end of r foot
			
			select $ikCtrl;

			print ("\nSUCCESS:BB IK matched to FK for " + $nameSpace + ".");
		}
		else
		{
			print ("\nERROR: Control not a *_Hand / *_Foot control.");
		}//end of verifying if _Hand is in name





		///////////////////////////////////
		//Verify - foot control
		if(`match "_Foot" $userHand[0]` == "_Foot" && `attributeExists "shinRoll" $userHand[0]` == 1)
		{
			//Find out just the name space
			string $justName = `match "[^:]*$" $userHand[0]`;
			$nameSpace = `substituteAllString $userHand[0] (":" + $justName) ""`;

			//Define IK Control
			select $userHand;
			string $ikCtrl[] = `ls -sl`;

			//Determine Which Side
			string $justName = `match "[^:]*$" $userHand[0]`;
			string $sdd = `substituteAllString $userHand[0] ($nameSpace + ":CTRL_") ""`;
			string $sider = `substituteAllString $sdd "_Foot" ""`;

			//Define IK / Pole Controls
			select ($nameSpace + ":CTRL_" + $sider + "_KneePole");
			string $ikPole[] = `ls -sl`;
			
			select ($nameSpace + ":CTRL_" + $sider + "_Ind_KneePole");
			string $ikIndPole[] = `ls -sl`;
		    
		    //Damn guess/check because Logan's Bee was built wrong
			if(`objExists ($nameSpace + ":CTRL_" + $sider + "_Iso_Trans_IK")` == 1)
			{select ($nameSpace + ":CTRL_" + $sider + "_Iso_Trans_IK");}
			
			if(`objExists ($nameSpace + ":CTRL_" + $sider + "Iso_IK")` == 1)
			{select ($nameSpace + ":CTRL_" + $sider + "Iso_IK");}
			
			string $ikIndCtrl[] = `ls -sl`;

			//Define FK Controls
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Hip");
			string $fkShoulder[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Knee");
			string $fkElbow[] = `ls -sl`;
			
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Shin");
			string $fkShin[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Ankle");
			string $fkWrist[] = `ls -sl`;	
			
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Toe");
			string $fkToe[] = `ls -sl`;
			
			//Set Ind Trans Control
			float $posVert[] = `xform -q -ws -rp $fkShin`;
			
			move -ws -rpr $posVert[0] $posVert[1] $posVert[2] $ikPole;
			

			//Define FK Wrist Position/Rotation, match IK ctrl to it
			
			//Apparently R side IKFK ankle is messed up
			if(`objExists ($nameSpace + ":rig_" + $sider + "_Foot_IKFK_Match")` == 1)
			{
				float $fkwristPos[] = `xform -q -ws -rp ($nameSpace + ":rig_" + $sider + "_Foot_IKFK_Match")`;
				move -ws -rpr $fkwristPos[0] $fkwristPos[1] $fkwristPos[2] $ikCtrl;
	
				float $fkwristrot[]=`xform -q -ws -rotation ($nameSpace + ":rig_" + $sider + "_Foot_IKFK_Match")`;
				xform -ws -rotation $fkwristrot[0] $fkwristrot[1] $fkwristrot[2] $ikCtrl;
			}
			else
			{
				float $fkwristPos[] = `xform -q -ws -rp $fkWrist[0]`;
				move -ws -rpr $fkwristPos[0] $fkwristPos[1] $fkwristPos[2] $ikCtrl;
	
				float $fkwristrot[]=`xform -q -ws -rotation $fkWrist[0]`;
				xform -ws -rotation $fkwristrot[0] $fkwristrot[1] $fkwristrot[2] $ikCtrl;
			}

			float $posShoulder[] = `xform -q -ws -rp $fkShoulder`;
			float $posElbow[] = `xform -q -ws -rp $fkElbow`;
			float $posShin[] = `xform -q -ws -rp $fkShin`;
			float $posWrist[] = `xform -q -ws -rp $fkWrist`;
			float $posToe[] = `xform -q -ws -rp $fkToe`;

			//Pole Finder (Main) - Proper
			group -empty;
			string $hipm[] = `ls -sl`;
			
			group -empty;
			string $kneeHipm[] = `ls -sl`;
			
			group -empty;
			string $anklem[] = `ls -sl`;
			
			group -empty;
			string $ankleKneem[] = `ls -sl`;
			
			group -empty;
			string $poleFindm[] = `ls -sl`;
			
			parent $kneeHipm $hipm;
			parent $ankleKneem $anklem;
			pointConstraint $kneeHipm $ankleKneem $poleFindm;
			
			parentConstraint $fkElbow $hipm;
			parentConstraint $fkShin $anklem;
			
			string $delme[] = `pointConstraint $fkShin $kneeHipm`;
			delete $delme;
			
			setAttr ($kneeHipm[0] + ".tx") ((`getAttr ($kneeHipm[0] + ".tx")`) * 2);
			
			string $delme[] = `pointConstraint $fkWrist $ankleKneem`;
			delete $delme;
			
			setAttr ($ankleKneem[0] + ".tx") ((`getAttr ($ankleKneem[0] + ".tx")`) * -2);
			
			float $posVertm[] = `xform -q -ws -rp $poleFindm`;
			
			//Pole Finder - Proper
			group -empty;
			string $hip[] = `ls -sl`;
			
			group -empty;
			string $kneeHip[] = `ls -sl`;
			
			group -empty;
			string $ankle[] = `ls -sl`;
			
			group -empty;
			string $ankleKnee[] = `ls -sl`;
			
			group -empty;
			string $poleFind[] = `ls -sl`;
			
			parent $kneeHip $hip;
			parent $ankleKnee $ankle;
			pointConstraint $kneeHip $ankleKnee $poleFind;
			
			parentConstraint $fkShoulder $hip;
			parentConstraint $fkElbow $ankle;
			
			string $delme[] = `pointConstraint $fkElbow $kneeHip`;
			delete $delme;
			
			setAttr ($kneeHip[0] + ".tx") ((`getAttr ($kneeHip[0] + ".tx")`) * 2);
			
			string $delme[] = `pointConstraint $fkShin $ankleKnee`;
			delete $delme;
			
			setAttr ($ankleKnee[0] + ".tx") ((`getAttr ($ankleKnee[0] + ".tx")`) * -2);
			
			float $posVert[] = `xform -q -ws -rp $poleFind`;
			
			
			//////
			//Actually Place poles
			int $whichPole = `getAttr ($userHand[0] + ".whichPole")`;
			
			if($whichPole == 1)
			{move -ws -rpr $posVert[0] $posVert[1] $posVert[2] $ikPole;}
			else
			{move -ws -rpr $posVertm[0] $posVertm[1] $posVertm[2] $ikPole;}
			
			delete $hipm $anklem $poleFindm;
			
			
			move -ws -rpr $posVert[0] $posVert[1] $posVert[2] $ikIndPole;
			delete $hip $ankle $poleFind;
			
			//Set Ind Trans Control
			float $posVert[] = `xform -q -ws -rp $fkShin`;
			
			move -ws -rpr $posVert[0] $posVert[1] $posVert[2] $ikIndCtrl;
			
			
			//Set All foot channels back to 0
			setAttr ($ikCtrl[0] + ".toeX") (`getAttr ($fkToe[0] + ".rx")`);
			setAttr ($ikCtrl[0] + ".toeY") (`getAttr ($fkToe[0] + ".ry")`);
			setAttr ($ikCtrl[0] + ".toeZ") (`getAttr ($fkToe[0] + ".rz")`);

			setAttr ($ikCtrl[0] + ".toeRoll") 0;
			setAttr ($ikCtrl[0] + ".shinRoll") 0;

			setAttr ($ikCtrl[0] + ".ankleX") 0;
			setAttr ($ikCtrl[0] + ".ankleY") 0;
			setAttr ($ikCtrl[0] + ".ankleZ") 0;

			setAttr ($ikCtrl[0] + ".ballX") 0;
			setAttr ($ikCtrl[0] + ".ballY") 0;
			setAttr ($ikCtrl[0] + ".ballZ") 0;

			setAttr ($ikCtrl[0] + ".toeTipX") 0;
			setAttr ($ikCtrl[0] + ".toeTipY") 0;
			setAttr ($ikCtrl[0] + ".toeTipZ") 0;

			setAttr ($ikCtrl[0] + ".outerX") 0;
			setAttr ($ikCtrl[0] + ".outerY") 0;
			setAttr ($ikCtrl[0] + ".outerZ") 0;

			setAttr ($ikCtrl[0] + ".innerX") 0;
			setAttr ($ikCtrl[0] + ".innerY") 0;
			setAttr ($ikCtrl[0] + ".innerZ") 0;

			setAttr ($ikCtrl[0] + ".heelX") 0;
			setAttr ($ikCtrl[0] + ".heelY") 0;
			setAttr ($ikCtrl[0] + ".heelZ") 0;

			setAttr ($ikCtrl[0] + ".heelTipX") 0;
			setAttr ($ikCtrl[0] + ".heelTipY") 0;
			setAttr ($ikCtrl[0] + ".heelTipZ") 0;
			
			setAttr ($ikCtrl[0] + ".limbLen") 1;
			setAttr ($ikCtrl[0] + ".upperLen") 0;
			setAttr ($ikCtrl[0] + ".midLen") 0;
			setAttr ($ikCtrl[0] + ".lowLen") 0;
			
			
			
			select $ikCtrl;

			print ("\nSUCCESS:AA IK matched to FK for " + $nameSpace + ".");
		}
		else
		{
			print ("\nERROR: Control not a *_Hand / *_Foot control.");
		}//end of verifying if _Hand is in name








	}//end of if single control selected
	else
	{
		print ("\nERROR: Please select the IK Hand Control.");
	}

}//end of global proc matchIKtoFK




//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Match FK to IK
global proc matchFKtoIK()
{
	//select IK hand control
	string $userHand[] = `ls -sl`;

	///////////////////////////////////////////////////////////////////////////
	//Work
	if(`size($userHand)`)
	{

		///////////////////////////////////
		//Verify - Hand Control
		if(`match "_Hand" $userHand[0]` == "_Hand")
		{

			//Find out just the name space
			string $justName = `match "[^:]*$" $userHand[0]`;
			$nameSpace = `substituteAllString $userHand[0] (":" + $justName) ""`;

			//Define IK Control
			select $userHand;
			string $ikCtrl[] = `ls -sl`;

			//Determine Which Side
			string $justName = `match "[^:]*$" $userHand[0]`;
			string $sdd = `substituteAllString $userHand[0] ($nameSpace + ":CTRL_") ""`;
			string $sider = `substituteAllString $sdd "_Hand" ""`;

			//Define Pole Control
			select ($nameSpace + ":CTRL_" + $sider + "_ElbowPole");
			string $ikPole[] = `ls -sl`;

			//Define FK Controls
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Shoulder");
			string $fkShoulderCtrl[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Elbow");
			string $fkElbowCtrl[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Wrist");
			string $fkWristCtrl[] = `ls -sl`;	



			//IK
			select ($nameSpace + ":rig_" + $sider + "_Shoulder_IK");
			string $ikShoulder[] = `ls -sl`;
			pickWalk -d down;
			string $ikElbow[] = `ls -sl`;
			pickWalk -d down;
			string $ikWrist[] = `ls -sl`;

			///////////////////////////////////////////
			//Shoulder
			select $ikShoulder;
				string $ikJoint[] = `ls -sl`;
			select $fkShoulderCtrl;
				string $Ctrl[] = `ls -sl`;
									
			select $Ctrl;
			duplicate -po $Ctrl;
			string $Dup[] = `ls -sl`;
									
			//unlock rotates
			setAttr ($Dup[0] + ".rx") -l 0;
			setAttr ($Dup[0] + ".ry") -l 0;
			setAttr ($Dup[0] + ".rz") -l 0;
									
			orientConstraint $ikJoint $Dup;
									
			if (`getAttr -l ($Ctrl[0] + ".rx")` != 1){setAttr ($Ctrl[0] + ".rx") (`getAttr ($Dup[0] + ".rx")`);}
			if (`getAttr -l ($Ctrl[0] + ".ry")` != 1){setAttr ($Ctrl[0] + ".ry") (`getAttr ($Dup[0] + ".ry")`);}
			if (`getAttr -l ($Ctrl[0] + ".rz")` != 1){setAttr ($Ctrl[0] + ".rz") (`getAttr ($Dup[0] + ".rz")`);}

			delete $Dup;
						
			////////////////////////////////////////
			//Elbow
			select $ikElbow;
				string $ikJoint[] = `ls -sl`;

			select $fkElbowCtrl;
				string $Ctrl[] = `ls -sl`;
									
			select $Ctrl;
			duplicate -po $Ctrl;
			string $Dup[] = `ls -sl`;
									
			//unlock rotates
			setAttr ($Dup[0] + ".rx") -l 0;
			setAttr ($Dup[0] + ".ry") -l 0;
			setAttr ($Dup[0] + ".rz") -l 0;
									
			orientConstraint $ikJoint $Dup;
									
			if (`getAttr -l ($Ctrl[0] + ".rx")` != 1){setAttr ($Ctrl[0] + ".rx") (`getAttr ($Dup[0] + ".rx")`);}
			if (`getAttr -l ($Ctrl[0] + ".ry")` != 1){setAttr ($Ctrl[0] + ".ry") (`getAttr ($Dup[0] + ".ry")`);}
			if (`getAttr -l ($Ctrl[0] + ".rz")` != 1){setAttr ($Ctrl[0] + ".rz") (`getAttr ($Dup[0] + ".rz")`);}

			delete $Dup;

			////////////////////////////////////////
			//Wrist
			select $ikWrist;
				string $ikJoint[] = `ls -sl`;

			select $fkWristCtrl;
				string $Ctrl[] = `ls -sl`;
									
			select $Ctrl;
			duplicate -po $Ctrl;
			string $Dup[] = `ls -sl`;
									
			//unlock rotates
			setAttr ($Dup[0] + ".rx") -l 0;
			setAttr ($Dup[0] + ".ry") -l 0;
			setAttr ($Dup[0] + ".rz") -l 0;
									
			orientConstraint $ikJoint $Dup;
									
			if (`getAttr -l ($Ctrl[0] + ".rx")` != 1){setAttr ($Ctrl[0] + ".rx") (`getAttr ($Dup[0] + ".rx")`);}
			if (`getAttr -l ($Ctrl[0] + ".ry")` != 1){setAttr ($Ctrl[0] + ".ry") (`getAttr ($Dup[0] + ".ry")`);}
			if (`getAttr -l ($Ctrl[0] + ".rz")` != 1){setAttr ($Ctrl[0] + ".rz") (`getAttr ($Dup[0] + ".rz")`);}
									
			delete $Dup;


			//////////////////////////////////////////////////////////////////////
			//For Scaling

			//Define FK Controls
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Shoulder");
			string $fkShoulder[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Elbow");
			string $fkElbow[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Wrist");
			string $fkWrist[] = `ls -sl`;	

			////////////////////////////////////////////
			//define joints
			$rigName = `substituteAllString $justName "CTRL" "rig"`;
			$rigName = `substituteAllString $rigName "Hand" "Elbow_IK"`;
			select ($nameSpace + ":" + $rigName);
			string $ikElbowJoint[] = `ls -sl`;

			$rigName = `substituteAllString $justName "CTRL" "rig"`;
			$rigName = `substituteAllString $rigName "Hand" "Wrist_IK"`;
			select ($nameSpace + ":" + $rigName);
			string $ikWristJoint[] = `ls -sl`;


			//define orgs
			$rigName = `substituteAllString $justName "CTRL" "rig"`;
			$rigName = `substituteAllString $rigName "_Hand" "_elbow_Org_MD"`;
			$rigName = `substituteAllString $rigName "_elbow" "_elbow"`;

			select ($nameSpace + ":" + $rigName);
			string $elbowOrgMD[] = `ls -sl`;

			$rigName = `substituteAllString $justName "CTRL" "rig"`;
			$rigName = `substituteAllString $rigName "_Hand" "_wrist_Org_MD"`;
			$rigName = `substituteAllString $rigName "_wrist" "_wrist"`;

			select ($nameSpace + ":" + $rigName);
			string $wristOrgMD[] = `ls -sl`;

			//get values
			float $orgWristTX = `getAttr ($wristOrgMD[0] + ".input1X")`;
			float $orgElbowTX = `getAttr ($elbowOrgMD[0] + ".input1X")`;

			float $curIKElbowTX = `getAttr ($ikElbowJoint[0] + ".tx")`;
			float $curIKWristTX = `getAttr ($ikWristJoint[0] + ".tx")`;

			//Set Values                                      
			setAttr ($fkElbow[0] + ".stretch") (($curIKElbowTX - $orgElbowTX)*2);
			setAttr ($fkWrist[0] + ".stretch") (($curIKWristTX - $orgWristTX)*3.333);


			///////////////////////////////////////////
			//Complete
			select $ikCtrl;

			print ("\nSUCCESS: FK matched to IK for " + $nameSpace + ".");
		}
		else
		{
			print ("\nERROR: Control not a *_Hand / *_Foot control.");
		}//end of verifying if _Hand is in name

		
		
		
		
		
		
		
		///////////////////////////////////
		//Verify - Foot control
		if(`match "_Foot" $userHand[0]` == "_Foot" && `attributeExists "shinRoll" $userHand[0]` == 0)
		{

			//Find out just the name space
			string $justName = `match "[^:]*$" $userHand[0]`;
			$nameSpace = `substituteAllString $userHand[0] (":" + $justName) ""`;

			//Define IK Control
			select $userHand;
			string $ikCtrl[] = `ls -sl`;

			//Determine Which Side
			string $justName = `match "[^:]*$" $userHand[0]`;
			string $sdd = `substituteAllString $userHand[0] ($nameSpace + ":CTRL_") ""`;
			string $sider = `substituteAllString $sdd "_Foot" ""`;

			//Define Pole Control
			select ($nameSpace + ":CTRL_" + $sider + "_KneePole");
			string $ikPole[] = `ls -sl`;

			//Define FK Controls
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Hip");
			string $fkShoulderCtrl[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Knee");
			string $fkElbowCtrl[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Ankle");
			string $fkWristCtrl[] = `ls -sl`;	
			
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Toe");
			string $fkToeCtrl[] = `ls -sl`;



			//IK
			select ($nameSpace + ":rig_" + $sider + "_Hip_IK");
			string $ikShoulder[] = `ls -sl`;
			pickWalk -d down;
			string $ikElbow[] = `ls -sl`;
			pickWalk -d down;
			string $ikWrist[] = `ls -sl`;

			///////////////////////////////////////////
			//Shoulder
			select $ikShoulder;
				string $ikJoint[] = `ls -sl`;
			select $fkShoulderCtrl;
				string $Ctrl[] = `ls -sl`;
									
			select $Ctrl;
			duplicate -po $Ctrl;
			string $Dup[] = `ls -sl`;
									
			//unlock rotates
			setAttr ($Dup[0] + ".rx") -l 0;
			setAttr ($Dup[0] + ".ry") -l 0;
			setAttr ($Dup[0] + ".rz") -l 0;
									
			orientConstraint $ikJoint $Dup;
									
			if (`getAttr -l ($Ctrl[0] + ".rx")` != 1){setAttr ($Ctrl[0] + ".rx") (`getAttr ($Dup[0] + ".rx")`);}
			if (`getAttr -l ($Ctrl[0] + ".ry")` != 1){setAttr ($Ctrl[0] + ".ry") (`getAttr ($Dup[0] + ".ry")`);}
			if (`getAttr -l ($Ctrl[0] + ".rz")` != 1){setAttr ($Ctrl[0] + ".rz") (`getAttr ($Dup[0] + ".rz")`);}

			delete $Dup;
						
			////////////////////////////////////////
			//Elbow
			select $ikElbow;
				string $ikJoint[] = `ls -sl`;

			select $fkElbowCtrl;
				string $Ctrl[] = `ls -sl`;
									
			select $Ctrl;
			duplicate -po $Ctrl;
			string $Dup[] = `ls -sl`;
									
			//unlock rotates
			setAttr ($Dup[0] + ".rx") -l 0;
			setAttr ($Dup[0] + ".ry") -l 0;
			setAttr ($Dup[0] + ".rz") -l 0;
									
			orientConstraint $ikJoint $Dup;
									
			if (`getAttr -l ($Ctrl[0] + ".rx")` != 1){setAttr ($Ctrl[0] + ".rx") (`getAttr ($Dup[0] + ".rx")`);}
			if (`getAttr -l ($Ctrl[0] + ".ry")` != 1){setAttr ($Ctrl[0] + ".ry") (`getAttr ($Dup[0] + ".ry")`);}
			if (`getAttr -l ($Ctrl[0] + ".rz")` != 1){setAttr ($Ctrl[0] + ".rz") (`getAttr ($Dup[0] + ".rz")`);}

			delete $Dup;

			////////////////////////////////////////
			//Wrist
			select $ikWrist;
				string $ikJoint[] = `ls -sl`;

			select $fkWristCtrl;
				string $Ctrl[] = `ls -sl`;
									
			select $Ctrl;
			duplicate -po $Ctrl;
			string $Dup[] = `ls -sl`;
									
			//unlock rotates
			setAttr ($Dup[0] + ".rx") -l 0;
			setAttr ($Dup[0] + ".ry") -l 0;
			setAttr ($Dup[0] + ".rz") -l 0;
									
			orientConstraint $ikJoint $Dup;
									
			if (`getAttr -l ($Ctrl[0] + ".rx")` != 1){setAttr ($Ctrl[0] + ".rx") (`getAttr ($Dup[0] + ".rx")`);}
			if (`getAttr -l ($Ctrl[0] + ".ry")` != 1){setAttr ($Ctrl[0] + ".ry") (`getAttr ($Dup[0] + ".ry")`);}
			if (`getAttr -l ($Ctrl[0] + ".rz")` != 1){setAttr ($Ctrl[0] + ".rz") (`getAttr ($Dup[0] + ".rz")`);}
									
			delete $Dup;


			//////////////////////////////////////////////////////////////////////
			//For Scaling

			//Define FK Controls
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Hip");
			string $fkShoulder[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Knee");
			string $fkElbow[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Ankle");
			string $fkWrist[] = `ls -sl`;

			select ($nameSpace + ":CTRL_FK_" + $sider + "_Toe");
			string $fkToe[] = `ls -sl`;	

			setAttr ($fkToe[0] + ".rx") (`getAttr ($nameSpace + ":" + $sider + "Leg_Toe.rx")`);
			setAttr ($fkToe[0] + ".ry") (`getAttr ($nameSpace + ":" + $sider + "Leg_Toe.ry")`);
			setAttr ($fkToe[0] + ".rz") (`getAttr ($nameSpace + ":" + $sider + "Leg_Toe.rz")`);

			////////////////////////////////////////////
			//define joints
			$rigName = `substituteAllString $justName "CTRL" "rig"`;
			$rigName = `substituteAllString $rigName "Foot" "Knee_IK"`;
			select ($nameSpace + ":" + $rigName);
			string $ikElbowJoint[] = `ls -sl`;

			$rigName = `substituteAllString $justName "CTRL" "rig"`;
			$rigName = `substituteAllString $rigName "Foot" "Ankle_IK"`;
			select ($nameSpace + ":" + $rigName);
			string $ikWristJoint[] = `ls -sl`;


			//define orgs
			$rigName = `substituteAllString $justName "CTRL" "rig"`;
			$rigName = `substituteAllString $rigName "_Foot" "__Leg_elbow_Org_MD"`;
			$rigName = `substituteAllString $rigName "_elbow" "_elbow"`;

			select ($nameSpace + ":" + $rigName);
			string $elbowOrgMD[] = `ls -sl`;

			$rigName = `substituteAllString $justName "CTRL" "rig"`;
			$rigName = `substituteAllString $rigName "_Foot" "__Leg_wrist_Org_MD"`;
			$rigName = `substituteAllString $rigName "_wrist" "_wrist"`;

			select ($nameSpace + ":" + $rigName);
			string $wristOrgMD[] = `ls -sl`;

			//get values
			float $orgWristTX = `getAttr ($wristOrgMD[0] + ".input1X")`;
			float $orgElbowTX = `getAttr ($elbowOrgMD[0] + ".input1X")`;

			float $curIKElbowTX = `getAttr ($ikElbowJoint[0] + ".tx")`;
			float $curIKWristTX = `getAttr ($ikWristJoint[0] + ".tx")`;

			//Set Values                                      
			setAttr ($fkElbow[0] + ".stretch") (($curIKElbowTX - $orgElbowTX)*2);
			setAttr ($fkWrist[0] + ".stretch") (($curIKWristTX - $orgWristTX)*3.333);


			///////////////////////////////////////////
			//Complete
			select $ikCtrl;

			print ("\nSUCCESS: FK matched to IK for " + $nameSpace + ".");
		}
		else
		{
			print ("\nERROR: Control not a *_Hand / *_Foot control.");
		}//end of verifying if _Hand is in name
		
		
		






		///////////////////////////////////
		//Verify - foot control
		if(`match "_Foot" $userHand[0]` == "_Foot" && `attributeExists "shinRoll" $userHand[0]` == 1)
		{
			//Find out just the name space
			string $justName = `match "[^:]*$" $userHand[0]`;
			$nameSpace = `substituteAllString $userHand[0] (":" + $justName) ""`;

			//Define IK Control
			select $userHand;
			string $ikCtrl[] = `ls -sl`;

			//Determine Which Side
			string $justName = `match "[^:]*$" $userHand[0]`;
			string $sdd = `substituteAllString $userHand[0] ($nameSpace + ":CTRL_") ""`;
			string $sider = `substituteAllString $sdd "_Foot" ""`;

			//Define FK Controls
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Hip");
			string $fkShoulder[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Knee");
			string $fkElbow[] = `ls -sl`;
			
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Shin");
			string $fkShin[] = `ls -sl`;
					
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Ankle");
			string $fkWrist[] = `ls -sl`;	
			
			select ($nameSpace + ":CTRL_FK_" + $sider + "_Toe");
			string $fkToe[] = `ls -sl`;
			
			//Define IK Joints
			select ($nameSpace + ":rig_" + $sider + "_Ind_Hip_IK");
			string $ikHip[] = `ls -sl`;
			
			select ($nameSpace + ":rig_" + $sider + "_Ind_Knee_IK");
			string $ikKnee[] = `ls -sl`;
			
			select ($nameSpace + ":rig_" + $sider + "_Shin_FKNull");
			string $ikShin[] = `ls -sl`;
			
			select ($nameSpace + ":rig_" + $sider + "_Ankle_IK");
			string $ikAnkle[] = `ls -sl`;
			
			select ($nameSpace + ":rig_" + $sider + "_Toe_IK");
			string $ikToe[] = `ls -sl`;
			
			
			//Create constraints to align
			string $delme[] = `parentConstraint $ikHip $fkShoulder`;
			delete $delme;
			
			string $delme[] = `parentConstraint $ikKnee $fkElbow`;
			delete $delme;
			
			string $delme[] = `parentConstraint $ikShin $fkShin`;
			delete $delme;
			
			string $delme[] = `parentConstraint $ikAnkle $fkWrist`;
			delete $delme;
			
			string $delme[] = `parentConstraint $ikToe $fkToe`;
			delete $delme;
			
			
			
			select $ikCtrl;

			print ("\nSUCCESS: FK matched to IK for " + $nameSpace + ".");
		}
		else
		{
			print ("\nERROR: Control not a *_Hand / *_Foot control.");
		}//end of verifying if _Hand is in name












		
		
		
		
	}//end of if single control selected
	else
	{
		print ("\nERROR: Please select the IK Hand Control.");
	}

}//end of global proc matchFKtoIK







//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Match IK to FK
global proc matchIKtoFKTen()
{
	//select Pinner control
	string $userPinner[] = `ls -sl`;

	///////////////////////////////////////////////////////////////////////////
	//Work
	if(`size($userPinner)`)
	{
		///////////////////////////////////
		//Verify - Pinner control
		if(`match "_Pinner" $userPinner[0]` == "_Pinner")
		{
			//Find out just the name space
			string $justName = `match "[^:]*$" $userPinner[0]`;
			$nameSpace = `substituteAllString $userPinner[0] $justName ""`;


int $jointNum = `getAttr ($userPinner[0] + ".jointNum")`;

string $noctrl = `substituteAllString $justName "CTRL_" "" `;
string $namer = `substituteAllString $noctrl "_Pinner" "" `;


select ($nameSpace + $namer + "_IKcurve");
string $clusPath[] = `ls -sl`;


int $counterClus;
int $sizerClus = `getAttr ($clusPath[0] + ".spans")`;
int $degreeC = `getAttr ($clusPath[0] + ".degree")`;
int $sizerClus;

string $objShapes[] = `listRelatives -shapes $clusPath[0]`;

//2 = closed, 0 = Open
int $closedRad = `getAttr ($objShapes[0] + ".form")`;

if($closedRad == 0)
{$sizerClus = ($sizerClus + $degreeC);}
else
{$sizerClus = $sizerClus;}//end of if curve is closed


//create base ep curve
curve -d 1 -p 0 0 0 -p -1 0 0 -k 0 -k 1 ;
string $baseCurve[] =`ls -sl`;

rebuildCurve -ch 1 -rpo 1 -rt 0 -end 1 -kr 0 -kcp 0 -kep 1 -kt 0 -s ($jointNum - 2) -d 3 -tol 0.01 $baseCurve;


int $counter;
int $sizer = $jointNum;

for ($counter = 0; $counter <= $sizer; $counter++)
{
    float $pos[] = `xform -q -ws -rp  ($nameSpace + "CTRL_" + $namer + "_" + $counter)`;
    
    select -r ($baseCurve[0] + ".cv[" + $counter + "]") ;
    move -ws $pos[0] $pos[1] $pos[2] ;
}//end of aligning cvs to fk

int $spanNum = `getAttr ($clusPath[0] + ".spans")`;
rebuildCurve -ch 1 -rpo 1 -rt 0 -end 1 -kr 0 -kcp 0 -kep 1 -kt 0 -s $spanNum -d 3 -tol 0.01 $baseCurve;


int $counter;
int $sizer = $sizerClus;

for ($counter = 0; $counter < $sizer; $counter++)
{

//$counter = 0;
float $pos[] = `xform -q -ws -t ($baseCurve[0] + ".cv[" + $counter + "]")`;
    
    select -r ($nameSpace + "CTRL_" + $namer + "_Clus_" + $counter) ;
    move -ws $pos[0] $pos[1] $pos[2] ;

}//end of align IK to cvs


delete $baseCurve;

select $userPinner;



}//end of verify pinner
else
{print "\nFAIL: Please Select the *_Pinner of the tentacle";}

}//end of verify only pinner selected
else
{print "\nFAIL: Please Select the *_Pinner of the tentacle";}

}//end of global proc













//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Match FK to IK
global proc matchFKtoIKTen()
{
	//select Pinner control
	string $userPinner[] = `ls -sl`;

	///////////////////////////////////////////////////////////////////////////
	//Work
	if(`size($userPinner)`)
	{
		///////////////////////////////////
		//Verify - Pinner control
		if(`match "_Pinner" $userPinner[0]` == "_Pinner")
		{
			//Find out just the name space
			string $justName = `match "[^:]*$" $userPinner[0]`;
			$nameSpace = `substituteAllString $userPinner[0] $justName ""`;


int $jointNum = `getAttr ($userPinner[0] + ".jointNum")`;

string $noctrl = `substituteAllString $justName "CTRL_" "" `;
string $namer = `substituteAllString $noctrl "_Pinner" "" `;


//Set FK Back to Default
setAttr ($nameSpace + "CTRL_" + $namer + "_0.allRotX") 0;
setAttr ($nameSpace + "CTRL_" + $namer + "_0.allRotY") 0;
setAttr ($nameSpace + "CTRL_" + $namer + "_0.allRotZ") 0;
setAttr ($nameSpace + "CTRL_" + $namer + "_0.allLength") 1;



int $counter;
int $sizer = $jointNum;

for ($counter = 0; $counter <= $sizer; $counter++)
{
setAttr ($nameSpace + "CTRL_" + $namer + "_" + $counter + ".sx") 1;
setAttr ($nameSpace + "CTRL_" + $namer + "_" + $counter + ".sy") 1;
setAttr ($nameSpace + "CTRL_" + $namer + "_" + $counter + ".sz") 1;


select ($nameSpace + "CTRL_" + $namer + "_" + $counter);
string $alignObjKF[] = `ls -sl`;

select ($nameSpace + "rig_IK_" + $namer + "_" + $counter);
string $alignGoalKF[] = `ls -sl`;


    int $txVal = `getAttr -se ($alignObjKF[0] + ".tx")`;
    int $tyVal = `getAttr -se ($alignObjKF[0] + ".ty")`;
    int $tzVal = `getAttr -se ($alignObjKF[0] + ".tz")`;
    int $rxVal = `getAttr -se ($alignObjKF[0] + ".rx")`;
    int $ryVal = `getAttr -se ($alignObjKF[0] + ".ry")`;
    int $rzVal = `getAttr -se ($alignObjKF[0] + ".rz")`;
    

    string $skiptx = "";
    string $skipty = "";
    string $skiptz = "";
    string $skiprx = "";
    string $skipry = "";
    string $skiprz = "";
      
    string $delme[] = `eval ("parentConstraint " + $skiptx + $skipty + $skiptz + $skiprx + $skipry + $skiprz + "-weight 1 " + $alignGoalKF[0] + " " + $alignObjKF[0] + ";")`;

    //gather attrs
    float $theTx = `getAttr ($alignObjKF[0] + ".tx")`;
    float $theTy = `getAttr ($alignObjKF[0] + ".ty")`;
    float $theTz = `getAttr ($alignObjKF[0] + ".tz")`;
    float $theRx = `getAttr ($alignObjKF[0] + ".rx")`;
    float $theRy = `getAttr ($alignObjKF[0] + ".ry")`;
    float $theRz = `getAttr ($alignObjKF[0] + ".rz")`;
    
    delete $delme;
    
    //set attrs
    if($txVal){setAttr ($alignObjKF[0] + ".tx") $theTx;}
    if($tyVal){setAttr ($alignObjKF[0] + ".ty") $theTy;}
    if($tzVal){setAttr ($alignObjKF[0] + ".tz") $theTz;}
    if($rxVal){setAttr ($alignObjKF[0] + ".rx") $theRx;}
    if($ryVal){setAttr ($alignObjKF[0] + ".ry") $theRy;}
    if($rzVal){setAttr ($alignObjKF[0] + ".rz") $theRz;}
  
}




}//end of verify pinner
else
{print "\nFAIL: Please Select the *_Pinner of the tentacle";}

}//end of verify only pinner selected
else
{print "\nFAIL: Please Select the *_Pinner of the tentacle";}

}//end of global proc














