Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Renaming Solid Layer (Expression or Script)

  • Renaming Solid Layer (Expression or Script)

  • Scott McGee

    March 28, 2017 at 8:31 am

    Morning all,

    Brains a bit mush today.

    I have 5 layers

    1: Bar 5
    2: Bar 4
    3: Bar 3
    4: Bar 2
    5: Bar 1

    If I delete a layer (e.g Bar3), if I duplicate (e.g Bar 2) I end up with this.

    1: Bar 5
    2: Bar 4
    3: Bar 6
    2: Bar 2
    1: Bar 1

    Because it doesn’t have a source text. I guess an expression won’t fix this, but is there anyway if I hit duplicate, it will create “Bar 3” again without having to delete all the layers above Bar 2. I have a script that relies on it being, Bar 1 sequentially upwards (Unless someone know a script that can look at the next layer below like the expression below this so that it knows which one to change) auto matically.

    This expression works great. To get my layers to always look at the one directly below and +1, even if I place a text layer, adjustment layer between any of the Bars.

    val = null;
    for ( i = index+1; i <= thisComp.numLayers; i++){
    try{
    val = thisComp.layer(i).effect(“bar_index”)(“Slider”).value +1;
    break;
    }catch(err){
    }
    }
    if (val != null){ // found one
    // do stuff
    }else{ // no luck
    // do other stuff
    }

    Is there anything like this as an expression that will rename my layers (Bar1 – Bar5). Or if I create a button apply an expression to a script. Hit the button and it will Rename all the layers that start with “Bar” in order. I can get a script that will organise them into order, but that doesn’t solve the problem of me missing Bar 3 in the example above.

  • Scott McGee

    March 28, 2017 at 9:57 pm

    After a few hours of research this script is essentially what I want

    str1 = “Metallic”;
    str2 = “Regent”;
    var comp = app.project.activeItem;
    for (var i = 1; i <= comp.numLayers; i++){
    if (comp.layer(i).name.substr(0,str1.length) == str1)
    comp.layer(i).name = str2 + comp.layer(i).name.substr(str1.length);
    }

    Is there anyway to adapt this to do this,

    str1 = “Bar”;
    str2 = “Bar”;
    k = [1,2,3,4]
    var comp = app.project.activeItem;
    for (var i = 1; i <= comp.numLayers; i++){
    if (comp.layer(i).name.substr(0,str1.length) == str1)
    comp.layer(i).name = str2 + k;
    }

  • Dan Ebberts

    March 28, 2017 at 10:34 pm

    Probably

    comp.layer(i).name = str2 + k[i-1];

    or just

    comp.layer(i).name = str2 + i;

    or probably better

    comp.layer(i).name = str2 + ” ” + i;

    Dan

  • Scott McGee

    March 29, 2017 at 8:24 am

    Hey Dan,

    I’ve combined two of them together, this is nearly what I want.

    comp.layer(i).name = str2 +” “+ k[i-1]

    str1 = “Bar”;
    str2 = “Bar”;
    k = [1,2,3,4]
    var comp = app.project.activeItem;
    for (var i = 1; i <= comp.numLayers; i++){
    if (comp.layer(i).name.substr(0,str1.length) == str1)
    comp.layer(i).name = str2 +” “+ k[i-1];
    }

    I want it to work from the ground up. In my example

    1: Bar 5
    2: Bar 4
    3: Bar 3
    4: Bar 2
    5: Bar 1

    This would work fine, if it is 5 layers, and my array is set [5,4,3,2,1]. I know this works, but if I have 10 layers. It comes up as undefined from layer 6 onwards, which is obvious as to why this happens, as it is looking at the first layer (i = 1) and because I have only 5 numbers in my array it stops after layer 5, how do I get it to work from the ground up?

    So if I had 20 layers, it knows to start from layer 20.

    Then the cherry on top. You wrote me an expression where if a break happens it moves to the next layer not taking the break into account

    val = null;
    for ( i = index+1; i <= thisComp.numLayers; i++){
    try{
    val = thisComp.layer(i).effect(“bar_index”)(“Slider”).value +1;
    break;
    }catch(err){
    }

    I’m happy to write out an array [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15….], because nobody is going to want more than 15. But if you put a text layer in between them I get this.

    1: Bar 1
    2: Bar 2
    3: Text 1
    4: Bar 4
    5: Bar 5
    6: Bar undefined

    I tried adding a break, I tried index +1, but I’m having no success.

    Is it something so simple, i can’t see the forest for the trees.

  • Scott McGee

    March 29, 2017 at 12:11 pm

    I’ve managed to shorten the script as I don’t need str2 as it’s not changing it from bar to something else. it’s just adding a number after

    str1 = “Bar”;
    k = [1,2,3,4,5]
    var comp = app.project.activeItem;
    for (var i = 1; i <= comp.numLayers; i++){
    if (comp.layer(i).name.substr(0,str1.length) == str1)
    comp.layer(i).name = str1 +” “+ k[i-1];
    }

    But i still haven’t got it to start from layer 6 and work it’s way back to layer 1 skipping anything that doesn’t have Bar in it’s title so that the next one in the chain should end up like this.

    1: Bar 5
    2: Bar 4
    3: Text 1
    3: Bar 3
    4: Bar 2
    5: Bar 1

  • Scott McGee

    March 29, 2017 at 3:34 pm

    I cracked it.

    It’s a dirty way to do it, so if anyone has a tidier way feel free to add.

    So Dan helped me with an expression that looked for a specific slider to +1, but if something went between two layers that didn’t have it. It would break and not work, or the expression would include that layer and add +2 which left me with a gap.

    This expression below, would look passed a layer that didn’t contain the slider and ignore it

    val = null;
    for ( i = index+1; i <= thisComp.numLayers; i++){
    try{
    val = thisComp.layer(i).effect(“bar_index”)(“Slider”).value +1;
    break;
    }catch(err){
    }

    So my .value in my slider would be like this

    1:Bar 5 (5)
    2:Bar 4 (4)
    3: Text 1 (Ignore)
    4: Bar 3 (3)
    5: Bar 2 (2)
    6: Bar 1 (1)

    Now if I delete a layer in the middle and and duplicate I get this, but my expression keeps the value of the slider the same like below.

    1:Bar 7 (5)
    2:Bar 4 (4)
    3: Text 1 (Ignore)
    4: Bar 6 (3)
    5: Bar 2 (2)
    6: Bar 1 (1)

    The problem now is that I need my layers to be sequential for my script to work. Otherwise it’s going to skip my editText box referencing Bar 2 and Bar 5

    So with the help of Dan and mucking I thought to get it to read the slider value to add the number next to it. So below.

    str1 = “Bar”;
    var comp = app.project.activeItem;
    for (var i = 1; i <= comp.numLayers; i++){
    if (comp.layer(i).name.substr(0,str1.length) == str1){
    comp.layer(i).name = str1 +” “+ comp.layer(i).effect(“bar_index”)(“Slider”).value;
    }
    }

    Once fired replaces it all back to

    1:Bar 5 (5)
    2:Bar 4 (4)
    3: Text 1 (Ignore)
    4: Bar 3 (3)
    5: Bar 2 (2)
    6: Bar 1 (1)

    For anyone who is thinking…What on earth would you use this for….A bar graph my dear friend. A bar graph that if someone who doesn’t know after effects can open up my template. Fire up my UI panel and tell it what Bar 1 – 5 values are, it will animate to the correct percentage. Too some of you this is child’s play. For me this was my personal mount everest to get this to work and eventually hand this over to the news team to quickly make a bar graph without needing any after effects knowledge…Woohoo.

    <b>str1 = "Bar";
    var comp = app.project.activeItem;
    for (var i = 1; i &lt;= comp.numLayers; i++){
    if (comp.layer(i).name.substr(0,str1.length) == str1){
    comp.layer(i).name = str1 +" "+ comp.layer(i).effect("bar_index")("Slider").value;
    }
    }</b>

  • Tomas Bumbulevičius

    September 27, 2019 at 9:00 am

    Hey Scott, nicely done! Just thought to give you heads up on this breakdown, as it seems you like to have hit a head into a wall for a while, until solution was found. Which, is much appreciated attitude, You, mountaineer! :)))

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.
    Boxer – Dynamic Text Boxes Template with a Live Preview

  • Scott McGee

    September 30, 2019 at 9:21 am

    Cheers Tomas,

    That expression and script saved me weeks of work, and even more so when I had to I had 24 hours to rebrand it all.

  • Aris Melachroinos

    January 3, 2021 at 10:29 am

    Hi Scott,

    I am not sure if I fully understood your whole project (I am not sure if/why you need a slider) but if you want to just rename the layers that start with ‘Bar’ from bottom to top, this is a very simple script. You don’t need any slider/expressions to do something like this, but maybe I misunderstood something.

    var comp = app.project.activeItem;
    var str1 = 'Bar';
    var numCounter = 1;
    //making a regular expression that starts with str1
    var regex = new RegExp("^" + str1);
    //looping through all layers from last to first
    for (var i = comp.numLayers; i >=1; i--){
    var currentLayer = comp.layer(i);
    var currentName = currentLayer.name;
    if(currentName.match(regex)==str1){
    //this layer name starts with str1
    currentLayer.name = str1 + " " + numCounter;
    numCounter++;
    }
    }
Viewing 1 - 9 of 9 posts

Log in to reply.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy