'####################################################################
'##                                                                ##
'##  Constraintion works great for imported GoWithTheFlow systems  ##
'##                                                                ##
'##         Constrains objects which end in the same number        ##
'##                                                                ##
'##     bull@thepentagon.com courtesy Orly Productions Ltd, NZ     ##
'##          Bad coders don't notate, long live Rockathon!         ##
'##                                                                ##
'####################################################################

dim Source, Target
dim SourceList, SourceListJoin
dim ObjList, ObjNumber
dim TargetArray(2), SourceArray(2)
dim SourceName, TargetName, TargetNameJoin
dim SourceNumber, TargetNumber

On Error Resume Next

set mySceneRoot = EnumElements("Project", TRUE)
set mySceneRoot = SIFilter(mySceneRoot, "Scene", TRUE )(0)
set mySceneRoot = EnumElements( mySceneRoot , TRUE)
set mySceneRoot = SIFilter(mySceneRoot, "SceneObject", TRUE)(0)

set constraintion_name = AddProp ("Custom_parameter_list",mySceneRoot, , "Constraintion").Value("Value")

SIAddCustomParameter constraintion_name, "Source", siString, 0.000, 0.000, 1.000, , 0, 0.000, 0.000
SIAddCustomParameter constraintion_name, "Target", siString, 0.000, 0.000, 1.000, , 0, 0.000, 0.000
SIAddCustomParameter constraintion_name, "Direction", siBool, 0.000, 0.000, 1.000, , 0, 0.000, 0.000
SIAddCustomParameter constraintion_name, "Position", siBool, 0.000, 0.000, 1.000, , 0, 0.000, 0.000
SIAddCustomParameter constraintion_name, "Orientation", siBool, 0.000, 0.000, 1.000, , 0, 0.000, 0.000

InspectObj mySceneRoot & "." & constraintion_name,,"Constraintion",4

Source = GetValue(constraintion_name &".Source")
Target = GetValue(constraintion_name &".Target")

SourceArray(0) = Source
SourceArray(1) = "*"
SourceListJoin = Join(SourceArray)
SourceList = Replace(SourceListJoin, " ", "")

SelectObj SourceList
set Objlist = GetValue("SelectionList")

	if GetValue(constraintion_name &".Direction") = TRUE then
		for each obj in ObjList
			SourceName = Obj
			ObjNumber = Len(SourceName)
			SourceNumber = Len(Source)
			TargetNumber = Right(SourceName, ObjNumber - SourceNumber)
			TargetArray(0) = Target
			TargetArray(1) = TargetNumber
			TargetNameJoin = Join(TargetArray)
			TargetName = Replace(TargetNameJoin, " ", "")
			ApplyCns "Direction", SourceName, TargetName
		next
	end if

	if GetValue(constraintion_name &".Position") = TRUE then
		for each obj in ObjList
			SourceName = Obj
			ObjNumber = Len(SourceName)
			SourceNumber = Len(Source)
			TargetNumber = Right(SourceName, ObjNumber - SourceNumber)
			TargetArray(0) = Target
			TargetArray(1) = TargetNumber
			TargetNameJoin = Join(TargetArray)
			TargetName = Replace(TargetNameJoin, " ", "")
			ApplyCns "Position", SourceName, TargetName
		next
	end if

	if GetValue(constraintion_name &".Orientation") = TRUE then
		for each obj in ObjList
			SourceName = Obj
			ObjNumber = Len(SourceName)
			SourceNumber = Len(Source)
			TargetNumber = Right(SourceName, ObjNumber - SourceNumber)
			TargetArray(0) = Target
			TargetArray(1) = TargetNumber
			TargetNameJoin = Join(TargetArray)
			TargetName = Replace(TargetNameJoin, " ", "")
			ApplyCns "Orientation", SourceName, TargetName
		next
	end if

DeleteObj mySceneRoot & "." & constraintion_name
DeselectAll
