'Bind Valve Skeleton to Character Studio Nulls

bindValveSkel
'----------------------------------------------
' Bind Valve Skel
'----------------------------------------------
function bindValveSkel
	set root = ActiveProject.ActiveScene.Root
	
	set MaxModel = root.FindChild("MAXSceneRoot1")
	set ValveSkel = root.FindChild("ValveBiped")
	
	PositionObjects MaxModel, ValveSkel
	refresh
	UpVectorObjects MaxModel, ValveSkel	

end function
'----------------------------------------------
' Position Objects
'----------------------------------------------
function PositionObjects(inMaxModel, inValveSkel)

	set constraints = CreateObject("XSI.Collection")
	PosCnstwithCompensation inValveSkel.FindChild("UpperBody"), inMaxModel.FindChild("Bip01"), constraints
	PosCnst inValveSkel.FindChild("Spine"), inMaxModel.FindChild("Bip01_Spine"), constraints	
	
	PosCnst inValveSkel.FindChild("Bip01_Spine"), inMaxModel.FindChild("Bip01_Spine"), constraints	
	
	PosCnst inValveSkel.FindChild("Bip01_Spine1"), inMaxModel.FindChild("Bip01_Spine1"), constraints		

	PosCnst inValveSkel.FindChild("Bip01_Spine2"), inMaxModel.FindChild("Bip01_Spine2"), constraints
	PosCnst inValveSkel.FindChild("Bip01_Spine4"), inMaxModel.FindChild("Bip01_Spine3"), constraints	

	PosCnst inValveSkel.FindChild("eff"), inMaxModel.FindChild("Bip01_Neck"), constraints	
	PosCnst inValveSkel.FindChild("eff"), inMaxModel.FindChild("Bip01_Neck"), constraints	

	PosCnst inValveSkel.FindChild("Bip01_Head1"), inMaxModel.FindChild("Bip01_Head"), constraints	
	PosCnst inValveSkel.FindChild("eff1"), inMaxModel.FindChild("Dummy21"), constraints	

	'right arm
	
	PosCnst inValveSkel.FindChild("RRootShoulder"), inMaxModel.FindChild("Bip01_R_Clavicle"), constraints	
	PosCnst inValveSkel.FindChild("RShoulderEff"), inMaxModel.FindChild("Bip01_R_UpperArm"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_R_Forearm"), inMaxModel.FindChild("Bip01_R_Forearm"), constraints	
	PosCnst inValveSkel.FindChild("RArmEff"), inMaxModel.FindChild("Bip01_R_Hand"), constraints	
	


	'left arm
	
	PosCnst inValveSkel.FindChild("LRootShoulder"), inMaxModel.FindChild("Bip01_L_Clavicle"), constraints	
	PosCnst inValveSkel.FindChild("LShoulderEff"), inMaxModel.FindChild("Bip01_L_UpperArm"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_L_Forearm"), inMaxModel.FindChild("Bip01_L_Forearm"), constraints	
	PosCnst inValveSkel.FindChild("LArmEff"), inMaxModel.FindChild("Bip01_L_Hand"), constraints	
	

	'right leg
	PosCnst inValveSkel.FindChild("RLegRoot"), inMaxModel.FindChild("Bip01_R_Thigh"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_R_Calf"), inMaxModel.FindChild("Bip01_R_Calf"), constraints	

	PoseC inValveSkel.FindChild("RightFoot"), inMaxModel.FindChild("Bip01_R_Foot"), constraints	
 	PoseC inValveSkel.FindChild("RightToe"), inMaxModel.FindChild("Bip01_R_Toe0"), constraints	


	'left leg
	PosCnst inValveSkel.FindChild("LLegRoot"), inMaxModel.FindChild("Bip01_L_Thigh"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_L_Calf"), inMaxModel.FindChild("Bip01_L_Calf"), constraints	
	
	PoseC inValveSkel.FindChild("LeftFoot"), inMaxModel.FindChild("Bip01_L_Foot"), constraints	
  	PoseC inValveSkel.FindChild("LeftToe"), inMaxModel.FindChild("Bip01_L_Toe0"), constraints	

	'Right Hand
	
	'Roots 
	PosCnst inValveSkel.FindChild("LThumbRoot"), inMaxModel.FindChild("Bip01_L_Finger0"), constraints	
	PosCnst inValveSkel.FindChild("LIndexRoot"), inMaxModel.FindChild("Bip01_L_Finger1"), constraints	
	PosCnst inValveSkel.FindChild("LMiddleRoot"), inMaxModel.FindChild("Bip01_L_Finger2"), constraints	
	PosCnst inValveSkel.FindChild("LRingRoot"), inMaxModel.FindChild("Bip01_L_Finger3"), constraints	
	PosCnst inValveSkel.FindChild("LPinkyRoot"), inMaxModel.FindChild("Bip01_L_Finger4"), constraints	

	'First Bone

	'Second Bone
	PosCnst inValveSkel.FindChild("Bip01_L_Finger01"), inMaxModel.FindChild("Bip01_L_Finger01"), constraints		
	PosCnst inValveSkel.FindChild("Bip01_L_Finger11"), inMaxModel.FindChild("Bip01_L_Finger11"), constraints		
	PosCnst inValveSkel.FindChild("Bip01_L_Finger21"), inMaxModel.FindChild("Bip01_L_Finger21"), constraints		
	PosCnst inValveSkel.FindChild("Bip01_L_Finger31"), inMaxModel.FindChild("Bip01_L_Finger31"), constraints		
	PosCnst inValveSkel.FindChild("Bip01_L_Finger41"), inMaxModel.FindChild("Bip01_L_Finger41"), constraints		


	' Third Bone
	PosCnst inValveSkel.FindChild("Bip01_L_Finger02"), inMaxModel.FindChild("Bip01_L_Finger02"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_L_Finger12"), inMaxModel.FindChild("Bip01_L_Finger12"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_L_Finger22"), inMaxModel.FindChild("Bip01_L_Finger22"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_L_Finger32"), inMaxModel.FindChild("Bip01_L_Finger32"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_L_Finger42"), inMaxModel.FindChild("Bip01_L_Finger42"), constraints	

	'Effector
	PosCnst inValveSkel.FindChild("LThumbEffector"), inMaxModel.FindChild("Dummy06"), constraints	
	PosCnst inValveSkel.FindChild("LIndexEffector"), inMaxModel.FindChild("Dummy07"), constraints	
	PosCnst inValveSkel.FindChild("LMiddleEffector"), inMaxModel.FindChild("Dummy08"), constraints	
	PosCnst inValveSkel.FindChild("LRingEffector"), inMaxModel.FindChild("Dummy09"), constraints	
	PosCnst inValveSkel.FindChild("LPinkyEffector"), inMaxModel.FindChild("Dummy10"), constraints	



	'Right Hand
	
	'Roots 
	PosCnst inValveSkel.FindChild("RThumbRoot"), inMaxModel.FindChild("Bip01_R_Finger0"), constraints	
	PosCnst inValveSkel.FindChild("RIndexRoot"), inMaxModel.FindChild("Bip01_R_Finger1"), constraints	
	PosCnst inValveSkel.FindChild("RMiddleRoot"), inMaxModel.FindChild("Bip01_R_Finger2"), constraints	
	PosCnst inValveSkel.FindChild("RRingRoot"), inMaxModel.FindChild("Bip01_R_Finger3"), constraints	
	PosCnst inValveSkel.FindChild("RPinkyRoot"), inMaxModel.FindChild("Bip01_R_Finger4"), constraints	

	'First Bone

	'Second Bone
	PosCnst inValveSkel.FindChild("Bip01_R_Finger01"), inMaxModel.FindChild("Bip01_R_Finger01"), constraints		
	PosCnst inValveSkel.FindChild("Bip01_R_Finger11"), inMaxModel.FindChild("Bip01_R_Finger11"), constraints		
	PosCnst inValveSkel.FindChild("Bip01_R_Finger21"), inMaxModel.FindChild("Bip01_R_Finger21"), constraints		
	PosCnst inValveSkel.FindChild("Bip01_R_Finger31"), inMaxModel.FindChild("Bip01_R_Finger31"), constraints		
	PosCnst inValveSkel.FindChild("Bip01_R_Finger41"), inMaxModel.FindChild("Bip01_R_Finger41"), constraints		


	' Third Bone
	PosCnst inValveSkel.FindChild("Bip01_R_Finger02"), inMaxModel.FindChild("Bip01_R_Finger02"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_R_Finger12"), inMaxModel.FindChild("Bip01_R_Finger12"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_R_Finger22"), inMaxModel.FindChild("Bip01_R_Finger22"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_R_Finger32"), inMaxModel.FindChild("Bip01_R_Finger32"), constraints	
	PosCnst inValveSkel.FindChild("Bip01_R_Finger42"), inMaxModel.FindChild("Bip01_R_Finger42"), constraints	

	'Effector
	PosCnst inValveSkel.FindChild("RThumbEffector"), inMaxModel.FindChild("Dummy01"), constraints	
	PosCnst inValveSkel.FindChild("RIndexEffector"), inMaxModel.FindChild("Dummy02"), constraints	
	PosCnst inValveSkel.FindChild("RMiddleEffector"), inMaxModel.FindChild("Dummy03"), constraints	
	PosCnst inValveSkel.FindChild("RRingEffector"), inMaxModel.FindChild("Dummy04"), constraints	
	PosCnst inValveSkel.FindChild("RPinkyEffector"), inMaxModel.FindChild("Dummy05"), constraints	


end function
'----------------------------------------------
' Up Vector Objects
'----------------------------------------------
function UpVectorObjects(inMaxModel, inValveSkel)

	set upVpoints = CreateObject("XSI.Collection")
	set Chains = CreateObject("XSI.Collection")

	UpV inValveSkel.FindChild("Bip01_Spine"), inMaxModel.FindChild("Bip01_Spine"), upVpoints, Chains
	UpV inValveSkel.FindChild("Bip01_Spine1"), inMaxModel.FindChild("Bip01_Spine1"), upVpoints, Chains		
	UpV inValveSkel.FindChild("Bip01_Spine2"), inMaxModel.FindChild("Bip01_Spine2"), upVpoints, Chains
	UpV inValveSkel.FindChild("Bip01_Spine4"), inMaxModel.FindChild("Bip01_Spine3"), upVpoints, Chains
	
		
	
	UpV inValveSkel.FindChild("Bip01_Neck1"), inMaxModel.FindChild("Bip01_Neck"), upVpoints, Chains

	UpV inValveSkel.FindChild("Bip01_Head1"), inMaxModel.FindChild("Bip01_Head"), upVpoints, Chains	

	'right arm
		
	UpV inValveSkel.FindChild("Bip01_R_Clavicle"), inMaxModel.FindChild("Bip01_R_Clavicle"), upVpoints, Chains	


	UpV inValveSkel.FindChild("Bip01_R_UpperArm"), inMaxModel.FindChild("Bip01_R_UpperArm"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Forearm"), inMaxModel.FindChild("Bip01_R_Forearm"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Hand"), inMaxModel.FindChild("Bip01_R_Hand"), upVpoints, Chains	


	'left arm
	
	UpV inValveSkel.FindChild("Bip01_L_Clavicle"), inMaxModel.FindChild("Bip01_L_Clavicle"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_UpperArm"), inMaxModel.FindChild("Bip01_L_UpperArm"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Forearm"), inMaxModel.FindChild("Bip01_L_Forearm"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Hand"), inMaxModel.FindChild("Bip01_L_Hand"), upVpoints, Chains	


	'right leg
	UpV inValveSkel.FindChild("Bip01_R_Thigh"), inMaxModel.FindChild("Bip01_R_Thigh"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Calf"), inMaxModel.FindChild("Bip01_R_Calf"), upVpoints, Chains	



	'left leg
	UpV inValveSkel.FindChild("Bip01_L_Thigh"), inMaxModel.FindChild("Bip01_L_Thigh"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Calf"), inMaxModel.FindChild("Bip01_L_Calf"), upVpoints, Chains	

	'Right Hand
	
	'First Bone
	UpV inValveSkel.FindChild("Bip01_L_Finger0"), inMaxModel.FindChild("Bip01_L_Finger0"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Finger1"), inMaxModel.FindChild("Bip01_L_Finger1"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Finger2"), inMaxModel.FindChild("Bip01_L_Finger2"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Finger3"), inMaxModel.FindChild("Bip01_L_Finger3"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Finger4"), inMaxModel.FindChild("Bip01_L_Finger4"), upVpoints, Chains	

	'Second Bone
	UpV inValveSkel.FindChild("Bip01_L_Finger01"), inMaxModel.FindChild("Bip01_L_Finger01"), upVpoints, Chains		
	UpV inValveSkel.FindChild("Bip01_L_Finger11"), inMaxModel.FindChild("Bip01_L_Finger11"), upVpoints, Chains		
	UpV inValveSkel.FindChild("Bip01_L_Finger21"), inMaxModel.FindChild("Bip01_L_Finger21"), upVpoints, Chains		
	UpV inValveSkel.FindChild("Bip01_L_Finger31"), inMaxModel.FindChild("Bip01_L_Finger31"), upVpoints, Chains		
	UpV inValveSkel.FindChild("Bip01_L_Finger41"), inMaxModel.FindChild("Bip01_L_Finger41"), upVpoints, Chains		


	' Third Bone
	UpV inValveSkel.FindChild("Bip01_L_Finger02"), inMaxModel.FindChild("Bip01_L_Finger02"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Finger12"), inMaxModel.FindChild("Bip01_L_Finger12"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Finger22"), inMaxModel.FindChild("Bip01_L_Finger22"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Finger32"), inMaxModel.FindChild("Bip01_L_Finger32"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_L_Finger42"), inMaxModel.FindChild("Bip01_L_Finger42"), upVpoints, Chains	



	'Right Hand
	
	'First Bone
	UpV inValveSkel.FindChild("Bip01_R_Finger0"), inMaxModel.FindChild("Bip01_R_Finger0"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Finger1"), inMaxModel.FindChild("Bip01_R_Finger1"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Finger2"), inMaxModel.FindChild("Bip01_R_Finger2"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Finger3"), inMaxModel.FindChild("Bip01_R_Finger3"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Finger4"), inMaxModel.FindChild("Bip01_R_Finger4"), upVpoints, Chains	

	'Second Bone
	UpV inValveSkel.FindChild("Bip01_R_Finger01"), inMaxModel.FindChild("Bip01_R_Finger01"), upVpoints, Chains		
	UpV inValveSkel.FindChild("Bip01_R_Finger11"), inMaxModel.FindChild("Bip01_R_Finger11"), upVpoints, Chains		
	UpV inValveSkel.FindChild("Bip01_R_Finger21"), inMaxModel.FindChild("Bip01_R_Finger21"), upVpoints, Chains		
	UpV inValveSkel.FindChild("Bip01_R_Finger31"), inMaxModel.FindChild("Bip01_R_Finger31"), upVpoints, Chains		
	UpV inValveSkel.FindChild("Bip01_R_Finger41"), inMaxModel.FindChild("Bip01_R_Finger41"), upVpoints, Chains		


	' Third Bone
	UpV inValveSkel.FindChild("Bip01_R_Finger02"), inMaxModel.FindChild("Bip01_R_Finger02"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Finger12"), inMaxModel.FindChild("Bip01_R_Finger12"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Finger22"), inMaxModel.FindChild("Bip01_R_Finger22"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Finger32"), inMaxModel.FindChild("Bip01_R_Finger32"), upVpoints, Chains	
	UpV inValveSkel.FindChild("Bip01_R_Finger42"), inMaxModel.FindChild("Bip01_R_Finger42"), upVpoints, Chains	


	ApplyUpVectors upVpoints, Chains

end function
'---------------------------------------------
' Pos Cnst
'---------------------------------------------
function PosCnst(in_constrained, in_constraining, in_constrainList)
	
	set Cnst = in_constrained.Kinematics.AddConstraint("Position", in_constraining, False)

end function
'---------------------------------------------
' Pos Cnst with Compensation
'---------------------------------------------
function PosCnstwithCompensation (in_constrained, in_constraining, in_constrainList)
	
	set Cnst = in_constrained.Kinematics.AddConstraint("Position", in_constraining, True)
	
end function
'---------------------------------------------
' Attach
'---------------------------------------------
function UpV(in_constrained, in_constraining, inUpVpoints, inChains)	
	
	if in_constrained.type = "bone" then
	
		dim snull
		set snull = in_constraining.AddPrimitive("Null", "Shadow_upVa")
		
		'get the orientation of the bone
		set boneOri = in_constrained.Kinematics.Global.Transform


		set upOffset = XSIMath.CreateVector3
		
		HackInversionTest = GetValue(in_constrained.Parent & ".kine.global.sclx")
		
		if HackInversionTest > 0 then
			upOffset.set 0,4,0
		else
			upOffset.set 0,-4,0
		end if
		
		boneOri.AddLocalTranslation upOffset
		
		set upPos = XSIMath.CreateVector3
		boneOri.GetTranslation upPos
				
		Translate snull , upPos.x, upPos.y, upPos.z, siAbsolute, siGlobal, siObj, siXYZ	


		'snull.properties("visibility").viewvis.value = False
	
		inUpVpoints.add snull
		inChains.add in_constrained
	
	end if

end function
'---------------------------------------------
' Pose C
'---------------------------------------------
function PoseC (in_constrained, in_constraining, in_constrainList)
 
	set Cnst = in_constrained.Kinematics.AddConstraint("Pose", in_constraining, True)
 
end function
'--------------------------------------------- 
' Apply Up Vectors
'--------------------------------------------- 
function ApplyUpVectors(inUpVpoints, inChains)
	
	for i = 0 to inUpVpoints.count - 1
		ApplyOp "SkeletonUpVector", inChains(i) &";"& inUpVpoints(i), 3, siPersistentOperation	
	next
end function
'--------------------------------------------- 


