Text Parsing script?

Text Parsing script?

6
NewbieNewbie
6

    Apr 28, 2008#1

    I'm very new to scripting, but what I'm hoping to be able to do seems very straightforward...I just have no idea how to start.

    We have thousands upon thousands of script files that we use to define particle emitters. These don't follow any logical formatting, but I'd like to rectify that through a script. Here is an example of an unformated particle script:

    Code: Select all

    ##############################
    #Particle System 
    
    System
    
    NewPerFrame		.05	
    
    StartSize		.2
    StartSizeJitter		0.05
    ExpandRate		0.9
     ExpandType		1
      EndSize		5
    
    OrientationJitter	180
    
    FadeInBy 		0
    FadeOutStart		3
    FadeOutBy   		5
    
    Burst			0			
    	
    WorldOrLocalPosition	1
    FrontOrLocalFacing	0
    
    EmissionType		0 
     EmissionRadius		0.0 
     EmissionHeight		0.0
    EmissionStartJitter	0.01 0.01 0.01  	
    
    InitialVelocity		0 0 0.0		
    InitialVelocityJitter	0.0 0.0 0.0	
    VelocityJitter		0.0 0.0 0.0
    
    Spin			0			
    SpinJitter		0			
    
    TightenUp		0
    
    
    StreakType		0 	
    StreakScale		0
    StreakOrient		1 	
    StreakDirection		0 
    			
    TextureName		sharpstar.tga
     TexScroll1		0.0 0.0 0.0
     TexScrollJitter1	0.0 0.0 0.0
     AnimFrames1		0
      AnimPace1		0.0  	
      AnimType1		0	
    				
    TextureName2		0
     TexScroll2		0.0 0.0 0.0
     TexScrollJitter2	0.0 0.0 0.0
     AnimFrames2		0
      AnimPace2		0.0  	
     AnimType2		0
    
    Blend_mode		1 			
    Alpha			130
    
    StartColor		255 255 255 
    ColorChangeType		0 
    
    End
    What I'd like to be able to do is open up one of these particle files, and run a javascript that will go through and read each line and store the values as a variable, then output them into a more formatted version of the file that looks something like this:

    Code: Select all

    #############################################################
    ## ParticleScriptName.part
    #############################################################
    
    System
    
    ## FLAGS ###########################################################
    
    Flags AlwaysDraw IgnoreFxTint
    
    ## EMITTER ###########################################################
    
    KickStart		0
    TimeToFull		10
    EmissionLifeSpan	0 +
    EmissionLifeSpanJitter	0 +
    DieLikeThis		0 #:BobbleDeath.part
    DeathAgeToZero		0
    
    Burst			0
    NewPerFrame		0.0
    MoveScale		1.0
    
    BurbleAmplitude		1.0
    BurbleType		0
    BurbleThreshold		0.0
    
    WorldOrLocalPosition	0
    EmissionType		0
    EmissionRadius		0.0
    EmissionHeight		0.0
    EmissionStartJitter	0.0 0.0 0.0
    TightenUp		0.0
    
    ## MOTION ###########################################################
    
    InitialVelocity		0.0 0.0 0.0
    InitialVelocityJitter	0.0 0.0 0.0
    VelocityJitter		0.0 0.0 0.0
    Magnetism		0.0
    Gravity			0.0
    Drag			0.0
    Stickiness		1.0
    
    ## PARTICLE ###########################################################
    
    FrontOrLocalFacing	0
    SortBias		0.5
    
    OrientationJitter	0
    Spin			0
    SpinJitter		0
    
    StartSize		5.0
    StartSizeJitter		0.0
    ExpandRate		0.0
    ExpandType		0
    EndSize			0.0
    
    StreakType		0
    StreakScale		20
    StreakOrient		1
    StreakDirection		1
    
    ## TEXTURE ###########################################################
    
    Blend_mode		Normal
    
    TextureName		white.tga
    TexScroll1		0.0 0.0 0.0
    TexScrollJitter1	0.0 0.0 0.0
    AnimFrames1		0
    AnimPace1		1
    AnimType1		0
    
    TextureName2		spark.tga
    TexScroll2		0.0 0.0 0.0
    TexScrollJitter2	0.0 0.0 0.0
    AnimFrames2		0
    AnimPace2		1
    AnimType2		0
    
    ## PARTICLE ALPHA ###########################################################
    
    Alpha			255
    FadeInBy		0
    FadeOutStart		0
    FadeOutBy		0
    
    StartColor		255 255 255
    ColorChangeType		0
    
    BeColor1		0 0 0
    ByTime1			0
    
    BeColor2		0 0 0
    ByTime2			0
    
    BeColor3		0 0 0
    ByTime3			0
    
    BeColor4		0 0 0
    ByTime4			0
    
    ColorOffset		0 0 0
    ColorOffsetJitter	0 0 0
    
    #############################################################
    
    End
    So far, I've figured out how to have UltraEdit ask me for a file name, create the new file, paste the template version of the particle script into it (line by line)...but that's it. I'm at a complete loss as to how to read a variable from one file and paste it into the new one.

    Here's my progress so far:

    Code: Select all

    // ----------------------------------------------------------------------------
    // Format_Particle_Script.js
    // Reads in variables from particle script and outputs into a templated format
    // ----------------------------------------------------------------------------
    // UltraEdit is our application object.  All UltraEdit operations will use this
    // object.
    // Operations may be performed on the activeDocument property of the UltraEdit
    // object.
    // See UltraEdit Help for more details.
    
    //Define Default Variables
    var KickStart=0
    var TimetoFull=10
    var EmissionLifeSpan=0
    var EmissionLifeSpanJitter=0
    var DieLikeThis=0
    var DeathAgeToZero=0
    var Burst=0
    var NewPerFrame=0.0
    var MoveScale=1.0
    var BurbleAmplitude=1.0
    var BurbleType=0
    var BurbleThreshold=0.0
    var WorldOrLocalPosition=0
    var EmissionType=0
    var EmissionRadius=0.0
    var EmissionHeight=0.0
    var EmissionStartJitter="0.0 0.0 0.0"
    var TightenUp=0.0
    var InitialVelocity="0.0 0.0 0.0"
    var InitialVelocityJitter="0.0 0.0 0.0"
    var VelocityJitter="0.0 0.0 0.0"
    var Magnetism=0.0
    var Gravity=0.0
    var Drag=0.0
    var Stickiness=1.0
    var FrontOrLocalFacing=0
    var SortBias=0.5
    var OrientationJitter=0
    var Spin=0
    var SpinJitter=0
    var StartSize=5.0
    var StartSizeJitter=0.0
    var ExpandRate=0.0
    var ExpandType=0
    var EndSize=0.0
    var StreakType=0
    var StreakScale=20
    var StreakOrient=1
    var StreakDirection=1
    var Blend_mode="Normal"
    var TextureName="white.tga"
    var TexScroll1="0.0 0.0 0.0"
    var TexScrollJitter1="0.0 0.0 0.0"
    var AnimFrames1=0
    var AnimPace1=1
    var AnimType1=0
    var TextureName2="0"
    var TexScroll2="0.0 0.0 0.0"
    var TexScrollJitter2="0.0 0.0 0.0"
    var AnimFrames2=0
    var AnimPace2=1
    var AnimType2=0
    var Alpha=255
    var FadeInBy=0
    var FadeOutStart=0
    var FadeOutBy=0
    var StartColor="255 255 255"
    var ColorChangeType=0
    var BeColor1="0 0 0"
    var ByTime1=0
    var BeColor2="0 0 0"
    var ByTime2=0
    var BeColor3="0 0 0"
    var ByTime3=0
    var BeColor4="0 0 0"
    var ByTime4=0
    var ColorOffset="0 0 0"
    var ColorOffsetJitter="0 0 0"
    
    // Get filename of current document
    var filepath = UltraEdit.activeDocument.path + "\r\n";
    var i = filepath.lastIndexOf("\\");
    var ParticleScriptName = filepath.slice(i + 1);
    
    // Get variables from current document
    var NewPerFrameSource="NewPerFrame		0.1"
    var i = NewPerFrameSource.lastIndexOf("	");
    var NewPerFrame = NewPerFrameSource.slice(i + 1);
    
    //Create a new file, which will become the currently active document
    UltraEdit.newFile();
    
    //Write the new Particle Script file
    UltraEdit.activeDocument.write("#############################################################\r\n");
    UltraEdit.activeDocument.write("## " + ParticleScriptName);
    UltraEdit.activeDocument.write("#############################################################\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("System\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("## FLAGS ###########################################################\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("#Flags AlwaysDraw IgnoreFxTint\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("## EMITTER ###########################################################\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("KickStart		" + KickStart + "\r\n");
    UltraEdit.activeDocument.write("TimeToFull		" + TimetoFull + "\r\n");
    UltraEdit.activeDocument.write("EmissionLifeSpan	" + EmissionLifeSpan + "\r\n");
    UltraEdit.activeDocument.write("EmissionLifeSpanJitter	" + EmissionLifeSpanJitter + "\r\n");
    UltraEdit.activeDocument.write("DieLikeThis		" + DieLikeThis + "\r\n");
    UltraEdit.activeDocument.write("DeathAgeToZero		" + DeathAgeToZero + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("Burst			" + Burst + "\r\n");
    UltraEdit.activeDocument.write("NewPerFrame		" + NewPerFrame + "\r\n");
    UltraEdit.activeDocument.write("MoveScale		" + MoveScale + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("BurbleAmplitude		" + BurbleAmplitude + "\r\n");
    UltraEdit.activeDocument.write("BurbleType		" + BurbleType + "\r\n");
    UltraEdit.activeDocument.write("BurbleThreshold		" + BurbleThreshold + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("WorldOrLocalPosition	" + WorldOrLocalPosition + "\r\n");
    UltraEdit.activeDocument.write("EmissionType		" + EmissionType + "\r\n");
    UltraEdit.activeDocument.write("EmissionRadius		" + EmissionRadius + "\r\n");
    UltraEdit.activeDocument.write("EmissionHeight		" + EmissionHeight + "\r\n");
    UltraEdit.activeDocument.write("EmissionStartJitter	" + EmissionStartJitter + "\r\n");
    UltraEdit.activeDocument.write("TightenUp		" + TightenUp + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("## MOTION ###########################################################\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("InitialVelocity		" + InitialVelocity + "\r\n");
    UltraEdit.activeDocument.write("InitialVelocityJitter	" + InitialVelocityJitter + "\r\n");
    UltraEdit.activeDocument.write("VelocityJitter		" + VelocityJitter + "\r\n");
    UltraEdit.activeDocument.write("Magnetism		" + Magnetism + "\r\n");
    UltraEdit.activeDocument.write("Gravity			" + Gravity + "\r\n");
    UltraEdit.activeDocument.write("Drag			" + Drag + "\r\n");
    UltraEdit.activeDocument.write("Stickiness		" + Stickiness + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("## PARTICLE ###########################################################\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("FrontOrLocalFacing	" + FrontOrLocalFacing + "\r\n");
    UltraEdit.activeDocument.write("SortBias		" + SortBias + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("OrientationJitter	" + OrientationJitter + "\r\n");
    UltraEdit.activeDocument.write("Spin			" + Spin + "\r\n");
    UltraEdit.activeDocument.write("SpinJitter		" + SpinJitter + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("StartSize		" + StartSize + "\r\n");
    UltraEdit.activeDocument.write("StartSizeJitter		" + StartSizeJitter + "\r\n");
    UltraEdit.activeDocument.write("ExpandRate		" + ExpandRate + "\r\n");
    UltraEdit.activeDocument.write("ExpandType		" + ExpandType + "\r\n");
    UltraEdit.activeDocument.write("EndSize			" + EndSize + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("StreakType		" + StreakType + "\r\n");
    UltraEdit.activeDocument.write("StreakScale		" + StreakScale + "\r\n");
    UltraEdit.activeDocument.write("StreakOrient		" + StreakOrient + "\r\n");
    UltraEdit.activeDocument.write("StreakDirection		" + StreakDirection + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("## TEXTURE ###########################################################\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("Blend_mode		" + Blend_mode + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("TextureName		" + TextureName + "\r\n");
    UltraEdit.activeDocument.write("TexScroll1		" + TexScroll1 + "\r\n");
    UltraEdit.activeDocument.write("TexScrollJitter1	" + TexScrollJitter1 + "\r\n");
    UltraEdit.activeDocument.write("AnimFrames1		" + AnimFrames1 + "\r\n");
    UltraEdit.activeDocument.write("AnimPace1		" + AnimPace1 + "\r\n");
    UltraEdit.activeDocument.write("AnimType1		" + AnimType1 + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("TextureName2		" + TextureName2 + "\r\n");
    UltraEdit.activeDocument.write("TexScroll2		" + TexScroll2 + "\r\n");
    UltraEdit.activeDocument.write("TexScrollJitter2	" + TexScrollJitter2 + "\r\n");
    UltraEdit.activeDocument.write("AnimFrames2		" + AnimFrames2 + "\r\n");
    UltraEdit.activeDocument.write("AnimPace2		" + AnimPace2 + "\r\n");
    UltraEdit.activeDocument.write("AnimType2		" + AnimType2 + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("## PARTICLE ALPHA ###########################################################\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("Alpha			" + Alpha + "\r\n");
    UltraEdit.activeDocument.write("FadeInBy		" + FadeInBy + "\r\n");
    UltraEdit.activeDocument.write("FadeOutStart		" + FadeOutStart + "\r\n");
    UltraEdit.activeDocument.write("FadeOutBy		" + FadeOutBy + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("StartColor		" + StartColor + "\r\n");
    UltraEdit.activeDocument.write("ColorChangeType		" + ColorChangeType + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("BeColor1		" + BeColor1 + "\r\n");
    UltraEdit.activeDocument.write("ByTime1			" + ByTime1 + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("BeColor2		" + BeColor2 + "\r\n");
    UltraEdit.activeDocument.write("ByTime2			" + ByTime2 + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("BeColor3		" + BeColor3 + "\r\n");
    UltraEdit.activeDocument.write("ByTime3			" + ByTime3 + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("BeColor4		" + BeColor4 + "\r\n");
    UltraEdit.activeDocument.write("ByTime4			" + ByTime4 + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("ColorOffset		" + ColorOffset + "\r\n");
    UltraEdit.activeDocument.write("ColorOffsetJitter	" + ColorOffsetJitter + "\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("#############################################################\r\n");
    UltraEdit.activeDocument.write("\r\n");
    UltraEdit.activeDocument.write("End");
    I have it mostly figured out...the part I'm not sure how to address is this part:

    Code: Select all

    // Get variables from current document
    var NewPerFrameSource="NewPerFrame		0.1" //This line is just a placeholder to work on the next two instructions.
    var i = NewPerFrameSource.lastIndexOf("	");
    var NewPerFrame = NewPerFrameSource.slice(i + 1);
    How can I read the current document and look for a line that contains a certain string (ie, NewPerFrame), then copy that whole line in as a variable that I can then use the lastIndexOf and slice to par down to the final variable/string I need to paste into my new format?

    262
    MasterMaster
    262

      Apr 29, 2008#2

      Here is something that might inspire you to further develop your script.

      I start from the part where you write "Get variables from current document" and end before "UltraEdit.newFile()";

      Code: Select all

      // Get variables from current document
      
      // Helper prototype to make a perl regexp available for UE find command:
      RegExp.prototype.toUEregexp = function() { return this.toString().replace(/^\/|\/$/g, ""); }; /* remove starting and ending slashes */
      
      // Set up search defaults:
      UltraEdit.activeDocument.findReplace.matchCase=false;
      UltraEdit.activeDocument.findReplace.matchWord=false;
      UltraEdit.activeDocument.findReplace.regExp=true;
      UltraEdit.activeDocument.findReplace.searchDown=true;
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
      
      // Use Perl regexp engine:
      UltraEdit.perlReOn();
      
      // Goto top of document:
      UltraEdit.activeDocument.top();
      
      // Perl regexp that will find the lines with variable assignments.
      // Lines starting with # is ignored (comments)
      // Leading whitespace is ignored
      // Two tagged expressions : \1 = Name, \2 = Value
      // Not explained any further. To do your own study go to: http://www.regular-expressions.info/
      var searchRegexp = /^[ \t]*?([^# \t\r\n]+)[ \t]+(.*)$/;
      
      // Go through the file while finding lines with variables assignments
      while (UltraEdit.activeDocument.findReplace.find(searchRegexp.toUEregexp())) {
      
      	// Get selected text
      	var line = UltraEdit.activeDocument.selection;
      
      	// Extract variable name and value by re-executing the perl regexp on the text with the exec-method.
      	var [ , variableName, variableValue ] = searchRegexp.exec(line);
      
      	// Compose javascript with variable name and value, that test for any undefined variables.
      	// Those are ignored while defined variables are reassigned a new value,
      	var assignmentStatement = "if (typeof("+variableName+") == \"undefined\") { /* ignore */ } else { "+variableName+ "= \"" +variableValue+ "\" } "; 
      	
      	// Execute "composed" javascript with eval function
      	eval(assignmentStatement);
      }
      
      //Create a new file, which will become the currently active document
      

      6
      NewbieNewbie
      6

        Apr 29, 2008#3

        :o
        I'm still trying to figure out what you did (zero familiarity with Perl regexp, so that bit looks like magic to me), but it's working perfectly. Thanks a ton.

        One more small thing. Is there a way to look at the variableValue for any decimals that don't have a 0 in front of them?

        For example, I might be getting this:
        InitialVelocity .001 0.0 -.1

        And I'd really like to be outputting this:
        InitialVelocity 0.001 0.0 -0.1

        I'm thinking the solution may be more trouble than it's worth, but it can't hurt to ask.
        Again, thanks for your help with this.

        262
        MasterMaster
        262

          Apr 29, 2008#4

          Well add this function to the script:

          Code: Select all

          function editNumerics(variableValue) {
             // Reduce spaces to a single space between numbers
             variableValue = variableValue.replace(/[ ]{2,}/g," ");
          
             // Split into an array
             var numbers = variableValue.split(" ");
          
             // Examine every number
             for (var i=0;i<numbers.length;i++) {
                // Use a regexp to find numbers starting with . or -. and put a 0 in front
                numbers[i] = numbers[i].replace(/^(\-)?(\.\d+)$/,"$10$2");
             }
          
             // Join array again and return it
             return numbers.join(" ");
          }
          Then change the line that "composes" the assignmentStatement to:

          Code: Select all

          var assignmentStatement = "if (typeof("+variableName+") == \"undefined\") { /* ignore */ } else { "+variableName+ "= editNumerics(\"" +variableValue+ "\") } ";

          6
          NewbieNewbie
          6

            May 05, 2008#5

            Wanted to thank you for all your help. I'll be honest, a lot of what you've given me is way over my head, but it's working exactly as I hoped it would. I hope it was easier for you to do than it looks to me :)

              Jul 17, 2008#6

              Back again. Right now the script is creating a new document and pasting the formated code into it. Is there a way to just get all of the variables from the current document, select all, delete, and then paste the formatted script back into the original file?

              What I'm currently using is this:

              Code: Select all

              UltraEdit.activeDocument.findReplace.replaceAll=true
              UltraEdit.activeDocument.top();
              UltraEdit.activeDocument.findReplace.replace(".*","");
              UltraEdit.activeDocument.top();
              UltraEdit.activeDocument.findReplace.replace("\r","");
              UltraEdit.activeDocument.top();
              UltraEdit.activeDocument.findReplace.replace("\n","");
              Which seems to work, but also seems very hacky.

              262
              MasterMaster
              262

                Jul 18, 2008#7

                Just use:

                Code: Select all

                UltraEdit.activeDocument.selectAll();
                UltraEdit.activeDocument.deleteText();