' Hierachy From Biped
' misner

hierachy_from_Biped
'-----------------------------------
' Hierachy from Biped
'----------------------------------- 
sub hierachy_from_Biped

	dim hType, valid, nbVertebra 


	setuserpref("SI3D_NODETRANSFORM_CHILD_COMPENSATE"), 0
	
 	attach_skeleton
 			
end sub

'*********************************************
'*********************************************
'*********************************************

' Attach Skeleton

'*********************************************
'*********************************************
'*********************************************
'---------------------------------------------

sub attach_skeleton()
	

	mpath = Application.InstallationPath (siFactoryPath) & "\Data\XSI_SAMPLES\Models\ValveBiped.emdl"

	ImportModel mpath ,,,myModel
	
	AS_model_and_Bip01_match myModel, valid
	if valid = False then exit sub
	
	AS_Attach_hips myModel, valid
	
	AS_Attach_Bip01_Pelvis myModel, valid
	
	AS_Attach_Hands myModel, valid
	
	AS_attach_feet myModel, valid
	
	attach_feet myModel, valid
	

	
end sub
'---------------------------------------------
' Model and Global SRT Match
'---------------------------------------------
function AS_model_and_Bip01_match(in_rigModel, out_valid)

	dim oRoot, PModel, PgSRT, RiggSRT

	set oRoot = Application.ActiveProject.ActiveScene.Root
	
	set PModel = oRoot.FindChild("CharacterStudioGuide")
	set PgSRT= oRoot.FindChild("CharacterStudioGuide.GlobalSRT")
	set RiggSRT = oRoot.FindChild(in_rigModel & ".Bip01")
	
	'match up the models
	rt_match PModel, in_rigModel
	
	'match up the globalSRT's
	rt_match PgSRT, RiggSRT
	
	out_valid = True
	
end function
'---------------------------------------------
' Attach Hips
'---------------------------------------------
function AS_Attach_hips (in_rigModel, out_valid)

	
	setuserpref("SI3D_CONSTRAINT_COMPENSATION_MODE"), 0
	SelectObj in_rigModel & ".Bip01_Pelvis", "BRANCH"
	MatchTransform , "CharacterStudioGuide.cog", siTrn
	
	
	'now match the top of the legs
	MatchTransform in_rigModel & ".LLegRoot", "CharacterStudioGuide.LLeg", siTrn
	MatchTransform in_rigModel & ".RLegRoot", "CharacterStudioGuide.RLeg", siTrn
		
	out_valid = True
		
end function
'---------------------------------------------
' Attach Upper Body
'---------------------------------------------
function AS_Attach_Bip01_Pelvis(in_rigModel, out_valid)
	
	MatchTransform in_rigModel & ".Bip01_Pelvis", "CharacterStudioGuide.SpineBase", siTrn
	MatchTransform in_rigModel & ".Spine", "CharacterStudioGuide.SpineBase", siTrn

	'match the bone lengths
	dim s1, s2, s3, s4

	s1 = GetValue("CharacterStudioGuide.bone_3.bone.length")
	s2 = GetValue("CharacterStudioGuide.bone_2.bone.length")
	s3 = GetValue("CharacterStudioGuide.bone_1.bone.length")
	s4 = GetValue("CharacterStudioGuide.bone37.bone.length")

	SetValue in_rigModel & ".Bip01_Spine.bone.length", s1
	SetValue in_rigModel & ".Bip01_Spine1.bone.length", s2
	SetValue in_rigModel & ".Bip01_Spine2.bone.length", s3
	SetValue in_rigModel & ".Bip01_Spine4.bone.length", s4

	'match the positions
	dim c1, c2, c3, c4
	c1 = ApplyCns ("Position", in_rigModel & ".Bip01_Spine1", "CharacterStudioGuide.Spine1")
	c2 = ApplyCns ("Position", in_rigModel & ".Bip01_Spine2", "CharacterStudioGuide.Spine2")
	c3 = ApplyCns ("Position", in_rigModel & ".Bip01_Spine4", "CharacterStudioGuide.Spine3")
	c4 = ApplyCns ("Position", in_rigModel & ".eff", "CharacterStudioGuide.RibCageEnd")

	refresh
	
	DeleteObj c1
	DeleteObj c2	
	DeleteObj c3
	DeleteObj c4

	dim b1,b2,b3,b4,b5,b6,b7,b8
	
	'head
	b1 = GetValue("CharacterStudioGuide.bone35.bone.length")
	b2 = GetValue("CharacterStudioGuide.bone36.bone.length")

	SetValue in_rigModel & ".Bip01_Neck1.bone.length", b1
	SetValue in_rigModel & ".Bip01_Head1.bone.length", b2
	
	c1 = ApplyCns ("Position", in_rigModel & ".Bip01_Neck1", "CharacterStudioGuide.RibCageEnd")
	c2 = ApplyCns ("Position", in_rigModel & ".Bip01_Head1", "CharacterStudioGuide.Neck")
	c3 = ApplyCns ("Position", in_rigModel & ".eff1", "CharacterStudioGuide.Head")
	
	refresh
	
	DeleteObj c1
	DeleteObj c2	
	DeleteObj c3
		
	'arms

	
	b3 = GetValue("CharacterStudioGuide.bone32.bone.length")
	b4 = GetValue("CharacterStudioGuide.bone33.bone.length")
	b5 = GetValue("CharacterStudioGuide.bone34.bone.length")
	b6 = GetValue("CharacterStudioGuide.bone31.bone.length")
	b7 = GetValue("CharacterStudioGuide.bone30.bone.length")	
	b8 = GetValue("CharacterStudioGuide.bone29.bone.length")
		
	
	SetValue in_rigModel & ".Bip01_R_Clavicle.bone.length", b3
	SetValue in_rigModel & ".Bip01_R_UpperArm.bone.length", b4
	SetValue in_rigModel & ".Bip01_R_Forearm.bone.length", b5
	SetValue in_rigModel & ".Bip01_L_Clavicle.bone.length", b6
	SetValue in_rigModel & ".Bip01_L_UpperArm.bone.length", b7
	SetValue in_rigModel & ".Bip01_L_Forearm.bone.length", b8
	
	
	
	c1 = ApplyCns ("Position", in_rigModel & ".RRootShoulder", "CharacterStudioGuide.RShoulderStart")
	c2 = ApplyCns ("Position", in_rigModel & ".RShoulderEff", "CharacterStudioGuide.RShoulder")
	c3 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Forearm", "CharacterStudioGuide.RElbow")
	c4 = ApplyCns ("Position", in_rigModel & ".RArmEff", "CharacterStudioGuide.RHand")
	
	c5 = ApplyCns ("Position", in_rigModel & ".LRootShoulder", "CharacterStudioGuide.LShoulderStart")
	c6 = ApplyCns ("Position", in_rigModel & ".LShoulderEff", "CharacterStudioGuide.LShoulder")
	c7 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Forearm", "CharacterStudioGuide.LElbow")
	c8 = ApplyCns ("Position", in_rigModel & ".LArmEff", "CharacterStudioGuide.LHand")	
	
	refresh

	Align_Arms in_rigModel 


	DeleteObj c1
	DeleteObj c2
	DeleteObj c3
	DeleteObj c4
	DeleteObj c5
	DeleteObj c6	
	DeleteObj c7	
	DeleteObj c8		

	out_valid = True
	
