'-------------------------------------------------------------
'-------------------------------------------------------------
'NAME:RenderSets
'AUTHOR:Jonathan Katoe
'CONTACT:jonkatoe@optonline.net
'VERSION:2.0
'DESCRIPTION:Generates a batch file for exported
'-mi2 files per pass. This version supports command
'-line options as well.
'FIXES:Compatibility with version 2.x and a bug which caused
'-launched renders to fail.<---bad thing
'-------------------------------------------------------------
'-------------------------------------------------------------
RenderSets
function RenderSets

dim mySceneRoot
dim mr
dim env
dim outfile
dim pass
dim passlist
dim startfr
dim endfr
dim mi2files
dim path
dim folder
dim oFol1
dim oFol2
dim passNB
dim params

set mySceneRoot = EnumElements("Project", TRUE)
set mySceneRoot = SIFilter(mySceneRoot, "Scene", TRUE )(0)
set mySceneRoot = EnumElements( mySceneRoot , TRUE)
set mySceneRoot = SIFilter(mySceneRoot, "SceneObject", TRUE)(0)

mr = Application.InstallationPath( siFactoryPath ) 
mr = mr & "\Application\bin\"
env = "call " & mr & "setenv.bat"

'---------Check application version
if left(application.version, 1) = 2 then
	mr = "call " & mr & "ray3.exe"
else
	mr = "call " & mr & "ray2.exe"
end if

set passlist = GetValue("Passes.List")
passNB = 0

'---------Create Options to select a pass via checkbox
set render_name = AddProp ("Custom_parameter_list",mySceneRoot, , "Render").Value("Value")
for each pass in passlist
	SIAddCustomParameter render_name, pass.name, siBool, 1.000, 0.000, 1.000, 0, 0, 0.000, 0.000
next
SIAddCustomParameter render_name, "CommandLineOptions", siString, 0, 0, 0, 0, 0, 0.000, 0.000
SIAddCustomParameter render_name, "ExportMI2files", siBool, 1.000, 0.000, 1.000, 0, 0, 0.000, 0.000

'---------Prompts user for input
InspectObj mySceneRoot & "." & render_name,,"Setup Export Options",4

if err.number <> 0 then
	DeleteObj mySceneRoot & ".Render"
	exit function
else
	on error resume next
	export = GetValue(render_name & ".ExportMI2files")
	params = GetValue(render_name & ".CommandLineOptions")

	'---------Cycle through all passes
	for each pass in passlist
	
		startfr = GetValue(pass & ".RenderOptions.StartFrame")
		endfr = GetValue(pass & ".RenderOptions.EndFrame")
		mi2files = GetValue(pass & ".RenderOptions.ExportMI2FileName")
	
		'---------Pass needs to be set to 0 for correct processing
		SetCurrentPass passlist.items(0)
	
		if GetValue(render_name & "." & pass.name) <> 0 then
				
			'---------Exports mi2 file for current pass in loop
			SetCurrentPass pass
			if export <> 0 then
				ExportMI2File
			end if
			
			'---------Set paths and invoke FSO
			path = Application.InstallationPath(siUserPath)
			if left(application.version, 1) = 2 then
				folder = "\data\Scripts\RenderSets\"
			else
				folder = "\Scripts\RenderSets\"
			end if
			set fso = CreateObject("Scripting.FileSystemObject")
			
			'---------Validate folder paths for the RenderSets folder and <Scene> folder beneath it
			if (fso.FolderExists(path & folder)) = 0 then
				set oFol1 = fso.CreateFolder(path & folder)		
			end if
				
			if (fso.FolderExists(path & folder & ActiveProject.activescene)) = 0 then
				set oFol2 = fso.CreateFolder(path & folder & "\" & ActiveProject.activescene)
			end if	
		
			'---------Output file for current pass in loop		
			file = path & folder & ActiveProject.activescene & "\" & pass.name & ".bat"
		
			'---------Outputs individual scripts for each pass
			set outfile = fso.CreateTextFile(file, True)
			outfile.Writeline(env)
			for i = startfr to endfr
				outfile.WriteLine(mr & " " & mi2files & "." & i & ".mi2" & " " & params)
			next
		end if
	next
end if
'---------Cleanup Props
DeleteObj mySceneRoot & ".Render"
end Function




