'-------------------------------------------------------------
'-------------------------------------------------------------
'NAME:KeySets
'AUTHOR:Jonathan Katoe
'CONTACT:jonkatoe@optonline.net
'VERSION:1.0
'DESCRIPTION:Makes a new toolbar with buttons to key any
'object in the original selection individually by name.
'-------------------------------------------------------------
'-------------------------------------------------------------
KeySets
function KeySets

dim file
dim toolbar
dim command
dim path
dim oSel
dim oItem
dim scn
dim scnName
dim oFol1
dim oFol2
dim rc
dim selFile


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 oSel = CreateObject("XSI.collection")
set oSel = GetValue("SelectionList")

'---------Error Checking SelectionList
if oSel.count = 0 then
	application.statusbar = "Nothing Selected"
	exit function
else

	'---------Create Modal Dialog box
	set key_name = AddProp ("Custom_parameter_list",mySceneRoot, , "KeySet").Value("Value")
	SIAddCustomParameter key_name, "ToolbarName", siString, 0, 0, 0, 0, 0, 0.000, 0.000
	SIAddCustomParameter key_name, "Local", siBool, 1.000, 0.000, 1.000, 0, 0, 0.000, 0.000
	SIAddCustomParameter key_name, "Scaling", siBool, 0.000, 0.000, 1.000, 0, 0, 0.000, 0.000
	SIAddCustomParameter key_name, "Rotation", siBool, 0.000, 0.000, 1.000, 0, 0, 0.000, 0.000
	SIAddCustomParameter key_name, "Translation", siBool, 1.000, 0.000, 1.000, 0, 0, 0.000, 0.000
	SIAddCustomParameter key_name, "X", siBool, 1.000, 0.000, 1.000, 0, 0, 0.000, 0.000
	SIAddCustomParameter key_name, "Y", siBool, 1.000, 0.000, 1.000, 0, 0, 0.000, 0.000
	SIAddCustomParameter key_name, "Z", siBool, 1.000, 0.000, 1.000, 0, 0, 0.000, 0.000

	'---------Prompts user for input
	InspectObj mySceneRoot & "." & key_name,,"Setup KeySet Options",4

	'---------Error Checking Toolbar field
	tb = GetValue(key_name & ".ToolbarName")
	if tb = "" then
		DeleteObj mySceneRoot & ".KeySet"
		application.statusbar = "ToolbarName Undefined!"
		exit function
	end if
	
	'---------More Error Checking
	if err.number <> 0 then
		DeleteObj mySceneRoot & ".KeySet"
		application.statusbar = "Operation has been canceled."
		exit function
	else

		'---------Grab info from dialog box
		Local = GetValue(key_name & ".Local")
		scl = GetValue(key_name & ".Scaling")
		rot = GetValue(key_name & ".Rotation")
		pos = GetValue(key_name & ".Translation")

		x = GetValue(key_name & ".X")
		y = GetValue(key_name & ".Y")
		z = GetValue(key_name & ".Z")

		

		'---------Cleanup ppg
		DeleteObj mySceneRoot & ".KeySet"

		set scn = ActiveProject.activescene
		scnName = scn.name

		'---------Create Toolbar
		toolbar = CreateToolbar(tb)
		
		'---------Resolve Paths & Create file
		path = Application.InstallationPath(siUserPath)
		folder = "\Scripts\KeySets\"
		set fso = CreateObject("Scripting.FileSystemObject")
		if (fso.FolderExists(path & folder)) = 0 then
			set oFol1 = fso.CreateFolder(path & folder)		
		end if
		if (fso.FolderExists(path & folder & "\" & scnName)) = 0 then
			set oFol2 = fso.CreateFolder(path & folder & "\" & scnName)
		end if	
		file = path & folder & scnName & "\" & tb & ".vbs"	
		set selFile = fso.CreateTextFile(file, True)
		selFile.writeline("'Script Generated by KeySets script - Jonathan Katoe")
		selFile.close
	
		'---------Check which Coordinates system to key
		if Local <> 0 then
			CoordSys = ".kine.local"
		else
			CoordSys = ".kine.global"
		end if	
	
		'---------Cycle through SelectionList
		for each oItem in oSel
	
			'---------Add subroutimes to out file
			set selFile = fso.OpenTextFile(file, 8, True)
			selFile.WriteLine("sub " & oItem.name)
		
			if scl <> 0 then
				if x <> 0 then
					selFile.WriteLine("SaveKey " & chr(34) & oItem & CoordSys & ".sclx" & chr(34) & ", " & "GetValue(" & chr(34) & "PlayControl.Current" & chr(34) & ")" & ", GetValue(" & chr(34) & oItem & CoordSys & ".sclx" & chr(34) & ")")
				end if
				if y <> 0 then
					selFile.WriteLine("SaveKey " & chr(34) & oItem & CoordSys & ".scly" & chr(34) & ", " & "GetValue(" & chr(34) & "PlayControl.Current" & chr(34) & ")" & ", GetValue(" & chr(34) & oItem & CoordSys & ".scly" & chr(34) & ")")
				end if
				if z <> 0 then
					selFile.WriteLine("SaveKey " & chr(34) & oItem & CoordSys & ".sclz" & chr(34) & ", " & "GetValue(" & chr(34) & "PlayControl.Current" & chr(34) & ")" & ", GetValue(" & chr(34) & oItem & CoordSys & ".sclz" & chr(34) & ")")

				end if
			end if
		
			if rot <> 0 then
				if x <> 0 then
					selFile.WriteLine("SaveKey " & chr(34) & oItem & CoordSys & ".rotx" & chr(34) & ", " & "GetValue(" & chr(34) & "PlayControl.Current" & chr(34) & ")" & ", GetValue(" & chr(34) & oItem & CoordSys & ".rotx" & chr(34) & ")")
				end if
				if y <> 0 then
					selFile.WriteLine("SaveKey " & chr(34) & oItem & CoordSys & ".roty" & chr(34) & ", " & "GetValue(" & chr(34) & "PlayControl.Current" & chr(34) & ")" & ", GetValue(" & chr(34) & oItem & CoordSys & ".roty" & chr(34) & ")")
				end if
				if z <> 0 then
					selFile.WriteLine("SaveKey " & chr(34) & oItem & CoordSys & ".rotz" & chr(34) & ", " & "GetValue(" & chr(34) & "PlayControl.Current" & chr(34) & ")" & ", GetValue(" & chr(34) & oItem & CoordSys & ".rotz" & chr(34) & ")")
				end if
			end if

			if pos <> 0 then
				if x <> 0 then
					selFile.WriteLine("SaveKey " & chr(34) & oItem & CoordSys & ".posx" & chr(34) & ", " & "GetValue(" & chr(34) & "PlayControl.Current" & chr(34) & ")" & ", GetValue(" & chr(34) & oItem & CoordSys & ".posx" & chr(34) & ")")
				end if
				if y <> 0 then
					selFile.WriteLine("SaveKey " & chr(34) & oItem & CoordSys & ".posy" & chr(34) & ", " & "GetValue(" & chr(34) & "PlayControl.Current" & chr(34) & ")" & ", GetValue(" & chr(34) & oItem & CoordSys & ".posy" & chr(34) & ")")
				end if
				if z <> 0 then
					selFile.WriteLine("SaveKey " & chr(34) & oItem & CoordSys & ".posz" & chr(34) & ", " & "GetValue(" & chr(34) & "PlayControl.Current" & chr(34) & ")" & ", GetValue(" & chr(34) & oItem & CoordSys & ".posz" & chr(34) & ")")
				end if
			end if
		
			selFile.WriteLine("end sub")
			selFile.WriteLine(" ")
			selFile.Close
			logmessage file
		
			'---------Create commands and buttons then add them to a toolbar
			command = CreateScriptCommand(oItem.name & " Key" , oItem.name & "_key" , file, oItem.name, "Keys " & oItem.name)
			CreateToolbarButton toolbar, command
		next
	end if
end if
end function