end function
'---------------------------------------------
' Attach Hands
'---------------------------------------------
function AS_Attach_Hands(in_rigModel, out_valid)



	out_valid = False
	dim c1
	
	'right hand

	'match the bone lengths
	dim b0, b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15
	
	b0 = GetValue("CharacterStudioGuide.bone62.bone.length")	
	
	b1 = GetValue("CharacterStudioGuide.bone46.bone.length")
	b2 = GetValue("CharacterStudioGuide.bone47.bone.length")
	b3 = GetValue("CharacterStudioGuide.bone48.bone.length")
	b4 = GetValue("CharacterStudioGuide.bone49.bone.length")
	b5 = GetValue("CharacterStudioGuide.bone50.bone.length")
	b6 = GetValue("CharacterStudioGuide.bone51.bone.length")
	b7 = GetValue("CharacterStudioGuide.bone52.bone.length")	
	b8 = GetValue("CharacterStudioGuide.bone53.bone.length")
	b9 = GetValue("CharacterStudioGuide.bone54.bone.length")
	b10 = GetValue("CharacterStudioGuide.bone55.bone.length")
	b11 = GetValue("CharacterStudioGuide.bone56.bone.length")
	b12 = GetValue("CharacterStudioGuide.bone57.bone.length")
	b13 = GetValue("CharacterStudioGuide.bone58.bone.length")
	b14 = GetValue("CharacterStudioGuide.bone59.bone.length")
	b15 = GetValue("CharacterStudioGuide.bone60.bone.length")
	
	
	SetValue in_rigModel & ".Bip01_R_Hand.bone.length", b0
	
	SetValue in_rigModel & ".Bip01_R_Finger0.bone.length", b1
	SetValue in_rigModel & ".Bip01_R_Finger01.bone.length", b2
	SetValue in_rigModel & ".Bip01_R_Finger02.bone.length", b3
	SetValue in_rigModel & ".Bip01_R_Finger1.bone.length", b4
	SetValue in_rigModel & ".Bip01_R_Finger11.bone.length", b5
	SetValue in_rigModel & ".Bip01_R_Finger12.bone.length", b6
	SetValue in_rigModel & ".Bip01_R_Finger2.bone.length", b7
	SetValue in_rigModel & ".Bip01_R_Finger21.bone.length", b8
	SetValue in_rigModel & ".Bip01_R_Finger22.bone.length", b9
	SetValue in_rigModel & ".Bip01_R_Finger3.bone.length", b10
	SetValue in_rigModel & ".Bip01_R_Finger31.bone.length", b11
	SetValue in_rigModel & ".Bip01_R_Finger31.bone.length", b12
	SetValue in_rigModel & ".Bip01_R_Finger4.bone.length", b13
	SetValue in_rigModel & ".Bip01_R_Finger41.bone.length", b14
	SetValue in_rigModel & ".Bip01_R_Finger42.bone.length", b15
	

	
	Rotate in_rigModel & ".Bip01_R_Hand", 0, 0, 0, siAbsolute, 70, siObj, siXYZ
	set c0 = ApplyCns ("Position", in_rigModel & ".RHandEffector", "CharacterStudioGuide.RHandEnd")	
	
	MatchTransform in_rigModel & ".RThumbRoot", "CharacterStudioGuide.RThumb1", siTrn
	MatchTransform in_rigModel & ".RIndexRoot", "CharacterStudioGuide.RIndex1", siTrn
	MatchTransform in_rigModel & ".RMiddleRoot", "CharacterStudioGuide.RMiddle1", siTrn
	MatchTransform in_rigModel & ".RRingRoot", "CharacterStudioGuide.RRing1", siTrn
	MatchTransform in_rigModel & ".RPinkyRoot", "CharacterStudioGuide.RPinky1", siTrn	


	dim c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16

	set c2 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger01", "CharacterStudioGuide.RThumb2")
	set c3 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger02", "CharacterStudioGuide.RThumb3")
	set c4 = ApplyCns ("Position", in_rigModel & ".RThumbEffector", "CharacterStudioGuide.RThumb4")

	set c5 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger11", "CharacterStudioGuide.RIndex2")
	set c6 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger12", "CharacterStudioGuide.RIndex3")
	set c7 = ApplyCns ("Position", in_rigModel & ".RIndexEffector", "CharacterStudioGuide.RIndex4")

	set c8 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger21", "CharacterStudioGuide.RMiddle2")
	set c9 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger22", "CharacterStudioGuide.RMiddle3")
	set c10 = ApplyCns ("Position", in_rigModel & ".RMiddleEffector", "CharacterStudioGuide.RMiddle4")


	set c11 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger31", "CharacterStudioGuide.RRing2")
	set c12 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger32", "CharacterStudioGuide.RRing3")
	set c13 = ApplyCns ("Position", in_rigModel & ".RRingEffector", "CharacterStudioGuide.RRing4")

	set c14 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger41", "CharacterStudioGuide.RPinky2")
	set c15 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Finger42", "CharacterStudioGuide.RPinky3")
	set c16 = ApplyCns ("Position", in_rigModel & ".RPinkyEffector", "CharacterStudioGuide.RPinky4")
	
	

	
	refresh
	
	DeleteObj c0	
	
	DeleteObj c2	
	DeleteObj c3
	DeleteObj c4
	DeleteObj c5	
	DeleteObj c6	
	DeleteObj c7
	DeleteObj c8	
	DeleteObj c9
	DeleteObj c10
	DeleteObj c11	
	DeleteObj c12
	DeleteObj c13 
	DeleteObj c14	
	DeleteObj c15
	DeleteObj c16

	'left hand

	'match the bone lengths
	
	b0 = GetValue("CharacterStudioGuide.bone62.bone.length")	
	
	b1 = GetValue("CharacterStudioGuide.bone.bone.length")
	b2 = GetValue("CharacterStudioGuide.bone15.bone.length")
	b3 = GetValue("CharacterStudioGuide.bone16.bone.length")
	b4 = GetValue("CharacterStudioGuide.bone19.bone.length")
	b5 = GetValue("CharacterStudioGuide.bone18.bone.length")
	b6 = GetValue("CharacterStudioGuide.bone17.bone.length")
	
	b7 = GetValue("CharacterStudioGuide.bone20.bone.length")	
	b8 = GetValue("CharacterStudioGuide.bone21.bone.length")
	b9 = GetValue("CharacterStudioGuide.bone22.bone.length")
	
	b10 = GetValue("CharacterStudioGuide.bone23.bone.length")
	b11 = GetValue("CharacterStudioGuide.bone24.bone.length")
	b12 = GetValue("CharacterStudioGuide.bone25.bone.length")
	
	b13 = GetValue("CharacterStudioGuide.bone26.bone.length")
	b14 = GetValue("CharacterStudioGuide.bone27.bone.length")
	b15 = GetValue("CharacterStudioGuide.bone28.bone.length")
	
	
	SetValue in_rigModel & ".Bip01_L_Hand.bone.length", b0
	
	SetValue in_rigModel & ".Bip01_L_Finger0.bone.length", b1
	SetValue in_rigModel & ".Bip01_L_Finger01.bone.length", b2
	SetValue in_rigModel & ".Bip01_L_Finger02.bone.length", b3
	SetValue in_rigModel & ".Bip01_L_Finger1.bone.length", b4
	SetValue in_rigModel & ".Bip01_L_Finger11.bone.length", b5
	SetValue in_rigModel & ".Bip01_L_Finger12.bone.length", b6
	SetValue in_rigModel & ".Bip01_L_Finger2.bone.length", b7
	SetValue in_rigModel & ".Bip01_L_Finger21.bone.length", b8
	SetValue in_rigModel & ".Bip01_L_Finger22.bone.length", b9
	SetValue in_rigModel & ".Bip01_L_Finger3.bone.length", b10
	SetValue in_rigModel & ".Bip01_L_Finger31.bone.length", b11
	SetValue in_rigModel & ".Bip01_L_Finger31.bone.length", b12
	SetValue in_rigModel & ".Bip01_L_Finger4.bone.length", b13
	SetValue in_rigModel & ".Bip01_L_Finger41.bone.length", b14
	SetValue in_rigModel & ".Bip01_L_Finger42.bone.length", b15
	


	set c0 = ApplyCns ("Position", in_rigModel & ".LHandEffector", "CharacterStudioGuide.LHandEnd")	

	refresh

	DeleteObj c0

	MatchTransform in_rigModel & ".LThumbRoot", "CharacterStudioGuide.LThumb1", siTrn
	MatchTransform in_rigModel & ".LIndexRoot", "CharacterStudioGuide.LIndex1", siTrn
	MatchTransform in_rigModel & ".LMiddleRoot", "CharacterStudioGuide.LMiddle1", siTrn
	MatchTransform in_rigModel & ".LRingRoot", "CharacterStudioGuide.LRing1", siTrn
	MatchTransform in_rigModel & ".LPinkyRoot", "CharacterStudioGuide.LPinky1", siTrn	


		

	set c2 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger01", "CharacterStudioGuide.LThumb2")
	set c3 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger02", "CharacterStudioGuide.LThumb3")
	set c4 = ApplyCns ("Position", in_rigModel & ".LThumbEffector", "CharacterStudioGuide.LThumb4")

	set c5 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger11", "CharacterStudioGuide.LIndex2")
	set c6 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger12", "CharacterStudioGuide.LIndex3")
	set c7 = ApplyCns ("Position", in_rigModel & ".LIndexEffector", "CharacterStudioGuide.LIndex4")

	set c8 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger21", "CharacterStudioGuide.LMiddle2")
	set c9 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger22", "CharacterStudioGuide.LMiddle3")
	set c10 = ApplyCns ("Position", in_rigModel & ".LMiddleEffector", "CharacterStudioGuide.LMiddle4")


	set c11 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger31", "CharacterStudioGuide.LRing2")
	set c12 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger32", "CharacterStudioGuide.LRing3")
	set c13 = ApplyCns ("Position", in_rigModel & ".LRingEffector", "CharacterStudioGuide.LRing4")

	set c14 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger41", "CharacterStudioGuide.LPinky2")
	set c15 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Finger42", "CharacterStudioGuide.LPinky3")
	set c16 = ApplyCns ("Position", in_rigModel & ".LPinkyEffector", "CharacterStudioGuide.LPinky4")
	
	refresh
	
	
	DeleteObj c2	
	DeleteObj c3
	DeleteObj c4
	DeleteObj c5	
	DeleteObj c6	
	DeleteObj c7
	DeleteObj c8	
	DeleteObj c9
	DeleteObj c10
	DeleteObj c11	
	DeleteObj c12
	DeleteObj c13 
	DeleteObj c14	
	DeleteObj c15
	DeleteObj c16

	out_valid = True
	
