' Instanced Texture Projections
' by misner June 2001

' for 3.X people who miss implicit texture
' inheritence

option explicit
create_linked_projections
'-----------------------------------------
' Create Linked Projections
'-----------------------------------------
sub create_linked_projections
	
	dim sel, source, valid, proj, localMat, groupMat
	
	set sel = GetValue("SelectionList")
	if sel.count = 0 then exit sub
	
	pick_source source, valid
	if valid = False then exit sub
	
	get_dialogue localMat, groupMat, valid
	if valid = False then exit sub
	
	get_projections source, proj
	
	copy_projections proj, sel
	
end sub
'----------------------------------------
' Pick Source
'----------------------------------------
function pick_source(out_pick, out_valid)

	dim button, pick
	out_valid = True
	
	ActivateObjectSelTool
	PickElement "object", "Select Source", , pick, button
	if button = 0 then 
		out_valid = False
		exit function
	end if 
	
	Set out_pick = pick 
	
end function
'---------------------------------------------
' Get Dialogue
'---------------------------------------------
function get_dialogue(out_localMat, out_groupMat, out_valid)
	
	dim SceneRoot, DialPath, mysliders
	
	out_valid = False
	
	Set SceneRoot = application.activeproject.activescene.root
	set DialPath = AddProp("Custom_parameter_list", SceneRoot, , "Instance Texture Projections").Value("Value")
	mysliders = SceneRoot & "." & DialPath

	SIAddCustomParameter mysliders , "Copy_Materials_Locally", siBool, 1.000, 0.000, 1.000, , siPersistable+siSilent, 0.000, 1.000
	SIAddCustomParameter mysliders , "Copy_Materials_Using_Groups", siBool, 0.000, 0.000, 1.000, , siPersistable+siSilent, 0.000, 1.000
			
	On Error Resume Next
	inspectobj mysliders ,,,siModal
	
	if Err.Number <> 0 then
		DeleteObj mysliders 
		exit function
	end if
	
	out_localMat = GetValue( mysliders & ".Copy_Materials_Locally" )
	out_groupMat = GetValue( mysliders & ".Copy_Materials_Using_Groups" )
	
	DeleteObj mysliders 
	out_valid = True

end function
'---------------------------------------------
' Get Projections
'---------------------------------------------
function get_projections (in_obj, out_proj)
	
	dim pro
	set out_proj = SIGetTextureUVDomains(in_obj)
	for each pro in out_proj
		addProp "Display Property", pro
		SetValue pro & ".display.wirecol", 380
	next
	

end function
'----------------------------------------
' Copy Material
'----------------------------------------
function copy_material (in_target, in_source)

	DeleteObj in_target.Material
	copy in_source.Material
	paste in_target

	Refresh

end function
'---------------------------------------------
' Copy Projections
'---------------------------------------------
function copy_projections (in_proj, in_sel)

	dim pro, elem, swapUV, projType, projPlane, Targetproj
	
	for each pro in in_proj
	
		if pro.type = "Control Object" then
		
			swapUV = GetValue( pro & ".Control Object.Swapuv") 
			for each elem in in_sel
		
				'if it NURBS UV transfering to poly apply a spherical
				if elem.type = "polymsh" then
					CreateProjection elem    , "siTxtSpherical", "siTxtDefaultSpherical", , "Texture_Projection14", True
	
				else

					' Copy NURBS UV to NURBS UV

 					logmessage selection(0).type
					
					' 1) create and copy projection
					CreateProjection elem , "siTxtUV", "siTxtDefaultPlanarXY", , "Texture_Projection", True
					'get the most recent texture projection (createProjection doesn't return it)
					set Targetproj = SIGetTextureUVDomains(elem)
					Targetproj = Targetproj(Targetproj.count - 1)
					
					' 2) copy the settings
					SetValue Targetproj & ".Control Object.Swapuv", swapUV
					
					'3) set a linking expression
					CreateExpression Targetproj, pro
					
					SetValue pro & ".display.wirecol", 380
			
				end if
				
			next
		
		else
			projType = GetValue( pro & ".Texture Support.projtype") 
			projPlane = GetValue( pro & ".Texture Support.projplane") 		
			swapUV = GetValue( pro & ".Texture Support.Swapuv") 
		
			for each elem in in_sel
				 '1) create and copy projection
		 
				CreateProjection elem, "siTxtPlanarXZ", "siTxtDefaultPlanarXZ", , "Texture_Projection", True
				'get the most recent texture projection (createProjection doesn't return it)
				set Targetproj = SIGetTextureUVDomains(elem)
				Targetproj = Targetproj(Targetproj.count - 1)
				
				' 2) copy the settings
				logmessage elem & ".Texture Support.projtype"
				SetValue Targetproj & ".Texture Support.projtype", projType
				SetValue Targetproj & ".Texture Support.projplane", projPlane
				SetValue Targetproj & ".Texture Support.Swapuv", swapUV
											
				'3) set a linking expression
				CreateExpression Targetproj, pro
			next
		end if 
	
	
	next


'logmessage selection(0).type
'SetValue "Texture_Support.Texture Support.projtype", 3.000
'SetValue "Texture_Support2.Texture Support.projplane", 0.000
'SetValue "Texture_Support2.Texture Support.Swapuv", False

'	"Control Object" = NURBS UV
'	"Texture Support" = Planar, ect.
'SetValue "Texture_Support1.Control Object.Swapuv", False


	
end function
'---------------------------------------------
' Create Expressions
'---------------------------------------------
function CreateExpression(in_target, in_source)


	SetExpr in_target & ".kine.local.posx", in_source & ".kine.local.posx"
	SetExpr in_target & ".kine.local.posy", in_source & ".kine.local.posy"
	SetExpr in_target & ".kine.local.posz", in_source & ".kine.local.posz"

	SetExpr in_target & ".kine.local.rotx", in_source & ".kine.local.rotx"
	SetExpr in_target & ".kine.local.roty", in_source & ".kine.local.roty"
	SetExpr in_target & ".kine.local.rotz", in_source & ".kine.local.rotz"	
	
	SetExpr in_target & ".kine.local.sclx", in_source & ".kine.local.sclx"
	SetExpr in_target & ".kine.local.scly", in_source & ".kine.local.scly"
	SetExpr in_target & ".kine.local.sclz", in_source & ".kine.local.sclz"
		
end function
'---------------------------------------------

