'/// BehaviorInstanceActors 1.0 for XSI 3.5+
'/// By Homam Bahnassi - In|Framez 2003
'--------------------------------------

set oSelection = Application.Selection

InstanceCrowd oSelection

function InstanceCrowd(oModelsCollection)

' Check if nothing is selected...
if oModelsCollection.Count = 0 then
	logmessage "InstanceCrowd: Nothing Selected..."
	InstanceCrowd = false
	exit function
end if

' Check if the selection contains anything other than model...
for each oModel in oModelsCollection
	if oModel.Type <> "#model" then
		logmessage "InstanceCrowd: Invaled Selection..."
		logmessage "InstanceCrowd: " & oModel & " is not a Model!"
		InstanceCrowd = false
		exit function
	end if
next

' Add Custom Pset under the SceneRoot for controlling the crowd offset...
set oRoot = ActiveProject.ActiveScene.Root
set oRootProps = oRoot.Properties
for each oProp in oRootProps
	if oProp = "CrowdOffset" then
		iUse = MsgBox("Exsiting PSet was found... use it",vbYesNo,"BehaviorInstanceActors")
		set OffsetPset = oProp
	end if
next

if iUse <> 6 then
	SIAddProp "Custom_parameter_list", oRoot , , "CrowdOffset", OffsetPset
	SIAddCustomParameter OffsetPset, "Time", siDouble, 10, -10000, 10000, , 5, -500, 500
end if

' Instantiate Selected Models and adds required offset expressions...
for each oModel in oModelsCollection
	' Get the current GlobalSRT...
	set oChildren = oModel.Children
	AnimatedChilds = 0
	for each oChild in oChildren
		if IsAnimated(oChild,sifCurveSource) = True then
			AnimatedChilds = AnimatedChilds+1
			set AnimatedChild = oChild
		end if
	next
	
	if AnimatedChilds > 1 then
		logmessage "More than one animated GlobalSRT..."
		DeleteObj OffsetPset
		InstanceCrowd = false
		exit function
	end if
	
	bMoveAnim AnimatedChild, oModel
	
	set oInstance = SIInstantiate (oModel, 1)
	
	setExpr oInstance & ".kine.local.posx", "at_frame( Fc - " & OffsetPset & ".Time," & oModel & ".kine.local.posx )"
	setExpr oInstance & ".kine.local.posy", "at_frame( Fc - " & OffsetPset & ".Time," & oModel & ".kine.local.posy )"
	setExpr oInstance & ".kine.local.posz", "at_frame( Fc - " & OffsetPset & ".Time," & oModel & ".kine.local.posz )"
	
	setExpr oInstance & ".kine.local.rotx", "at_frame( Fc - " & OffsetPset & ".Time," & oModel & ".kine.local.rotx )"
	setExpr oInstance & ".kine.local.roty", "at_frame( Fc - " & OffsetPset & ".Time," & oModel & ".kine.local.roty )"
	setExpr oInstance & ".kine.local.rotz", "at_frame( Fc - " & OffsetPset & ".Time," & oModel & ".kine.local.rotz )"
next
InstanceCrowd = true
end function

function bMoveAnim(oSrcObj,oDstObj)
	if typename(oSrcObj) = "Empty" or typename(oDstObj) = "Empty" then
		logmessage "bMoveAnim : Invaled Arguments..."
		bMoveAnim = false
		exit function
	end if
	CopyAllAnimation2 oSrcObj, siAnySource, siAllParam, True
	RemoveAllAnimation oSrcObj, 0, siUnspecified, siAnySource, siAllParam
	ResetTransform oSrcObj, siObj, siSRT, siXYZ
	PasteAllAnimation oDstObj, 0
	bMoveAnim = true
end function