end function


'---------------------------------------------
' Attach Feet
'---------------------------------------------
function AS_attach_feet(in_rigModel, out_valid)

	out_valid = False

	'match the bone lengths
	dim b1,b2,b3,b4,b5,b6,b7,b8
	b1 = GetValue("CharacterStudioGuide.bone42.bone.length")
	b2 = GetValue("CharacterStudioGuide.bone43.bone.length")
	b3 = GetValue("CharacterStudioGuide.bone44.bone.length")
	b4 = GetValue("CharacterStudioGuide.bone45.bone.length")
	b5 = GetValue("CharacterStudioGuide.bone38.bone.length")
	b6 = GetValue("CharacterStudioGuide.bone39.bone.length")
	b7 = GetValue("CharacterStudioGuide.bone40.bone.length")	
	b8 = GetValue("CharacterStudioGuide.bone61.bone.length")
	
	SetValue in_rigModel & ".Bip01_R_Thigh.bone.length", b1
	SetValue in_rigModel & ".Bip01_R_Calf.bone.length", b2
	SetValue in_rigModel & ".Bip01_R_Foot.bone.length", b3
	SetValue in_rigModel & ".Bip01_R_Toe0.bone.length", b4
	SetValue in_rigModel & ".Bip01_L_Thigh.bone.length", b5
	SetValue in_rigModel & ".Bip01_L_Calf.bone.length", b6
	SetValue in_rigModel & ".Bip01_L_Foot.bone.length", b7
	SetValue in_rigModel & ".Bip01_L_Toe0.bone.length", b8
												
												
 	'match the foot postition
	setuserpref("SI3D_CONSTRAINT_COMPENSATION_MODE"), 0
	
	SelectObj in_rigModel & ".RLegRoot", "BRANCH"
	MatchTransform , "CharacterStudioGuide.RLeg", siTrn
	
	SelectObj in_rigModel & ".LLegRoot", "BRANCH"
	MatchTransform , "CharacterStudioGuide.LLeg", siTrn		
										
	set c1 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Calf", "CharacterStudioGuide.RKnee")
	set c2 = ApplyCns ("Position", in_rigModel & ".RFootEff", "CharacterStudioGuide.RFoot")
	set c3 = ApplyCns ("Position", in_rigModel & ".Bip01_R_Toe0", "CharacterStudioGuide.RHeel")
	set c4 = ApplyCns ("Position", in_rigModel & ".RToeEff", "CharacterStudioGuide.RToe")
	
	set c5 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Calf", "CharacterStudioGuide.LKnee")
	set c6 = ApplyCns ("Position", in_rigModel & ".LFootEff", "CharacterStudioGuide.LFoot")
	set c7 = ApplyCns ("Position", in_rigModel & ".Bip01_L_Toe0", "CharacterStudioGuide.LHeel")
	set c8 = ApplyCns ("Position", in_rigModel & ".LToeEff", "CharacterStudioGuide.LToe")
	refresh
	
	Align_Legs in_rigModel
		
	
	
	DeleteObj c1
	DeleteObj c2
	DeleteObj c3
	DeleteObj c4	
	DeleteObj c5
	DeleteObj c6
	DeleteObj c7
	DeleteObj c8	
		
