ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Ae Scripting - Iterate through text layers with same name, only working on first layer with that name

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Paul Beckett
Ae Scripting - Iterate through text layers with same name, only working on first layer with that name
on Dec 28, 2019 at 2:12:15 pm

Hi,

I have been slowly learning scripting and have encountered a problem which I am not sure how to resolve and hoped that the good people of CC might be willing to give me some pointers.


The problem is this, I have a project with three compositions and in each comp there are ten text layers with the same name in each comp, comp1 = "myTextLayer1" , comp2 = "myTextLayer2" , comp3 = "myTextLayer3"

I have written a small script to batch change each text layer in each comp, it does the following:

1. Loops through all comps in the project.
2. Finds 'text layer' type of layers.
3. If the text layer name matches to "myTextLayer1" for example it changes the font and style properties to the ones defined in the script.

So far, so good....

However the script only changes the first text layer in each comp with the matching name and skips the remaining nine identically named text layers.

What I would like it to do is continue through the remaining layers with that name until it has changed them all.

I think I know where it is going wrong. To my inexperienced eye it is iterating through each layer in the comp until it reaches the first text layer which matches the criteria, changes the font and style as intended, then iterates out of that comp to the next one and thus not checking the remaining text layers in the current comp.

I think I need to get it to iterate through the comp before completing the first if statement, I'm just not sure how to achieve that here.

My code is below - it is only a snippet, there is more code which deals with other font styles and UI but I think this snippet is essentially where it is going wrong...

Many thanks in advance!

P.

//Main logic

//Conditional logic for line 1 font style

dropList1.onChange = function() {

//DropList1 index 0 logic line 1

if (dropList1.selection.index == 0) {

//search through every comp
for (var i = 1; i &lt;= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem) {



//alert(compName + " is a comp");

//...and in every layer
for (j = 1; j &lt;= app.project.item(i).layers.length; j++) {
if (app.project.item(i).layer(j).name === "myTextLayer1") {
var textProp = app.project.item(i).layer("myTextLayer1").property("Source Text");
var textDocument = textProp.value;
textDocument.font = "myFontRegular";
textDocument.fontSize = 140;

textProp.setValue(textDocument);

};

if (app.project.item(i).layer(j).name === "myTextLayer2") {
var textProp2 = app.project.item(i).layer("myTextLayer3").property("Source Text");
var textDocument2 = textProp2.value;
textDocument2.font = "myFontBold";
textDocument2.fontSize = 140;

textProp2.setValue(textDocument2);

};

if (app.project.item(i).layer(j).name === "myTextLayer3") {
var textProp3 = app.project.item(i).layer("myTextLayer3").property("Source Text");
var textDocument3 = textProp3.value;
textDocument3.font = "myFontItalic";
textDocument3.fontSize = 140;

textProp3.setValue(textDocument3);

//alert("Found text layer");

}
}
}
};

//alert("Done")

};


Return to posts index

Dan Ebberts
Re: Ae Scripting - Iterate through text layers with same name, only working on first layer with that name
on Dec 28, 2019 at 8:59:53 pm

A couple of things-- instead of this:

app.project.item(i).layers.length

I'd use this:

app.project.item(i).numLayers

Also, your section that processes myTextLayer2 uses the text from myTextLayer3.

Dan



Return to posts index

Paul Beckett
Re: Ae Scripting - Iterate through text layers with same name, only working on first layer with that name
on Dec 29, 2019 at 7:28:05 pm

Hi Dan,

Thanks for the quick reply and the help. I've tried what you suggested and also corrected my type-o, however the problem persists. It works fine on the first instance of the text layer named "myTextLayer1" but skips the remaining layers in the comp with the exact same name...

I think something is causing the code, after it has executed on the first instance of the text layer, to jump into next comp before it iterates through the remaining layers, I just can't figure out where that is happening at the moment.

If you had any further thoughts on this I'd really appreciate it, in the meantime I'll keep on chipping away at this problem and hopefully find the answer.

Thanks again,

P.


Return to posts index


Dan Ebberts
Re: Ae Scripting - Iterate through text layers with same name, only working on first layer with that name
on Dec 29, 2019 at 8:12:36 pm

Ah, ok. In the three places where you have lines like this:

var textProp = app.project.item(i).layer("myTextLayer1").property("Source Text");

change the layer name to j, like this:

var textProp = app.project.item(i).layer(j).property("Source Text");

When you access a layer by name, you'll always the the first layer with that name.

Dan



Return to posts index

Paul Beckett
Re: Ae Scripting - Iterate through text layers with same name, only working on first layer with that name
on Dec 29, 2019 at 8:46:37 pm

Hi Dan,

Thank you again for the quick reply - I will give that a go.

Cheers,

P.


Return to posts index

Paul Beckett
Re: Ae Scripting - Iterate through text layers with same name, only working on first layer with that name
on Dec 30, 2019 at 5:44:35 am

Hi Dan,

That works great, thank you. Another hole in my understanding filled.

Cheers,

P.


Return to posts index

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