'-----------------------------------------------------------------------------------
' CreateObjectsForParticles Modified by Olivier Lelardoux 2002
'
' This script will create an object hierarchy needed to work with the 
' Particles_AttachObject scripted operator preset. 
'
'
' Notice: if this script fails on the ApplyOp command, it's likely because the 
' scripted operator preset isn't found (should have been placed under 
' Data/DSPresets/Operators) or because the associated spdl file hasn't been registered
' (normally it should have been registered upon install of XSI).
' To register the spdl manually, you may have to run this (make sure that the paths
' point to your install paths):
' spdlreg2 -D ...Application\bin\ophost.dll 
'  -P ...Application\bin\plugincore.dll 
'  -r ...Data\DSPresets\Operators\Particles_AttachObject.spdl
'
'
'-----------------------------------------------------------------------------------

set mySceneRoot = Application.ActiveProject.ActiveScene.Root
set myPannel = mySceneRoot.AddProperty("Custom_parameter_list", , "How much ?")
set myCopies = myPannel.AddParameter ("Copies", siInt4, , , , , , 1, 1, 1000, 1, 1000)
InspectObj myPannel, , "Object to Particle Olivier Lelardoux Version", siModal
myValue = myCopies.Value
DeleteObj myPannel

CreateObjectsForParticles myValue, "part", "cloud"

sub CreateObjectsForParticles( in_nobj, in_objname, in_cloudname )

	Dim l_root, l_objects, l_targets, l_objectsupport, l_object, l_dupli, l_target, l_op, l_portstr, buttonPressed, PickedElement

	call PickElement( "Object", "Select an Object", "Select an object", PickedElement, ButtonPressed )
		If ButtonPressed <> 0 Then
				SelectObj PickedElement
					ApplyCns "Position", PickedElement, "ParticleEmitter"	
		End if
		
	set l_root = GetPrim( "Null", in_objname )
	set l_objects = GetPrim( "Null", l_root.name & "_Objects", l_root )
	set l_targets = GetPrim( "Null", l_root.name & "_Targets", l_root )
	for i = 0 to in_nobj - 1
	
		set l_objectsupport = GetPrim( "Null", in_objname, l_objects )
		setValue l_objectsupport & ".kine.global.sclx", 0.3
		setValue l_objectsupport & ".kine.global.scly", 0.3
		setValue l_objectsupport & ".kine.global.sclz", 0.3
	
		set l_dupli = Duplicate( PickedElement, , 2, 1, 1, 0, 0, 1, 0, 1 )
		removecns l_dupli
		set l_object = l_dupli
		ParentObj l_objectsupport, l_object
				
		set l_target = GetPrim( "Null", l_objectsupport.name & "_dir", l_targets )
		setValue l_target & ".kine.global.sclx", 0.3
		setValue l_target & ".kine.global.scly", 0.3
		setValue l_target & ".kine.global.sclz", 0.3


		ApplyCns "Direction", l_objectsupport, l_target
	
		l_portstr = l_objectsupport & ".kine.global," & in_cloudname & ".cloud"
		set l_op = ApplyOp( "Particles_AttachObject", l_portstr )
		
		SetValue l_op & ".Active", 			True
		SetValue l_op & ".ReuseObjects", 		True
		SetValue l_op & ".InheritOrientation",	True
		SetValue l_op & ".InitialId", 		i		
		SetValue l_op & ".CurrentId", 		i		
		SetValue l_op & ".IsObject", 			True
				
		l_portstr = l_target & ".kine.global," & in_cloudname & ".cloud"
		set l_op = ApplyOp( "Particles_AttachObject", l_portstr )
		
		SetValue l_op & ".Active", 			True
		SetValue l_op & ".ReuseObjects", 		True
		SetValue l_op & ".InheritOrientation",	True
		SetValue l_op & ".InitialId", 		i		
		SetValue l_op & ".CurrentId", 		i		
		SetValue l_op & ".IsObject", 			False			
	next

end sub