end function
'---------------------------------------------
' FK Prep
'---------------------------------------------
function AS_fk_prep(in_model)
	
	setuserpref("SI3D_CONSTRAINT_COMPENSATION_MODE"), 1
	
	root_Alignment in_model, "SpineRoot"
	root_Alignment in_model, "Spine"	
	
	'shoulders
	root_Alignment in_model, "Bip01_Spine4"
	root_Alignment in_model, "Bip01_L_Finger22"	
	
	'hands
	root_Alignment in_model, "Bip01_R_Clavicle"	
		
		
	root_Alignment in_model, "Bip01_R_Finger12"	
	root_Alignment in_model, "Bip01_R_Finger02"	
	root_Alignment in_model, "RThumbEffector"	
	root_Alignment in_model, "Bip01_L_Finger41"	
	root_Alignment in_model, "Bip01_R_UpperArm"						
			
	root_Alignment in_model, "Bip01_L_Clavicle"

	root_Alignment in_model, "Bip01_L_UpperArm"	
	root_Alignment in_model, "Bip01_Neck1"	
	root_Alignment in_model, "LThumbEffector"	
	root_Alignment in_model, "Bip01_L_Finger02"
	root_Alignment in_model, "Bip01_L_Finger12"	
	
	setuserpref("SI3D_CONSTRAINT_COMPENSATION_MODE"), 0	
	
end function
'---------------------------------------------
' Root Alignment
'---------------------------------------------
function root_Alignment(in_model, in_rootstring)

	in_root = in_model.FindChild(in_rootstring)

	dim mySkeleton, BoneIndex, rroot, rbone, zero, newrot
	set mySkeleton =  GetSkeleton(in_root, BoneIndex ) 	
	
	set boneRot = XSIMath.CreateRotation
	set zero = XSIMath.CreateRotation
		
	mySkeleton(1).Kinematics.local.Transform.GetRotation boneRot
	boneRot.GetXYZAnglesValues x,y,z

	Rotate mySkeleton(0), 0, 0, XSIMath.RadiansToDegrees(z), siRelative, siLocal, siObj, siXYZ
	Rotate mySkeleton(0), 0, XSIMath.RadiansToDegrees(y), 0, siRelative, siLocal, siObj, siXYZ
	Rotate mySkeleton(0), XSIMath.RadiansToDegrees(x), 0, 0, siRelative, siLocal, siObj, siXYZ

	set boneTrans = mySkeleton(1).Kinematics.local.Transform
	zero.SetFromXYZAnglesValues 0,0,0 	
	
	boneTrans.SetRotation zero 
	
	mySkeleton(1).Kinematics.local.Transform = boneTrans 
		
end function
'---------------------------------------------
' Is Component 
'---------------------------------------------
function IsComponent(in_string)
	
	dim oRoot
	IsComponent = True
	set oRoot = Application.ActiveProject.ActiveScene.Root
	if typename(oRoot.FindChild(in_string)) = "Nothing" then IsComponent = False
	
end function
'--------------------------------------
' RT Match
'--------------------------------------
function rt_match (in_source, in_target)

	logmessage "SOURCE: " & in_source
	logmessage "TARGET: " & in_target	


	dim sx, sy, sz
	dim rx, ry, rz	
	dim tx, ty, tz
	
	sx = GetValue(in_source & ".kine.global.sclx")
	sy = GetValue(in_source & ".kine.global.scly")	
	sz = GetValue(in_source & ".kine.global.sclz")
	
	rx = GetValue(in_source & ".kine.global.rotx")
	ry = GetValue(in_source & ".kine.global.roty")
	rz = GetValue(in_source & ".kine.global.rotz")
	
	tx = GetValue(in_source & ".kine.global.posx")
	ty = GetValue(in_source & ".kine.global.posy")
	tz = GetValue(in_source & ".kine.global.posz")
	
	SelectObj in_target, "BRANCH"

	Scale , sx, sy, sz, siAbsolute, siGlobal, siObj, siXYZ
	Rotate , rx, ry, rz, siAbsolute, siGlobal, siObj, siXYZ
	Translate , tx, ty, tz, siAbsolute, siGlobal, siObj, siXYZ
	
end function
'-----------------------------------------------
' Set Prefangles
'-----------------------------------------------
function set_prefangles(in_bone)
	 
	' set the rotations to pref angles. 
	
	dim pref1, pref2, pref3, chain, bIndex
	
	set chain = GetSkeleton(in_bone, bIndex)
	
	'if not the root or the first bone set the pref of one back
	
	if bIndex > 1 then
		set pref1 = createobject("Sumatra\Scripting\Math\SIVector3")
		GetVector chain(bIndex -1) & ".kine.local.rot", pref1
		SetValue chain(bIndex - 1) &".joint.prefrotx"	, pref1.x
		SetValue chain(bIndex - 1) &".joint.prefroty"	, pref1.y
		SetValue chain(bIndex - 1) &".joint.prefrotz"	, pref1.z			
	end if	

	
	' if in_bone is not an effector set itself.
	
	if bIndex < chain.count - 1 then
		set pref2 = createobject("Sumatra\Scripting\Math\SIVector3")
		GetVector chain(bIndex) & ".kine.local.rot", pref2
		SetValue chain(bIndex) &".joint.prefrotx"	, pref2.x
		SetValue chain(bIndex) &".joint.prefroty"	, pref2.y
		SetValue chain(bIndex) &".joint.prefrotz"	, pref2.z	
	end if
	
	
	' if bIndex + 1 is beyond the size of the chain or and effector set
	' the pref angles
		
	if bIndex < chain.count - 2 then
		set pref3 = createobject("Sumatra\Scripting\Math\SIVector3")  
		GetVector chain(bIndex + 1) & ".kine.local.rot", pref3
		SetValue chain(bIndex + 1) &".joint.prefrotx"	, pref3.x
		SetValue chain(bIndex + 1) &".joint.prefroty"	, pref3.y
		SetValue chain(bIndex + 1) &".joint.prefrotz"	, pref3.z	
	end if	
	
	
end function

'-----------------------------------------------
' Get Vector
'-----------------------------------------------
function GetVector(in_Path, out_vector)

	'Create a vector containing the x y z component of the path
	'for example  GetValueVector(obj & "kine.local.pos", frame)
	Dim V
	set V = createobject("Sumatra\Scripting\Math\SIVector3")
	V.Set GetValue(in_Path & "x"),_
	GetValue(in_Path & "y"),_
	GetValue(in_Path & "z")
	set out_vector = V
end function
'---------------------------------------------
' Attach Feet
'---------------------------------------------
function attach_feet(in_rigModel, out_valid)

	out_valid = False
	

 	'match the foot postition
	setuserpref("SI3D_CONSTRAINT_COMPENSATION_MODE"), 0
	
	SelectObj in_rigModel & ".LeftFootParent", "BRANCH"
	MatchTransform , "CharacterStudioGuide.LFoot", siTrn
	
	SelectObj in_rigModel & ".RightFootParent", "BRANCH"
	MatchTransform , "CharacterStudioGuide.RFoot", siTrn											
		

	set root = ActiveProject.ActiveScene.Root								
	dim lFoot, lHeel, rFoot, rHeel
	
	'match the left rig
	
	set lFoot = root.FindChild(in_rigModel & ".LeftFoot")
	set lHeel = root.FindChild("CharacterStudioGuide.LHeel")
	align_foot lFoot, lHeel 
	
	SelectObj in_rigModel & ".heel", "BRANCH", True
	MatchTransform , "CharacterStudioGuide.LHeelPivot", siTrn
	MatchTransform in_rigModel & ".toe", "CharacterStudioGuide.LToe", siTrn
	SelectObj in_rigModel & ".mid", "BRANCH", True									
	MatchTransform , "CharacterStudioGuide.LHeel", siTrn																		
	MatchTransform in_rigModel & ".ankle", "CharacterStudioGuide.LFoot", siTrn	
	
	setuserpref("SI3D_CONSTRAINT_COMPENSATION_MODE"), 1
	refresh
	MatchTransform in_rigModel & ".LeftToe", "CharacterStudioGuide.LHeel", siTrn
	MatchTransform in_rigModel & ".LToeEff", "CharacterStudioGuide.LToe", siTrn	
	
	DeleteObj in_rigModel & ".LeftToeTarget.kine.posecns"
	MatchTransform in_rigModel & ".LeftToeTarget", "CharacterStudioGuide.LToe", siTrn
	ApplyCns "Pose", in_rigModel & ".LeftToeTarget", in_rigModel & ".LeftToe", True	
	
	DeleteObj in_rigModel & ".LToeEff.kine.poscns"	
	setuserpref("SI3D_CONSTRAINT_COMPENSATION_MODE"), 0	
	ApplyCns "Position", in_rigModel & ".LToeEff", in_rigModel & ".LeftToeTarget"
	
	

	'match the right rig
	set rFoot = root.FindChild(in_rigModel & ".RightFoot")
	set rHeel = root.FindChild("CharacterStudioGuide.RHeel")
	align_foot rFoot, rHeel 
	
	SelectObj in_rigModel & ".heel1", "BRANCH", True
	MatchTransform , "CharacterStudioGuide.RHeelPivot", siTrn
	MatchTransform in_rigModel & ".toe1", "CharacterStudioGuide.RToe", siTrn
	SelectObj in_rigModel & ".mid1", "BRANCH", True									
	MatchTransform , "CharacterStudioGuide.RHeel", siTrn																		
	MatchTransform in_rigModel & ".ankle1", "CharacterStudioGuide.RFoot", siTrn	
	
	setuserpref("SI3D_CONSTRAINT_COMPENSATION_MODE"), 1
	refresh
	MatchTransform in_rigModel & ".RightToe", "CharacterStudioGuide.RHeel", siTrn
	MatchTransform in_rigModel & ".RToeEff", "CharacterStudioGuide.RToe", siTrn	
	
	DeleteObj in_rigModel & ".RightToeTarget.kine.posecns"
	MatchTransform in_rigModel & ".RightToeTarget", "CharacterStudioGuide.RToe", siTrn
	ApplyCns "Pose", in_rigModel & ".RightToeTarget", in_rigModel & ".RightToe", True	
	
	DeleteObj in_rigModel & ".RToeEff.kine.poscns"	
	setuserpref("SI3D_CONSTRAINT_COMPENSATION_MODE"), 0	
	ApplyCns "Position", in_rigModel & ".RToeEff", in_rigModel & ".RightToeTarget"	
		

	out_valid = True
	
