Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28

Thread: Batch rename layers; replace sub string

  1. #16
    Code Muppet evenflcw's Avatar
    Join Date
    Feb 2003
    Location
    Stockholm, Sweden
    Posts
    2,642
    It's Lscript language syntax, not regexp syntax. The ~ is the regexp operator in lscript. Just like + is the addition operator. It's really all there is to it.

    Ie.
    x+=y is equal to x=x+y, where + applies y on x and assigns the result back to x.
    x~=y is equal to x=x~y, where ~ applies y on x and assigns the result back to x.

    In the latter case y is supposed to be a regular expression and x a string. So the expression is applied on the string.

    PS. I'm not sure if ~ standalone is valid in lscript or if you can swap the places of x and y. Something in the back of my head says it couldn't be done, but I could remember wrong. But I think the above is still a good way to think of it. Ie. just another operator that takes two operands.
    Last edited by evenflcw; 04-14-2010 at 05:38 PM.

  2. #17
    Code Muppet evenflcw's Avatar
    Join Date
    Feb 2003
    Location
    Stockholm, Sweden
    Posts
    2,642
    Looked at the previous post. My first guess is that layers which have not been assigned a name by the user might not actually have one. Ie the value returned from layerName(index) might be nil (ie. not a valid string). So you're trying to apply a regular expression on nil (undefined both in type and value) which is invalid. Do a check before applying the regular expression.

    Second guess, the regexp() function is buggy with empty string replace. Although that seems unlikely.

    Nitpick. You really only need to create the expression once. It's reusable. So put that line, exp=regexp(myfind,myreplace);, outside the loop.
    Last edited by evenflcw; 04-14-2010 at 05:35 PM.

  3. #18
    Not to scale BlueApple's Avatar
    Join Date
    Nov 2005
    Location
    Los Estados Unidos
    Posts
    1,026
    Quote Originally Posted by evenflcw View Post
    It's Lscript language syntax, not regexp syntax. The ~ is the regexp operator in lscript. Just like + is the addition operator. It's really all there is to it.
    Just what I was looking for; thanks. Perhaps the LScript documentation I have is old, or maybe ~ just isn't in there.

    As to your suggestions on how to fix the script, thanks again. I will give it a go first thing in the morning.
    Adam Martin
    my portfolio
    Mac Pro 2x3Ghz Dual-Core Intel | 8 Gb RAM | OS 10.6.8 | ATI Radeon X1900 XT

  4. #19
    Not to scale BlueApple's Avatar
    Join Date
    Nov 2005
    Location
    Los Estados Unidos
    Posts
    1,026
    Made the adjustments that evenflcw suggested and am now very close.

    Code:
    @version 2.2
    @warnings
    @script modeler
    
    main
    {
    	myfind="_";
    	myreplace="";
    	mymesh=Mesh(0);
        mydata=lyrdata();  // returns an array with all the non-empty layers in it
    	
    	exp = regexp(myfind,myreplace);
    	
    	for(index=1;index<=mydata.size();index++)
    	{
    		myname=mymesh.layerName(index);
    		if(myname != nil)	//check if the layer is unnamed
    		{
                lyrsetfg(index);
    			myname ~= exp;
    			setlayername(myname);
    		}
    	
    	}
    }
    The problem now is that we are not getting through all of the layers. This is because the for statement only performs an iteration for every layer containing data.

    Consider an object with nothing on layer 1, and then a cube on layer 2. mydata.size will equal 1 so only layer 1 will be examined. We never get to layer 2.

    I tried looking at the last index in the lyrdata array, however that array lists integers for the layer numbers in the order that the layer data was created. The last number in the array is not necessarily the last layer number containing data.

    Do I need to dump lyrdata into a new variable, sort it, grab the biggest number there, and use that in my for statement?
    Adam Martin
    my portfolio
    Mac Pro 2x3Ghz Dual-Core Intel | 8 Gb RAM | OS 10.6.8 | ATI Radeon X1900 XT

  5. #20
    Code Muppet evenflcw's Avatar
    Join Date
    Feb 2003
    Location
    Stockholm, Sweden
    Posts
    2,642
    Hehe... "hjärnsläpp" (trans:"temporary brain loss") as we say in swedish. Happens even to the best.

    I think processing all layers up to the highest layer number mentioned in lyrdata (and lyrempty even?) sounds like a good idea.
    Last edited by evenflcw; 04-15-2010 at 09:42 AM.

  6. #21
    Valiant NewTeKnight Matt's Avatar
    Join Date
    Feb 2003
    Location
    San Antonio, Texas, USA
    Posts
    13,056

    Arrow

    Try this ...

    Code:
    @version 2.2
    @warnings
    @script modeler
    @name "Layer Rename"
    
    main
    {
    	find_string = " ";
    	replace_string = "_";
    
    	current_mesh = Mesh(0);
    	
    	// Put all the non-empty layer info into an array
    	layers = lyrdata();
    	
    	// Put all the empty layer info into an array
    	layersempty = lyrempty();
    
    	// Find max index of non-empty layers
    	max_layer = layers[layers.size()].asInt();
    	
    	// Find max index of empty layers
    	max_layer_empty = layersempty[layersempty.size()].asInt();
    
    	// Create search / replace expression
    	exp = regexp(find_string,replace_string);
    
    	// Loop through all layers until we hit the max layer
    	for(index = 1; index <= max( max_layer, max_layer_empty ); index++)
    	{
    		// Grab the current layer name
    		current_layer_name = current_mesh.layerName(index);
    
    		// Don't do anything to unnamed layers
    		if(current_layer_name != nil)
    		{
    			// Set layer to foreground
    			lyrsetfg(index);
    
    			// Rename layer
    			current_layer_name ~= exp;
    
    			// Set new name for layer
    			setlayername(current_layer_name);
    		}
    	}
    }
    Also added more comments for those wanting to learn LScript, and renamed some variables to be nice and clear.
    Last edited by Matt; 04-15-2010 at 10:16 AM.
    UI / UX Designer @ NewTek
    __________________________________________________
    www.pixsim.co.uk : LightWave Video Tutorials & Tools


  7. #22
    Not to scale BlueApple's Avatar
    Join Date
    Nov 2005
    Location
    Los Estados Unidos
    Posts
    1,026
    Success:

    Code:
    @version 2.2
    @warnings
    @script modeler
    
    main
    {
    
    //This script will go through an object in Modeler 8.5 and remove the "_" from all layer names.
    
    	myfind="_";
    	myreplace="";
    	mymesh=Mesh(0);
        mydata=lyrdata();  // returns an array with all the non-empty layers in it
    	
    	exp = regexp(myfind,myreplace);
    	
    	aTemp = mydata; //creates a duplicate of the array.
    	aTemp.sortA();	//sorts the array
    	
    	for(index=1;index<=aTemp[aTemp.size()];index++)
    	{
    		myname=mymesh.layerName(index);
    		if(myname != nil)	//check if the layer is unnamed
    		{
                lyrsetfg(index);
    			myname ~= exp;
    			setlayername(myname);
    		}
    	
    	}
    }
    Thank you all for your assistance.
    Adam Martin
    my portfolio
    Mac Pro 2x3Ghz Dual-Core Intel | 8 Gb RAM | OS 10.6.8 | ATI Radeon X1900 XT

  8. #23
    Valiant NewTeKnight Matt's Avatar
    Join Date
    Feb 2003
    Location
    San Antonio, Texas, USA
    Posts
    13,056

    Arrow

    Quote Originally Posted by BlueApple View Post
    Success:

    Thank you all for your assistance.
    Not quite

    It fails when:
    • The object has no geometry, but has named layers
    • Doesn't catch empty but named layers after any layers with objects on (see attached JPG)

    Attached is the script I modified above, but with a simple UI on it to allow easier entering of search / replace strings.

    Works in all situations, including the two points mentioned above.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	script_will_fail.png 
