'/// MergeStaticClips: ver 1.0 for XSI 3.5+
'/// By Homam Bahnassi - In|Framez 2002
'--------------------------------------

'--- Get the current selected ActionSources
set oInputSources = Application.Selection

GetTargetSource oTargetSource, oInputSources

MergeStaticSources oInputSources, oTargetSource

function GetTargetSource(oResultSource,SourceCollection)
if SourceCollection.Count = 0 then
	oResultSource = "NULL"
	exit function
end if
'--- Ask to merge to an existing source or create a new source...
iAskDestination = MsgBox("Merge selected sources to a new source or to an exsiting one?", vbYesNoCancel, "Merge Static Sources")
if iAskDestination = 6 then 'Yes... merge to a new source
	'--- Specify the new source name...
	sSourceName = InputBox("Enter a name for the new source","Merge Static Sources","NewSource")
	if sSourceName = "" Then
		logmessage "MergeStaticSources: Cancelled by User"
		oResultSource = "NULL"
		exit function
	end if
	
	'--- Create destination source...
	set oModel = SourceCollection(0).Model
	set oResultSource = oModel.AddActionSource( sSourceName )

elseif iAskDestination = 7 then 'No... Merge to an existing source
	'--- Pick destination source...
	call PickElement( , "Select Destination Source", ,oResultSource, iDstButton ) 
	if iDstButton = 0 Then
		logmessage "MergeStaticSources: Cancelled by User"
		oResultSource = "NULL"
		exit function
	end if
	if oResultSource.type <> "Action" then
		logmessage "MergeStaticSources: Target is not action source..."
		oResultSource = "NULL"
		logmessage "MergeStaticSources: Exiting..."
		exit function
	end if
	
	'--- Analyse destination source...
	'set oActionItemsDst = oResultSource.SourceItems
	'/* logmessage "number of items in " & oActionSourceDst.name & " = " & oActionItemsDst.count
	'logmessage "Dumbing Destination Data..."
	'for j=0 to oActionItemsDst.count-1
	'	logmessage oActionItemsDst(j).target
	'next
	'logmessage "---------------------------" '*/ Debug Info

else
	logmessage "MergeStaticSources: Cancelled by User"
	oResultSource = "NULL"
	exit function
end If
end function

'-------------------------------------------------------
function MergeStaticSources(oInputSources, oDestSource)

MergeStaticSources = false

if oInputSources.count = 0 then
	logmessage "MergeStaticSources: Action source(s) required..."
	exit function
end if
for each ActionSource in oInputSources
	if ActionSource.Type <> "Action" then
		logmessage "MergeStaticSources: " & ActionSource.name & " is not action source..."
		logmessage "MergeStaticSources: Exiting..."
		exit function
	end if
next

if oDestSource = "NULL" then
	logmessage "MergeStaticSources: Destination source required..."
	exit function
end if

'--- analyse selected action sources... 
for each oActionSource in oInputSources
	set oActionItems = oActionSource.SourceItems
	'logmessage "number of items in " & oActionSource.name & " = " & oActionItems.count 'Debug Info
	'logmessage "Dumbing Sorce " & oActionSource.name & " Data..."
	redim target_value(oActionItems.count)
	for i=0 to oActionItems.count-1
		sTarget = oActionSource.Model & "." & oActionItems(i).target
		Target_value(i) = GetValue (sTarget)
		logmessage oActionItems(i).target & " = " & target_value(i)
	next
	'logmessage "---------------------------"
	
	'--- merges the selected clips into the target clip...
	for i=0 to oActionItems.count-1
		set oDestItem = oDestSource.AddSourceItem(oActionItems(i).target)
		call oDestItem.SetAsStatic( target_value(i) )
	next
next

MergeStaticSources = true

end function