'
' Highlight - v0.2
'
' A Script that creates a surface constrained null that will
' act as specular highlight target, automaticaly driving
' a light ( or object for reflective effect ).
'
' A creation panel gives the choice of the type of light
' to be created.
'
' by Guy Rabiller (grabiller@3dvf.net), December 3rd, 2001
'
' Installation:
' Drag and Drop on Toolbar.
'               Command Name = "Highlight"
' Command Name for Scripting = "Highlight"
'
' * v0.2 - Update:
'	. Now in VBScript ( previously JScript )
'	. Display the Property Page ( surfcns ) right after Creation
'	. User can pick an Object ( for reflective effect )
'

createHighlight

Sub createHighlight

	'--- In case somethings goes wrong, just continue --------------------------
	'
	On Error Resume Next
	
	'--- Check the Selection ---------------------------------------------------
	'
	If Application.Selection.Count < 1 Then
		LogMessage "Nothing Selected", siError
		Exit Sub
	ElseIf Application.Selection.Count > 1 Then
		LogMessage "Select only 1 Surface Mesh ( Nurbs )", siError
		Exit Sub
	ElseIf Application.Selection.Item(0).Type <> "surfmsh" Then
		LogMessage "Select 1 Surface Mesh ( Nurbs )", siError
		Exit Sub
	End If

	'--- Retreive the Selected Surface Mesh Name -------------------------------
	'
	myObjName = Application.Selection.Item(0).Name
	
	'--- In case the user Change the name of the Scene Root,
	' find the Scene_Root object -----------------------------------------------
	'
	set mySceneRoot = EnumElements("Project", TRUE)
	set mySceneRoot = SIFilter(mySceneRoot, "Scene", TRUE )(0)
	set mySceneRoot = EnumElements( mySceneRoot , TRUE)
	set mySceneRoot = SIFilter(mySceneRoot, "SceneObject", TRUE)(0)
	
	'--- In case someone as already created,
	' put a custom parameter named "Highlight" ---------------------------------
	'
	set highlight_cp = AddProp ("Custom_parameter_list",mySceneRoot, , "Highlight").Value("Value")
	
	'--- Add the Parameters in the Custom parameter ----------------------------
	'
	SIAddCustomParameter highlight_cp, "Surface",        siString, 0.000, 0.000, 1.000, , siReadOnly, 0.000, 1.000
	SIAddCustomParameter highlight_cp, "light_LightBox", siBool,   0.000, 0.000, 1.000, , 0, 0.000, 1.000
	SIAddCustomParameter highlight_cp, "light_Neon",     siBool,   0.000, 0.000, 1.000, , 0, 0.000, 1.000
	SIAddCustomParameter highlight_cp, "light_Point",    siBool,   1.000, 0.000, 1.000, , 0, 0.000, 1.000
	SIAddCustomParameter highlight_cp, "light_Spot",     siBool,   0.000, 0.000, 1.000, , 0, 0.000, 1.000
	SIAddCustomParameter highlight_cp, "PickObject",     siBool,   0.000, 0.000, 1.000, , 0, 0.000, 1.000

	
	'--- Put the Selection Name to the Panel -----------------------------------
	'
	SetValue highlight_cp & ".Surface", myObjName
			
	'--- Opening the CustomParameter for the user to Interact with it ----------
	'
	InspectObj mySceneRoot & "." & highlight_cp,,"Create Highlight",siModal

	'--- Retreive the values ---------------------------------------------------
	'
	myLTypLBox  = GetValue(highlight_cp &".light_LightBox")
	myLTypNeon 	= GetValue(highlight_cp &".light_Neon")
	myLTypPoint = GetValue(highlight_cp &".light_Point")
	myLTypSpot	= GetValue(highlight_cp &".light_Spot")
	myPObject	= GetValue(highlight_cp &".PickObject")
	
	'--- At the end we do a simple cleanup of the Custom Parameter -------------
	'
	DeleteObj mySceneRoot & "." & highlight_cp
	
	'--- If user clicked 'OK' ( if 'Cancel', Err.Number = 1223 ) ---------------
	'
	if Err.Number = 0 then
	
		'--- Create the Surface Constrained Null ( the Highlight repere ) ------
		'
		hl = myObjName & "_highlight"
		GetPrim "Null", hl
		CopyPaste hl, , myObjName, 1
		ApplyCns "Surface", hl, myObjName
		SetValue hl & ".kine.surfcns.tangent", true
		SetValue hl & ".kine.surfcns.upvct_active", true
		SetValue hl & ".kine.surfcns.posu", 0.500
		SetValue hl & ".kine.surfcns.posv", 0.500
	
		'--- Create the Camera Constrained Null 
		' ( symmetry repere for the highlight null ) ---------------------------
		'
		hlcam = hl & "_camCNS"
		GetPrim "Null", hlcam
		SetValue hlcam & ".visibility.viewvis", false

		'--- if I use directly hl and hlcam, it doesn't work :/ ----------------
		' CopyPaste hlcam, , hl, 1
		'
		CopyPaste myObjName & "_highlight_camCNS", , myObjName & "_highlight", 1
		ApplyCns "Position", hlcam, "Camera"
	
		'--- Create the Null Constraint for the Light(s) -----------------------
		'
		hllight = hl & "_lightCNS"
		GetPrim "Null", hllight
		SetValue hllight & ".visibility.viewvis", false

		'--- if I use directly hl and hllight, it doesn't work :/ --------------
		' CopyPaste hllight, , hl, 1
		'
		CopyPaste myObjName & "_highlight_lightCNS", , myObjName & "_highlight", 1
	
		'--- Set Expression that will mirror the Camera position
		' to the Light's Constraint from the Highlight Null --------------------
		'
		SetExpr hllight & ".kine.local.posx", "- " & hlcam & ".kine.local.posx"
		SetExpr hllight & ".kine.local.posy",        hlcam & ".kine.local.posy"
		SetExpr hllight & ".kine.local.posz", "- " & hlcam & ".kine.local.posz"
		
		'--- Deal with Spot Light ----------------------------------------------
		'
		If myLTypSpot = True Then
			hlspot = hl & "_spot"
			GetPrimLight "Spot.Preset",hlspot
			SetValue hlspot & "_Root.visibility.viewvis", false
			SetValue hlspot & "_Interest.visibility.viewvis", false
			CopyPaste hlspot & "_Root", , myObjName & "_highlight", 1
			ApplyCns "Position", hlspot & "_Interest", hl
			ApplyCns "Position", hlspot, hllight
		End If
		
		'--- Deal with Light Box -----------------------------------------------
		'
		If myLTypLBox = True Then
			hlbox = hl & "_lbox"
			GetPrimLight "Light_Box.Preset",hlbox
			SetValue hlbox & "_Root.visibility.viewvis", false
			SetValue hlbox & "_Interest.visibility.viewvis", false
			CopyPaste hlbox & "_Root", , myObjName & "_highlight", 1
			ApplyCns "Position", hlbox & "_Interest", hl
			ApplyCns "Position", hlbox, hllight
		End If
		
		'--- Deal with Point Light ---------------------------------------------
		'
		If myLTypPoint = True Then
			hlpnt = hl & "_point"
			GetPrimLight "Point.Preset",hlpnt
			CopyPaste hlpnt, , myObjName & "_highlight", 1
			ApplyCns "Position", hlpnt, hllight
		End If
		
		'--- Deal with Neon Light ----------------------------------------------
		'
		If myLTypNeon = True Then
			hlneon = hl & "_neon"
			GetPrimLight "Neon.Preset",hlneon
			CopyPaste hlneon, , myObjName & "_highlight", 1
			ApplyCns "Position", hlneon, hllight
			ApplyCns "Direction", hlneon, hl
			SetValue  hlneon & ".kine.dircns.dirz", 1.000
			SetValue  hlneon & ".kine.dircns.dirx", 0.000
			SetValue  hlneon & ".kine.dircns.upvct_active", true
		End If
		
		'--- Deal with Object --------------------------------------------------
		'
		If myPObject = True Then
			PickElement "object", "Select Object", "", mySObject, button
			If button = 1 Then
				ApplyCns "Position", mySObject, hllight
			End If
		End If
		
		SelectObj hl
		
		LogMessage myObjName & " Highlighted :-)"
		
		InspectObj myObjName & "_highlight.kine.surfcns", , ,siLock
	
	End If
	
End Sub