Views:	59 
Size:	15.8 KB 
ID:	84048  
    Attached Files Attached Files
    Last edited by Matt; 04-15-2010 at 10:56 AM.
    UI / UX Designer @ NewTek
    __________________________________________________
    www.pixsim.co.uk : LightWave Video Tutorials & Tools


  9. #24
    Not to scale BlueApple's Avatar
    Join Date
    Nov 2005
    Location
    Los Estados Unidos
    Posts
    1,026
    Quote Originally Posted by Matt View Post
    It fails when:
    • The object has no geometry, but has named layers
    • Doesn't catch empty but named layers after any layers with objects on (see attached JPG)
    I should've said it works successfully for my situation. I don't need to perform this operation in the two scenarios you laid out. However making the script handle more situations, and the UI are welcome additions so thank you.

    I ran your script on the attached object and it appears to rename the first two object layers and then stops. I populated the first box in the UI with an underscore and left the second box blank. Any thoughts?
    Attached Files Attached Files
    Adam Martin
    my portfolio
    Mac Pro 2x3Ghz Dual-Core Intel | 8 Gb RAM | OS 10.6.8 | ATI Radeon X1900 XT

  10. #25
    Valiant NewTeKnight Matt's Avatar
    Join Date
    Feb 2003
    Location
    San Antonio, Texas, USA
    Posts
    13,056

    Arrow

    Quote Originally Posted by BlueApple View Post
    I should've said it works successfully for my situation. I don't need to perform this operation in the two scenarios you laid out. However making the script handle more situations, and the UI are welcome additions so thank you.

    I ran your script on the attached object and it appears to rename the first two object layers and then stops. I populated the first box in the UI with an underscore and left the second box blank. Any thoughts?
    Fixed, turns out saving an object with blank layers between populated layers changes the lyrdata() and lyrempty() arrays so that the highest layer number may not always be the last (as it was when I tested it).

    Added sortA() to both which solves the situation.
    Attached Files Attached Files
    UI / UX Designer @ NewTek
    __________________________________________________
    www.pixsim.co.uk : LightWave Video Tutorials & Tools


  11. #26
    Not to scale BlueApple's Avatar
    Join Date
    Nov 2005
    Location
    Los Estados Unidos
    Posts
    1,026
    I'll check it out when I'm back in the office, Matt. Thanks for your assistance.
    Adam Martin
    my portfolio
    Mac Pro 2x3Ghz Dual-Core Intel | 8 Gb RAM | OS 10.6.8 | ATI Radeon X1900 XT

  12. #27
    Super Member geo_n's Avatar
    Join Date
    Aug 2007
    Location
    jpn
    Posts
    4,677
    This is cool. Will try this out. I needed to rename a lot of layers in modeller and I could have used this. I always liked Mike Green's MGrenamer in layout and wish it had a version in modeller.
    I'm also using a surface global renamer that adds prefix and suffix as well. Forgot who made it though.

  13. #28
    Not to scale BlueApple's Avatar
    Join Date
    Nov 2005
    Location
    Los Estados Unidos
    Posts
    1,026
    Quote Originally Posted by Matt View Post
    Fixed, turns out saving an object with blank layers between populated layers changes the lyrdata() and lyrempty() arrays so that the highest layer number may not always be the last (as it was when I tested it).
    I think that those lyrdata is populated with layer numbers in the order that geometry was created in them. If you make a box on layer 1, 2 and then 3 your array will read [1,2,3]. If you first make a box on layer 3, then layer 1 and finally layer 2 you will get [3,1,2]. I didn't do a lot of testing to confirm this but it seemed to be the behavior I was seeing.

    Your script is great, and the UI is a wonderful addition. Thanks again for your work on this, sir.
    Adam Martin
    my portfolio
    Mac Pro 2x3Ghz Dual-Core Intel | 8 Gb RAM | OS 10.6.8 | ATI Radeon X1900 XT

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •