Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: Batch rename layers; replace sub string

  1. #1

    Batch rename layers; replace sub string

    We have a situation where we need to go through each layer of an object and replace a substring (if it’s encountered) with another string. Ex:

    Swap "Car" with "Vehicle"

    Layer 1 Name: Car_Tire
    Layer 2 Name: Car_Wheel
    Layer 3 Name: RedCar
    Layer 4 Name: Helicopter

    ...would return...

    Layer 1 Name: Vehicle_Tire
    Layer 2 Name: Vehicle_Wheel
    Layer 3 Name: RedVehicle
    Layer 4 Name: Helicopter

    I am familiar with Actionscript, but Lscript is very new to me. Can anyone point me in the right direction please?

    Thanks.

  2. #2
    Worms no more! Free fun! Dodgy's Avatar
    Join Date
    Feb 2003
    Location
    Sydney
    Posts
    8,279
    Code:
    myfind="Blah";
    myreplace="More";
    
    mymesh=Mesh(0);
    index=1;
    myname=mymesh.layername(index);
    
    while(myname)
    {
    	exp = regexp(myfind,myreplace);
    	myname ~= exp;
            setlayername(myname);
            index++;
            myname=mymesh.layername(index);           
    }
    Should be something along those lines I think....
    Last edited by Dodgy; 12-08-2006 at 10:34 AM.
    Push the software, don't let it push YOU!
    My own plugins and work:www.mikegreen.name
    LaptopSetup:corei7 2.4GHz nvidia GT 650M 8Gig win8 64
    WorkSetup:core i7 3.4Ghz nvidia Quadro 2000 12Gig Ram Win7

  3. #3
    Worms no more! Free fun! Dodgy's Avatar
    Join Date
    Feb 2003
    Location
    Sydney
    Posts
    8,279
    You might want to look at my plugin pages, as it has the most complete version of Lscript docs.
    Push the software, don't let it push YOU!
    My own plugins and work:www.mikegreen.name
    LaptopSetup:corei7 2.4GHz nvidia GT 650M 8Gig win8 64
    WorkSetup:core i7 3.4Ghz nvidia Quadro 2000 12Gig Ram Win7

  4. #4
    Thanks for the fast reply. I gave that code a try, but got an error:

    Line 8, found 'w', expecting
    Line: while(myname)

    I can check out your site tonight; unfortunately I am at work right now and have restriced web access.

  5. #5
    Worms no more! Free fun! Dodgy's Avatar
    Join Date
    Feb 2003
    Location
    Sydney
    Posts
    8,279
    Did you just try that code on it's own? You'll need a lscript template around it, that was just a fragment. Setup a template in lsed (the lscript editor in the programs directory and try it in that.
    Push the software, don't let it push YOU!
    My own plugins and work:www.mikegreen.name
    LaptopSetup:corei7 2.4GHz nvidia GT 650M 8Gig win8 64
    WorkSetup:core i7 3.4Ghz nvidia Quadro 2000 12Gig Ram Win7

  6. #6
    After pulling up a template (Tools>Templates>LScript(Modeler)) in the LScript Editor, I inserted your code inside of the brackets below "main" (see below code.) I am still getting errors, likely because I am pasting code the way an ape throws its own waste.

    I will check out your site and get this sorted out. Your assistance is appreciated Dodgy.

    Code:

    //Rename

    @version 2.2
    @warnings
    @script modeler

    main
    {

    myfind="Container";
    myreplace="Null";

    mymesh=Mesh(0);
    index=1;
    myname=mymesh.layername(index);

    while(myname)
    {
    exp = regexp(myfind,myreplace);
    myname ~= exp;
    setlayername(myname);
    index++;
    myname=mymesh.layername(index);
    }

    }

  7. #7
    Worms no more! Free fun! Dodgy's Avatar
    Join Date
    Feb 2003
    Location
    Sydney
    Posts
    8,279
    Looks like I got the layername command wrong, it should be .layerName instead.
    Last edited by Dodgy; 12-13-2006 at 08:53 AM.
    Push the software, don't let it push YOU!
    My own plugins and work:www.mikegreen.name
    LaptopSetup:corei7 2.4GHz nvidia GT 650M 8Gig win8 64
    WorkSetup:core i7 3.4Ghz nvidia Quadro 2000 12Gig Ram Win7

  8. #8
    Worms no more! Free fun! Dodgy's Avatar
    Join Date
    Feb 2003
    Location
    Sydney
    Posts
    8,279
    And I also forgot to set the fg layer to rename the correct layer
    Code:
    @version 2.2
    @warnings
    @script modeler
    
    main
    {
    
    	myfind="Container";
    	myreplace="Null";
    	
    	mymesh=Mesh(0);
    	index=1;
    	myname=mymesh.layerName(index);
    //debug();
    	lyrsetfg(index);
    
    	while(myname)
    	{
    		exp = regexp(myfind,myreplace);
    		myname ~= exp;
    		setlayername(myname);
    		index++;
    		lyrsetfg(index);
    		myname=mymesh.layerName(index);
    	}
    
    }
    Push the software, don't let it push YOU!
    My own plugins and work:www.mikegreen.name
    LaptopSetup:corei7 2.4GHz nvidia GT 650M 8Gig win8 64
    WorkSetup:core i7 3.4Ghz nvidia Quadro 2000 12Gig Ram Win7

  9. #9
    That works beautifully, Dodgy. Thanks again.

  10. #10
    Newbie Member
    Join Date
    Feb 2006
    Location
    USA
    Posts
    30
    Hi,
    I have been using this script an it works great until it runs into an empty layer. I'm not a great scripter. What statements would need to be inserted to skip empty layers and move on to subsequent layers with names?
    Thank you,
    GPM

  11. #11
    Worms no more! Free fun! Dodgy's Avatar
    Join Date
    Feb 2003
    Location
    Sydney
    Posts
    8,279
    You'd probably need to just get the layers with data from the beginning. Like so:
    Code:
    @version 2.2
    @warnings
    @script modeler
    
    main
    {
    
    	myfind="Container";
    	myreplace="Null";
    	
    	mymesh=Mesh(0);
            mydata=lyrdata();  // returns an array with all the non-empty layers in it
    
    //debug();
    
    	for(index=1;index<=mydata.size();index++)
    	{
    		myname=mymesh.layerName(index);
                    lyrsetfg(index);
    
    	        exp = regexp(myfind,myreplace);
    		myname ~= exp;
    		setlayername(myname);
    	}
    
    }
    Last edited by Dodgy; 01-26-2007 at 10:21 AM.
    Push the software, don't let it push YOU!
    My own plugins and work:www.mikegreen.name
    LaptopSetup:corei7 2.4GHz nvidia GT 650M 8Gig win8 64
    WorkSetup:core i7 3.4Ghz nvidia Quadro 2000 12Gig Ram Win7

  12. #12
    Not to scale BlueApple's Avatar
    Join Date
    Nov 2005
    Location
    Los Estados Unidos
    Posts
    1,026
    I'm having trouble now and I am not sure why. I am running the below script on an .lwo file with a bunch of object layers. Some with "_" in the name, some without, and some empty layers inbetween. I am trying to simlpy remove the "_" from the object layer names but I get an error ("Line 22, illegal search/replace operation").

    Any thoughts on solving this? I am using LW Modeler 8.5.

    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
    
    //debug();
    
    	for(index=1;index<=mydata.size();index++)
    	{
    		myname=mymesh.layerName(index);
                    lyrsetfg(index);
    
    	        exp = regexp(myfind,myreplace);
    		myname ~= exp;	//!this returns an error: Line 22, illegal search/replace operation
    		setlayername(myname);
    	}
    
    }
    Thanks,

    -Adam
    Adam Martin
    my portfolio
    Mac Pro 2x3Ghz Dual-Core Intel | 8 Gb RAM | OS 10.6.8 | ATI Radeon X1900 XT

  13. #13
    Not to scale BlueApple's Avatar
    Join Date
    Nov 2005
    Location
    Los Estados Unidos
    Posts
    1,026
    A related question:

    What does "~=" do? I can't find it in either the LScriptReference.pdf or LscriptUserGuide.pdf. Does it mean "approximately equal to" or something?
    Adam Martin
    my portfolio
    Mac Pro 2x3Ghz Dual-Core Intel | 8 Gb RAM | OS 10.6.8 | ATI Radeon X1900 XT

  14. #14
    Valiant NewTeKnight Matt's Avatar
    Join Date
    Feb 2003
    Location
    San Antonio, Texas, USA
    Posts
    13,056
    Quote Originally Posted by BlueApple View Post
    A related question:

    What does "~=" do? I can't find it in either the LScriptReference.pdf or LscriptUserGuide.pdf. Does it mean "approximately equal to" or something?
    Regular Expressions (regexp)
    http://en.wikipedia.org/wiki/Regular_expression
    UI / UX Designer @ NewTek
    __________________________________________________
    www.pixsim.co.uk : LightWave Video Tutorials & Tools


  15. #15
    Not to scale BlueApple's Avatar
    Join Date
    Nov 2005
    Location
    Los Estados Unidos
    Posts
    1,026
    Thanks for the reply, Matt, though I still don't follow what the "~=" operator is supposed to do. I'm somewhat familiar with regular expressions already, I just didn't know what "~=" does.

    The "~" only exists once in the page you linked to, and it doesn't shed any light on my question as far as I can tell, but perhaps I am being dense.
    Adam Martin
    my portfolio
    Mac Pro 2x3Ghz Dual-Core Intel | 8 Gb RAM | OS 10.6.8 | ATI Radeon X1900 XT

Page 1 of 2 12 LastLast

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
  •