end function
'------------------------------------
' Align Foot
'------------------------------------
function align_foot(in_foot, in_heel)

	dim Pose1, dCnst, Pose2, yRot
	
	set Pose1 = in_foot.Kinematics.Global.Transform

	' Point the foot at the Toe
	set dCnst = in_foot.Kinematics.AddConstraint("Direction", in_heel, False)
	dCnst.dirx = 0
	dCnst.dirz = 1
	dCnst.upvct_active = True

	'now calculate the rotation to get there only rotating on the global y axis

	set Pose2 = in_foot.Kinematics.Global.Transform

	get_y_rotation Pose2, Pose1, yRot


	DeleteObj dCnst
	in_foot.Kinematics.Global.Transform = Pose1

	if yRot > 90 then yRot = yRot - 180
	if yRot < -90 then yRot = yRot + 180
	
	Rotate in_foot, 0, yRot, 0, siAbsolute, 65, siObj, siY

end function
'------------------------------------
' Get Y rotation
'------------------------------------
' We're not completly matching the rotations because 
' small variations of the foot angle up or down 
' shouldn't be used to rotate the body 
'....................................
function get_y_rotation(in_t1, in_t2, out_yRot)
	
	dim pushOut, gv1, gv2, gt1, gt2, cp ,angle, sroot
	
	set sRoot = Application.ActiveProject.ActiveScene.Root	
	
	set pushOut = xsimath.CreateVector3 
	pushOut.set 5,0,0
	
	'get the pushOut vector from local cordinates to a
	'global position
	
	set gv1 = XSIMath.MapObjectPositionToWorldSpace(in_t1,pushOut)  
	set gv2 = XSIMath.MapObjectPositionToWorldSpace(in_t2,pushOut) 
	
	'get the global translation to bring both vectors back to the origion
	set gt1 = xsimath.CreateVector3 
	set gt2 = xsimath.CreateVector3 
	in_t1.getTranslation gt1 
	in_t2.getTranslation gt2 

	gv1.SubInPlace gt1 
	gv2.SubInPlace gt2 
	
	'now project those vectors onto the xz plane	
	gv1.y = 0
	gv2.y = 0
	
	'now get the angle between them
	
	angle = gv2.Angle(gv1)  

	'get the cross product
	set cp = xsimath.CreateVector3 
	cp.cross gv1, gv2
	
	out_yRot = XSIMath.RadiansToDegrees(angle)

	if cp.y > 0 then out_yRot = out_yRot * -1 
	
end function
'-----------------------------------
' Handle Orientation
'-----------------------------------
function LegHandleOrientation(inSkeleton, inHandle, inDiagnostic)
	
	' get the normal for the Handle orientation plane
	' on this skeleton it should be the x+ axis of the 
	' up vector rotation handle
	
	set HandlePlaneStart = XSIMath.CreateVector3
	set HandleTransform = inHandle.parent.Kinematics.Global.Transform
	HandleTransform.GetTranslation HandlePlaneStart
	set ZAxis = XSIMath.CreateVector3
	ZAxis.set 0,-1,0
	HandleTransform.AddLocalTranslation ZAxis
	set HandlePlaneEnd = XSIMath.CreateVector3
	HandleTransform.GetTranslation HandlePlaneEnd 
	set HandlePlane = XSIMath.CreateVector3
	HandlePlane.sub HandlePlaneEnd, HandlePlaneStart



	'Also get the zero point for the HandleParent
	'in this case it's the -z axis (0,0,-1)
	
	set ZeroTrans = inHandle.parent.Kinematics.Global.Transform
	set ZeroOri = XSIMath.CreateVector3
	ZeroOri.set 0,0,-1
	ZeroTrans.AddLocalTranslation ZeroOri
	set ZeroEnd = XSIMath.CreateVector3
	ZeroTrans.GetTranslation ZeroEnd
	set ZeroVector = XSIMath.CreateVector3
	ZeroVector.Sub ZeroEnd, HandlePlaneStart
	

	'now get the plane for the upvector
	ProcessUpV inSkeleton, inDiagnostic, UpVPlane, FirstBoneYplus
	
	'now get the projection of the UpVectorPlane onto the HandlePlane
	set Projection = XSIMath.CreateVector3
	Projection.cross UpVPlane, HandlePlane


	' when taking the vector between the two planes you need to pick
	' the one that closest matches the Y plus vector coming off the
	' first bone.
	
	set NegProjection = XSIMath.CreateVector3
	NegProjection.Negate Projection
	
	ArmDot1 = Projection.Dot(FirstBoneYplus)
	ArmDot2 = NegProjection.Dot(FirstBoneYplus)
	

		
	if ArmDot1 < .0001 and ArmDot1 > -.0001 then exit function
	
	if ArmDot2 > ArmDot1 then
		set Projection = NegProjection
	end if
	
	
	'now get the angle between the Projection and the ZeroVector
	
	angle = ZeroVector.Angle(Projection)
	angle = XSIMath.RadiansToDegrees(angle)
	
	'find the sign of the angle
	
	set SignCp = xsimath.CreateVector3 
	SignCp.cross ZeroVector, Projection
	
	'the sign can be determined by the projection of SignCp onto the handlePlane
	
	if SignCp.Dot(HandlePlane) > 0 then
		sign = -1
	else	
		sign = 1
	end if
	
	SetValue inHandle & ".kine.local.roty", angle * sign
	
	
	if inDiagnostic = True then

		logmessage "DOT WITH PROJECTION: " & ArmDot1
		logmessage "DOT WITH NEG PROJECTION: " & ArmDot2

		set FirstBoneYplusDraw = XSIMath.CreateVector3
		FirstBoneYplus.AddInPlace HandlePlaneStart
		DrawLine2 HandlePlaneStart, FirstBoneYplus, "FirstBoneYplus"	

		HandlePlane.ScaleInPlace 10
		set HandlePlaneDraw = XSIMath.CreateVector3
		HandlePlaneDraw.add HandlePlaneStart, HandlePlane
		DrawLine2 HandlePlaneStart, HandlePlaneDraw, "HandlePlane"
		
		ZeroVector.ScaleInPlace 10
		set ZeroVectorDraw = XSIMath.CreateVector3
		ZeroVectorDraw.add HandlePlaneStart, ZeroVector
		DrawLine2 HandlePlaneStart, ZeroVectorDraw, "ZeroVector"
		
		set ProjectionDraw = XSIMath.CreateVector3
		ProjectionDraw.add HandlePlaneStart, Projection
		DrawLine2 HandlePlaneStart, ProjectionDraw, "Projection"					
		
	end if
	
	
end function
'-----------------------------------
' Process UpV
'-----------------------------------
function ProcessUpV(inSkel, inDiagnostic, outUpVectorPlane, outFirstBoneYplus)
	
	set inBone = inSkel(1)
	
	'first get the global transform
	
	set Trans = inBone.Kinematics.Global.Transform
	set startPoint = XSIMath.CreateVector3
	Trans.GetTranslation startPoint
	
	'get the bone length
	length = inBone.length.value
	
	'get the bone roll
	roll = GetValue(inBone & ".joint.roll")
	
	set EffPointAtTrans = RootToEffectorTrans(inSkel)
	

	'spin back the Trans as though it wasn't affected by the bone roll
	
	set Trans = RotationByReference(EffPointAtTrans, Trans , 0, -roll, 0)
	

	' get the first Bone y+ (used to figure out which side of the two plane
	' intesection is the right orientation).
 	set pos = XSIMath.CreateVector3
	pos.Set 0, 7, 0
	set Trans2 = XSIMath.CreateTransform
	Trans2.Copy Trans
	Trans2.AddLocalTranslation pos	
	set FirstBoneYplus= XSIMath.CreateVector3
	Trans2.GetTranslation FirstBoneYplus
	FirstBoneYplus.SubInPlace startPoint
	set outFirstBoneYplus = FirstBoneYplus

	' Get the position of the effector
	set UpVEnd = XSIMath.CreateVector3
	inSkel(inSkel.count - 1).Kinematics.Global.Transform.GetTranslation UpVEnd	

	'get the End the of first Bone Vector, in the space of the removed roll
	set Bpos = XSIMath.CreateVector3
	set BoneEnd = XSIMath.CreateVector3
	Bpos.Set length, 0, 0
	Trans.AddLocalTranslation Bpos	
	Trans.GetTranslation BoneEnd 

	'now get the two vectors for the plane of the bone
	set V1 = XSIMath.CreateVector3
	set V2 = XSIMath.CreateVector3
	
	V1.sub BoneEnd, startPoint
	V2.sub UpVEnd, startPoint
		
	set UpVectorPlane = XSIMath.CreateVector3
	UpVectorPlane.Cross V1, V2

	if inDiagnostic = True then
		
			DrawLine startPoint, BoneEnd, "BonePlane"
			DrawLine startPoint, UpVEnd, "UpVLine"	
			
			UpVectorPlane.NormalizeInPlace
			UpVectorPlane.ScaleInPlace 5
			set UpVectorPlaneOnArm = XSIMath.CreateVector3
			UpVectorPlaneOnArm.add startPoint, UpVectorPlane
			DrawLine startPoint, UpVectorPlaneOnArm, "UpVectorPlane"	
	end if
	
	set outUpVectorPlane = UpVectorPlane
	
end function
'-----------------------------------
' Root To Effector Trans
'-----------------------------------
function RootToEffectorTrans(inSkel)

	'Get a Transform Describing the root pointing towards the effector
	
	set inEff = inSkel(0)
	set inRoot = inSkel(inSkel.count - 1)
	
	set startAxis = XSIMath.CreateVector3	
	set endAxis = XSIMath.CreateVector3
	set Axis = XSIMath.CreateVector3

	inEff.Kinematics.Global.Transform.GetTranslation startAxis
	inRoot.Kinematics.Global.Transform.GetTranslation endAxis	
	Axis.sub startAxis, endAxis
		
	set PointAxis = XSIMath.CreateVector3	
	set cp  = XSIMath.CreateVector3	
	
	PointAxis.set 0,-1,0 
	angle = Axis.Angle(PointAxis)
	cp.cross PointAxis, Axis
	
	set Orot = XSIMath.CreateRotation	
	Orot.SetFromAxisAngle cp, angle
	
	set r1 = XSIMath.CreateVector3	
	Orot.GetXYZAngles r1
		
	'Create a Transform for the Return
	set returnTrans = XSIMath.CreateTransform
	returnTrans.SetTranslation endAxis
	returnTrans.SetRotation Orot

	set RootToEffectorTrans = returnTrans
	
end function
'-------------------------------------
' Rotation By Reference
'-------------------------------------
' inRef: the transform of Rotating Reference
' inObj: the transform to rotate in the space of inRef
' inX, inY, inZ: the rotation
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function RotationByReference(inRef, inObj, inX, inY, inZ)

	'Describe the Object in the Space of the Reference
	set RelativeObj = XSIMath.MapWorldPoseToObjectSpace(inRef, inObj)
	
	set rot = XSIMath.CreateRotation
	
	inX = XSIMath.DegreesToRadians(inX)
	inY = XSIMath.DegreesToRadians(inY)	
	inZ = XSIMath.DegreesToRadians(inZ)
		
	rot.SetFromXYZAnglesValues inX, inY, inZ
	
	RelativeObj.AddParentRotation rot
	
	'now put the position back into global space
	set returnTrans = XSIMath.MapObjectPoseToWorldSpace(inRef, RelativeObj)
	
	set RotationByReference = returnTrans 
	
end function
'-------------------------------------
' Draw Line
'-------------------------------------
function DrawLine(inStart, inEnd, inName)

	set myCurve = SICreateCurve ("crvlist", 1, 1)
	SISetCurvePoints myCurve, "("&inStart.x&","&inStart.y&","&inStart.z&"),("&inEnd.x&","&inEnd.y&","&inEnd.z&")", 0
	
	myCurve.name = inName
	myCurve.AddProperty("Display Property")
	myCurve.properties("display").wirecol.value = 507
	
end function
'-------------------------------------
' Draw Line 2
'-------------------------------------
function DrawLine2(inStart, inEnd, inName)

	set myCurve = SICreateCurve ("crvlist", 1, 1)
	SISetCurvePoints myCurve, "("&inStart.x&","&inStart.y&","&inStart.z&"),("&inEnd.x&","&inEnd.y&","&inEnd.z&")", 0
	
	myCurve.name = inName
	myCurve.AddProperty("Display Property")
	myCurve.properties("display").wirecol.value = 222
	
end function
'-------------------------------------
' Align Legs
'-------------------------------------
function Align_Legs(inModel)
	
	Diagnostic = False	
	
	set LThigh = inModel.FindChild("Bip01_L_Thigh")
	set LRoll = inModel.FindChild("LLegRoll")
	
	set MySkeleton =  GetSkeleton(LThigh, BoneIndex ) 	
	LegHandleOrientation MySkeleton, LRoll, Diagnostic 
		
	set RThigh = inModel.FindChild("Bip01_R_Thigh")
	set RRoll = inModel.FindChild("RLegRoll")
	
	set MySkeleton =  GetSkeleton(RThigh, BoneIndex ) 	
	LegHandleOrientation MySkeleton, RRoll, Diagnostic

end function
'-------------------------------------
' Align Arms
'-------------------------------------
function Align_Arms(inModel)
	
	Diagnostic = False	
	
	set LThigh = inModel.FindChild("Bip01_L_UpperArm")
	set LRoll = inModel.FindChild("LArmRoll")
	
	set MySkeleton =  GetSkeleton(LThigh, BoneIndex ) 	
	ArmHandleOrientation MySkeleton, LRoll, Diagnostic 
		
	set RThigh = inModel.FindChild("Bip01_R_UpperArm")
	set RRoll = inModel.FindChild("RArmRoll")
	
	set MySkeleton =  GetSkeleton(RThigh, BoneIndex ) 	
	ArmHandleOrientation MySkeleton, RRoll, Diagnostic

end function
'-----------------------------------
' Arm Handle Orientation
'-----------------------------------
function ArmHandleOrientation(inSkeleton, inHandle, inDiagnostic)
	
	' get the normal for the Handle orientation plane
	' on this skeleton it should be the x+ axis of the 
	' up vector rotation handle
	
	set HandlePlaneStart = XSIMath.CreateVector3
	set HandleTransform = inHandle.parent.Kinematics.Global.Transform
	HandleTransform.GetTranslation HandlePlaneStart
	set ZAxis = XSIMath.CreateVector3
	ZAxis.set 1,0,0
	HandleTransform.AddLocalTranslation ZAxis
	set HandlePlaneEnd = XSIMath.CreateVector3
	HandleTransform.GetTranslation HandlePlaneEnd 
	set HandlePlane = XSIMath.CreateVector3
	HandlePlane.sub HandlePlaneEnd, HandlePlaneStart



	'Also get the zero point for the HandleParent
	'in this case it's the -z axis (0,0,-1)
	
	set ZeroTrans = inHandle.parent.Kinematics.Global.Transform
	set ZeroOri = XSIMath.CreateVector3
	ZeroOri.set 0,0,-1
	ZeroTrans.AddLocalTranslation ZeroOri
	set ZeroEnd = XSIMath.CreateVector3
	ZeroTrans.GetTranslation ZeroEnd
	set ZeroVector = XSIMath.CreateVector3
	ZeroVector.Sub ZeroEnd, HandlePlaneStart
	

	'now get the plane for the upvector
	ProcessUpV inSkeleton, inDiagnostic, UpVPlane, FirstBoneYplus
	
	'now get the projection of the UpVectorPlane onto the HandlePlane
	set Projection = XSIMath.CreateVector3
	Projection.cross UpVPlane, HandlePlane


	' when taking the vector between the two planes you need to pick
	' the one that closest matches the Y plus vector coming off the
	' first bone.
	
	set NegProjection = XSIMath.CreateVector3
	NegProjection.Negate Projection
	
	ArmDot1 = Projection.Dot(FirstBoneYplus)
	ArmDot2 = NegProjection.Dot(FirstBoneYplus)
	

		
	if ArmDot1 < .0001 and ArmDot1 > -.0001 then exit function
	
	if ArmDot2 > ArmDot1 then
		set Projection = NegProjection
	end if
	
	
	'now get the angle between the Projection and the ZeroVector
	
	angle = ZeroVector.Angle(Projection)
	angle = XSIMath.RadiansToDegrees(angle)
	
	'find the sign of the angle
	
	set SignCp = xsimath.CreateVector3 
	SignCp.cross ZeroVector, Projection
	
	'the sign can be determined by the projection of SignCp onto the handlePlane
	
	if SignCp.Dot(HandlePlane) > 0 then
		sign = 1
	else	
		sign = -1
	end if
	
	SetValue inHandle & ".kine.local.rotx", angle * sign
	
	
	if inDiagnostic = True then

		logmessage "DOT WITH PROJECTION: " & ArmDot1
		logmessage "DOT WITH NEG PROJECTION: " & ArmDot2

		set FirstBoneYplusDraw = XSIMath.CreateVector3
		FirstBoneYplus.AddInPlace HandlePlaneStart
		DrawLine2 HandlePlaneStart, FirstBoneYplus, "FirstBoneYplus"	

		HandlePlane.ScaleInPlace 10
		set HandlePlaneDraw = XSIMath.CreateVector3
		HandlePlaneDraw.add HandlePlaneStart, HandlePlane
		DrawLine2 HandlePlaneStart, HandlePlaneDraw, "HandlePlane"
		
		ZeroVector.ScaleInPlace 10
		set ZeroVectorDraw = XSIMath.CreateVector3
		ZeroVectorDraw.add HandlePlaneStart, ZeroVector
		DrawLine2 HandlePlaneStart, ZeroVectorDraw, "ZeroVector"
		
		set ProjectionDraw = XSIMath.CreateVector3
		ProjectionDraw.add HandlePlaneStart, Projection
		DrawLine2 HandlePlaneStart, ProjectionDraw, "Projection"					
		
	end if
	
	
end function
'-----------------------------------













