// XSI_XML_Import.js
// This script imports an XML data file fromXSI that contains camera, null, light,  and polygon information.
//
// original script by Helge Mathe
// revised September 2007 Byron Nash

//Usage:
//Follow instructions on companion XSI script "AEExport_v4.js"
//Select a composition or have one active
//Run this script and choose the exported XML file

var scaleFactor = 10;

var file = File.openDialog("Choose XML..."," ");
if (file){
       importXMLFile(file);
}

function importXMLFile(file)
{

 // create undo group
 app.beginUndoGroup("Import Camera File");

 var myComp = app.project.activeItem;
 if((! myComp instanceof CompItem)||(!myComp)) {
   alert("Please select a composition");
   return false;
 }


       writeLn("Start..... in comp "+myComp.name);
       file.open("r","","");

       var fps = myComp.frameDuration;

       var fileFrameIn = 0;
       var fileFrameOut = 99;
       var fileFps = 25;
       var inRoot = false;
       var currentObj = "";
       var obj;
       var solidComp;

 var c = 0;
       while(!file.eof)
       {
          c++;
               var s = file.readln();

   // get rid of all tabs and the < and >
   while(s[0]=="\t")
     s = s.substr(1,10000);
   try{
     s = s.substr(1,s.length-2);
   }catch(e){}
   var tokens = s.split(" ");

               if(s!="")
               {
                 if(inRoot){
       if(tokens[0]=="/root")
         inRoot = false;
       else
       {
         if(s.substr(0,1)=="/"){
             currentObj = "";
         }else{
           var name ="";
           var lightType ="";
           if(currentObj){

             // this where the animation takes place
             var frame = 0;
             var pnts = new Array();
             for(var i=1;i<tokens.length;i++){
               var token = tokens[i].split("=");
               if(token[0]=="frame"){
                 frame = eval(token[1]);
               }else if(token[0]=="pos"){
                 obj.property("position").setValueAtTime((frame-1)*fps,[scaleFactor*eval(token[1].split("|")[0]),-scaleFactor*eval(token[1].split("|")[1]),-scaleFactor*eval(token[1].split("|")[2])]);
               }else if(token[0]=="ori"){
                 //obj.property("orientation").setValueAtTime((frame-1)*fps,[eval(token[1].split("|")[0]),-eval(token[1].split("|")[1]),-eval(token[1].split("|")[2])]);
                 //alert(90+eval(token[1].split("|")[0]));
                 obj.property("rotationX").setValueAtTime((frame-1)*fps,eval(token[1].split("|")[0]));
                 obj.property("rotationY").setValueAtTime((frame-1)*fps,-eval(token[1].split("|")[1]));
                 obj.property("rotationZ").setValueAtTime((frame-1)*fps,-eval(token[1].split("|")[2]));
               }else if(token[0]=="rot"){
                 obj.property("rotationX").setValueAtTime((frame-1)*fps,eval(token[1].split("|")[0]));
                 obj.property("rotationY").setValueAtTime((frame-1)*fps,eval(token[1].split("|")[1]));
                 obj.property("rotationZ").setValueAtTime((frame-1)*fps,eval(token[1].split("|")[2]));
               }else if(token[0]=="interest"){
                 obj.property("pointOfInterest").setValueAtTime((frame-1)*fps,[scaleFactor*eval(token[1].split("|")[0]),-scaleFactor*eval(token[1].split("|")[1]),-scaleFactor*eval(token[1].split("|")[2])]);
               }else if(token[0]=="fov"){
                 obj.property("zoom").setValueAtTime((frame-1)*fps,eval(token[1]));
               }else if(token[0]=="color"){
                 obj.property("color").setValueAtTime((frame-1)*fps,[eval(token[1].split("|")[0]),eval(token[1].split("|")[1]),eval(token[1].split("|")[2])]);
               }else if(token[0]=="intensity"){
                 obj.property("intensity").setValueAtTime((frame-1)*fps,100*eval(token[1]));
               }else if(token[0]=="size"){
                 obj.property("scale").setValueAtTime((frame-1)*fps,[2*scaleFactor*eval(token[1]),2*scaleFactor*eval(token[1])]);
               }else if(token[0]=="pnt"){
                 pnts.push([50+50*eval(token[1].split('|')[0]),50-50*eval(token[1].split('|')[1])]);
               }else if(token[0]=="roll"){
                 obj.property("rotation").setValueAtTime((frame-1)*fps,-eval(token[1]));
               }
             }

             if(pnts.length>0){
                                                               mask = obj.Masks(1);
                                                               maskShape = mask.property("maskShape");
                                                               shape = maskShape.value;
                                                               shape.vertices = pnts;
                                                               shape.closed = true;
                                                               maskShape.setValueAtTime((frame-1)*fps,shape);
             }

           }else{
             currentObj = tokens[0];
             name = tokens[1].split("=")[1];
             if(tokens.length>2){
               lightType = tokens[2].split("=")[1];
             }else{
               lightType='';
             }

             //what to do with the different kinds of objects
             if(currentObj=="camera"){
               obj = myComp.layers.addCamera(name,new Array(0,0));
               obj = myComp.layer(1);
               obj.threeDLayer = true;
               obj.startTime = 0;
               obj.inPoint = 0;
               obj.property("Point Of Interest").expression = "position";
             }else if(currentObj=="light"){
               obj = myComp.layers.addLight(name,new Array(0,0));
               obj.threeDLayer = true;
               obj.startTime = 0;
               obj.inPoint = 0;
             }else if(currentObj=="solid"){
                                       //solidComp = app.project.items.addComp('solidComp',100,100,1,15,25);
                                       obj = solidComp.duplicate();
               //obj = myComp.layers.add(solidComp,15);
               obj.threeDLayer = true;
               obj.startTime = 0;
               obj.inPoint = 0;
                                                               obj.Masks.addProperty("Mask");
             }else if(currentObj=="null"){
               obj = myComp.layers.addNull();
               obj.name = name;
               obj.threeDLayer = true;
               obj.startTime = 0;
               obj.inPoint = 0;
             }else{
               currentObj = "";
             }
           }
         }
       }
     }else{
       if(tokens[0]=="root"){
         inRoot = true;
         fileFrameIn = eval(tokens[1].split('=')[1]);
         fileFrameOut = eval(tokens[2].split('=')[1]);
         fileFps = eval(tokens[3].split('=')[1]);
		 var compRateRounded = Math.round(myComp.frameRate*100)/100;
		 if(fileFps!=compRateRounded){
           //alert('File fps ('+fileFps+') does not match composition fps ('+myComp.frameRate+').\nCannot import file.');
           var changeFPS = confirm('XML File frame per second ('+fileFps+') does not match composition frames per second ('+myComp.frameRate+').\nClick "Yes" to change the composition frame rate to match the rate from the file? \nClick "No" to abort the import process.');
			if(changeFPS == true){
					myComp.frameRate = fileFps;
				}else{
					file.close();
					return false;
			}
           
         }
         var solid = myComp.layers.addSolid(new Array(0.7,0.7,0.7),'solid',100,100,1);
                                       solidComp = myComp.layers.precompose([solid.index],'solidComp',false);
                                       solidComp = myComp.layers[1];


       }
     }
               }
       }
       file.close();
       solidComp.remove();
       writeLn("Done...");
 app.endUndoGroup();

}