ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Renaming Solid Layer (Expression or Script)

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Scott McGee
Renaming Solid Layer (Expression or Script)
on Mar 28, 2017 at 8:31:26 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.


Return to posts index

Scott McGee
Re: Renaming Solid Layer (Expression or Script)
on Mar 28, 2017 at 9:57:41 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;
}


Return to posts index

Dan Ebberts
Re: Renaming Solid Layer (Expression or Script)
on Mar 28, 2017 at 10:34:32 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



Return to posts index


Scott McGee
Re: Renaming Solid Layer (Expression or Script)
on Mar 29, 2017 at 8:24:33 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.


Return to posts index

Scott McGee
Re: Renaming Solid Layer (Expression or Script)
on Mar 29, 2017 at 12:11:01 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


Return to posts index

Scott McGee
Re: Renaming Solid Layer (Expression or Script)
on Mar 29, 2017 at 3:34:09 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>


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]