parsing layer names

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
bkan
Posts: 51
Joined: November 6th, 2013, 8:33 am

Hello,
I try to parse layer names. This works, but it launches only the name of the first layer (it's normal) :
var curComp = app.project.activeItem;
for (var i=0;i<curComp.numLayers;i++){
alert("number of layers is " +curComp.layers[1].name);
}


But, I don't understand because, when I assign the var "i" to the layers, it doesn't work

var curComp = app.project.activeItem;
for (var i=0;i<curComp.numLayers;i++){
var number = i;
alert("number of layers is " +curComp.layers.name);
}

Any idea??
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

Aren't you getting this error message in the ESTK?
"After Effects error: Value 0 out of range 1 to ..."

That should give you a clue. Layer indexes start from 1. So you'd need:
for (var i = 1; i <= curComp.numLayers; i++) {

Paul
bkan
Posts: 51
Joined: November 6th, 2013, 8:33 am

Paul Tuersley wrote:Aren't you getting this error message in the ESTK?
"After Effects error: Value 0 out of range 1 to ..."

That should give you a clue. Layer indexes start from 1. So you'd need:
for (var i = 1; i <= curComp.numLayers; i++) {

Paul
Ok, Paul thank you again, it works!
Now, I would like to parse characters in my condition. I explain : I wrote that :

function setupFolder(){
var curComp = app.project.activeItem;
var nbControlleur = 0;
for (var i = 1; i <= curComp.numLayers; i++) {
if(curComp.layers.name=="Controlleur Bounce"+i){
nbControlleur=nbControlleur+1;
alert("A controlleur is already created = "+nbControlleur);
}
}
}

My problem is at this line : if(curComp.layers.name=="Controlleur Bounce"+i){
The "+i" doesn't work, I know, but I want to know how many layers CONTENTS the words "Controlleur Bounce". Because in my comp, there are a certain numbers of these layers, and there names are "Controlleur Bounce1", "Controlleur Bounce2", "Controlleur Bounce3"etc.... Any idea?
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

It's not so much that the +i doesn't work, it's just that your logic is wrong in that it will only work if layer 1 is named "Controlleur Bounce1", layer 2 is called "Controlleur Bounce2" etc

You can used .indexOf to see if a string contains what you're looking for. It returns the character index of the first character of the search string (i.e. 0 if the string begins with the search string) or -1 if the search string isn't contained in the string.

So for example:

Code: Select all

var curComp = app.project.activeItem;
var nbControlleur = 0;
for (var i = 1; i <= curComp.numLayers; i++) {
	$.writeln("checking " + curComp.layers[i].name);
	$.writeln(curComp.layers[i].name.indexOf("Controlleur Bounce"));
	if(curComp.layers[i].name.indexOf("Controlleur Bounce") != -1) {
		nbControlleur++;
		
	}
}
alert("A controlleur is already created = "+nbControlleur);
Use the $.writeln() functionality to get it to write stuff into your ESTK console. This is very useful for examining your logic and helping to track down issues in the debugging stage. Just add // in front of those lines to disable them, so you can easily enable them again later if needed.

Paul
bkan
Posts: 51
Joined: November 6th, 2013, 8:33 am

I successes writing this :
var curComp = app.project.activeItem
var nbControlleur = 1;
var reg_exp = /Controlleur_bounce/;
for (var i = 1; i <= curComp.numLayers; i++) {
if(curComp.layers.name.match(reg_exp)){
nbControlleur=nbControlleur+1;
}
}
Post Reply