//Byron Nash 12/2005
//byronnash@gmail.com

//Exports all passes to After Effects. 

//Select the passes you would like to export or select nothing for all passes
//The script sends a script command to After Effects using the "-s" parameter
//the command imports the rendered sequence, taking into account Pixel Aspect Ratio, Field Dominance, and frame rate
//In addition, it sets the imported sequence's matte to Premultiplied over black.

//It's best to have AE already open but not necessary. 
//
//
//
{
// In batch mode buttonPressed is always "siMsgYes"
var buttonPressed = XSIUIToolkit.Msgbox( "Would you like to write a batch file for later use instead of sending the command to After Effects now?\n \n YES = Write a .bat file for later execution \n NO = Send the passes to After Effects now", 
          siMsgYesNoCancel | siMsgQuestion | siMsgDefaultButton2, "Please Choose" ) ;

if ( buttonPressed == siMsgYes | buttonPressed == siMsgNo  ){
logmessage("User did not press cancel");
var oPasses = ActiveProject.ActiveScene.Passes;
var oSel = Application.selection;
var selPasses = new Array();

for (var q = oSel.count-1; q > 0; q--){
	logmessage(oSel(q).name + " looping through sel items");
	if ( oSel(q).type != "#Pass"){

		ToggleSelection (oSel(q));
	}
}



if ( oSel.count > 0){
	oPasses = oSel;
	
}

logmessage(" There are " + oPasses.count + " passes selected ");





if ( buttonPressed == siMsgNo ){
	var addQuote = "";
	}else{
	var addQuote = '"';
}


// Begin building the AE script
//

var cmd = '"C:\\Adobe\\After Effects 6.5\\Support Files\\AfterFX.exe" -s  \n '; 
cmd +=	addQuote +'var selectedItems = app.project.selection; \n'; 
cmd +=	'for (var i = 0; i < selectedItems.length; i++){ \n'; 
cmd +=	'	selectedItems[i].selected = false; \n';
cmd +=	'} \n';


//Loop through the selected passes, sending it to the function to extract the data we need

for (var i = 0; i < oPasses.count; i++) {
	LogMessage ( "Sending pass " +oPasses(i).Name + " to After Effects");

	
	
	var rPassData = PassInfo(oPasses(i));;
	
	logmessage(rPassData);  //Write out everything we obtained from the function
	
	//setup the variables from the returned array
	var rFileName = rPassData[0];
	var rPixAspect = rPassData[1];
	var rFieldBool = rPassData[2];
	var rFieldOrder = rPassData[3];
	var fps = GetValue("PlayControl.Rate");
	
	cmd +=	'var theFile = new File("'+ rFileName +'"); \n';
	
	cmd +=	'if(theFile.exists){  \n';   
	
	cmd +=	'var importOptions = new ImportOptions (theFile); \n';
	cmd +=	'importOptions.sequence = true; \n';
	cmd +=	'app.project.importFile (importOptions);';
	cmd +=	'var curSel = app.project.selection; \n'; 
	cmd +=	'curSel[0].mainSource.alphaMode = AlphaMode.PREMULTIPLIED; \n';
	cmd +=	'curSel[0].pixelAspect = '+ rPixAspect +'; \n';
	
	//Check for field rendering and then for field dominance
	if (rFieldBool == true){
		if (rFieldOrder == 0){
		cmd +=	'curSel[0].mainSource.fieldSeparationType = FieldSeparationType.LOWER_FIELD_FIRST;  \n';	
		}else{
		cmd +=	'curSel[0].mainSource.fieldSeparationType = FieldSeparationType.UPPER_FIELD_FIRST;  \n';
		}
	}
		cmd +=	'curSel[0].mainSource.conformFrameRate = '+fps+';  \n';
		
	cmd +=	'}\n';
	//use this for blank lines or copy-paste  cmd +=	'  \n';
}
if ( buttonPressed == siMsgYes ){
	cmd +=	'"\n';
	}


//LogMessage(cmd)  //used for checking the output script




if ( buttonPressed == siMsgNo )
{
   LogMessage( "Sending command to After Effects now..." ) ;
   
   //This command executes the script we have written
   XSIUtils.LaunchProcess(cmd, false)

}
else
{
   LogMessage( "Writing Batch file now..." ) ;
          var fileBrowser = XSIUIToolkit.FileBrowser;
          
          fileBrowser.Filter = "BAT (*.bat)|*.bat||";
          
          fileBrowser.ShowSave();
          
          var fileName = fileBrowser.filepathname;
          
          //logmessage(filename);   
          
          var fso = new ActiveXObject('Scripting.FileSystemObject');
          
          var file = fso.CreateTextFile(fileName);
          
          var newcmd = cmd.replace(/\n/g, "");
          //newcmd = newcmd.replace(/-s/, "-s "");
          file.write(newcmd);
       	  
       	  file.close();
}





function PassInfo(curPass){//extract data about the selected pass

	var oROptions = (curPass+ ".renderoptions");
	//LogMessage( oROptions);
	var passfilename	= getValue( oROptions + ".Imagefilepath");
	var startframe = getValue(oROptions + ".StartFrame");

	var oPixAspect = getvalue(oROptions + ".PixelRatio");
	var oFieldBool = getvalue(oROptions + ".FieldRendering");
	var oFieldOrder = getvalue(oROptions + ".FieldDominance");
	
	//logmessage("The pixel aspect is " +oPixAspect+ ", this pass has fields " +oFieldBool+ ", and the field order is " +oFieldOrder);
	
	//replace the # with the first frame of the sequence
	var oFilename = passfilename.replace(/#/, startframe);
	
	//add "\"'s to the filepath. We need to send 4 ("\\\\") slashes for each "\". 
	oFilename = oFilename.replace( /\\/g, "\\\\\\\\" );
		
	//LogMessage(oFilename);
	
	//declare an array for returning
	var oPassData = new Array(oFilename, oPixAspect, oFieldBool, oFieldOrder);
	
	//logmessage(oPassData);
return oPassData

}//end function
}//end first if
}//closing brace