Highlighted

How to writing a loop that detect text layer also within precomps

New Here ,
Apr 22, 2020

Copy link to clipboard

Copied

I'm writing a script for localization a project, and I need the script to go over all the layer in the composition, check if them are text layers and change their value, and if there are precomps layers go inside does precomps and do the exact same thing.

 

Any advice on how to write somorhing like that ? 

Topics

How to, Scripting

Views

477

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to writing a loop that detect text layer also within precomps

New Here ,
Apr 22, 2020

Copy link to clipboard

Copied

I'm writing a script for localization a project, and I need the script to go over all the layer in the composition, check if them are text layers and change their value, and if there are precomps layers go inside does precomps and do the exact same thing.

 

Any advice on how to write somorhing like that ? 

Topics

How to, Scripting

Views

478

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 22, 2020 0
Most Valuable Participant ,
Apr 23, 2020

Copy link to clipboard

Copied

You can easily find comps by matching names, which is the only surefire way, anyway. If you don't adhere to specific naming conventions, AE doesn't discriminate nor care whether a comp is actualyl used as a pre-comp somewhere or not. Of course you can still detect whether a layer is a pre-comp, but there is no unambiguous one-way relationship between a pre-comp and its parent comp.

 

Mylenium

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

I think I need to do it with a recurisve function. 

One that detect the layer type, if it is a text layer apply text change function and if it is a composition apply the same detection function again, but I having some trouble in writing that script, can't figuire our how to determin the layer type 

 

function checkLayers(){
var comp = app.project.activeItem;

for(var i = 0 ; i < comp.numLayers; i++){
var layer = comp.layer[i];
if(layer.property("sourceText") !== null){
alert("sdf");
changeComp(fileData, "GR");
}

}

}


changeComp(fileData, "GR");
}

}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Guide ,
Apr 23, 2020

Copy link to clipboard

Copied

The best solution for this is to have a function with a compitem as argument. This can be app.project.activeItem (the comp you clicked on in project panel), as well as a specific compostion as well as all compositions in project.

 

In this function, you have a for-loop starting at i = 0 and counting to number of layers in this compitem. After defining the compitem is you use compitem.layers.length to get the number of layers and compitem.layers to get the layer-array, if I remember correctly.

 

Next you have an if-condition to check, if the current layers[i] is a text layer and if so, you can do your alternation.

"instanceof Text" or somthing like that should do the check.

 

With a second if-condition, you check if the layer is a precomp by "layers[i].source instanceof CompItem" and if so, you call the function again, passing layers[i].source as argument.

 

This way you can go into every precomp, no matter how deep it is in the hierachy.

 

Check out the scripting api for more details:

http://docs.aenhancers.com/

 

It's pretty much just finding the correct puzzle pieces and putting them together.

 

*Martin

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

Thanks for you help. 

But I still doesn't understand why this function doesn't work 

 

function checkForTextLayers(){
var comp = app.project.activeItem;

for(i = 1 ; i < comp.numLayers; i++){
//alert("Hello"+i);
var layer = comp.layer[i];
if(layer.property("Source Text") !== null){
alert("sdf");
changeComp(fileData, "GR");
}

}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Guide ,
Apr 23, 2020

Copy link to clipboard

Copied

Maybe because of the syntax error: !==, which should be !=.

 

I wouldn't hang on this, you can check for textlayers directly.

 

*Martin

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

I fixed this error and it is still doesn't work

 

function checkForTextLayers(){
var comp = app.project.activeItem;

for(i = 1 ; i < comp.numLayers; i++){
//alert("Hello"+i);
var layer = comp.layer[i];
if(layer.property("Source Text") != null){
alert("is Text");
changeComp(fileData, "GR");
}

}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

Changed the comp.layer[i] to (i) , works now, Thanks 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

Basiclly I understood that I need to do a recursive function. 

and I wrote this function. I'm still missing the piece for to precomp issue that I can't figure out, please adivse 

 

function checkForTextLayers(){
var fileData;
fileData = readJson();
var comp = app.project.activeItem;
for(i = 1 ; i < comp.numLayers; i++){
//alert("Hello"+i);
var layer = comp.layer(i);
if(layer.property("Source Text") != null){
alert("is Text");
changeComp(fileData, "GR");
}if(layer instanceof CompItem){

checkForTextLayers();

}
}
}

 

 

Thanks 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Guide ,
Apr 23, 2020

Copy link to clipboard

Copied

You need to pass an argument to that function.

Somethink like this:

 

var argument = app.project.activeItem;

 

function checkForTextLayers(argument){

var comp = argument;

// code, code code

if (layer instanceof CompItem){

   checkForTextLayers(layer.source);

};

};

 

*Martin

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

First of all Thanks so much for your help, 

Second, I tried it and it doesn't work 

 

var comp = app.project.activeItem;
function checkForTextLayers(comp){
var fileData;
fileData = readJson();

for(i = 1 ; i < comp.numLayers; i++){
//alert("Hello"+i);
var layer = comp.layer(i);
if(layer.property("Source Text") != null){
alert("is Text");
changeComp(fileData, "GR");
}if(layer instanceof CompItem){

checkForTextLayers(layer.source);

}
}
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Guide ,
Apr 23, 2020

Copy link to clipboard

Copied

Yeah, sorry, I made an edit - see my post now - this should work.

 

But please do some research on yourself! I'm writing the code from memory and haven't written a script for years. Doin't copy/paste the code and expect it working on first try. 

 

*Martin

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

I'm new at this and I don't have a programimng background so it take me time ... 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

And still it doesn't work ...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Guide ,
Apr 23, 2020

Copy link to clipboard

Copied

That's okay! Take your time.

I had to learn all of this, too - spending hours over hours with the API documentation and web searches. Finding, trying, understanding, editing code snippets I found online was my main learning ressource, and that's what I'm still doing, when I code.

 

*Martin

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Guide ,
Apr 23, 2020

Copy link to clipboard

Copied

It's

 

if (layer.source instanceof CompItem){};

 

*Martin

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

Still can't figure out this 

 

var activeComp = app.project.activeItem;
function checkForTextLayers(activeComp){
var comp = activeComp;
var fileData;
fileData = readJson();

for(i = 1 ; i < comp.numLayers; i++){
//alert("Hello"+i);
var layer = comp.layer(i);
if(layer.property("Source Text") != null){
alert("is Text");
changeComp(fileData, "GR");
}if(layer.source instanceof CompItem){

checkForTextLayers(layer.source);

}
}
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Guide ,
Apr 23, 2020

Copy link to clipboard

Copied

Looks good to me.

If the script hangs, you'll have to find the problematic line and inspect the bug.

Next to  researching, that's the other main activity when you code 😉

 

If you tell me what exactly isn't working and how your test enviroment looks like and how you do the test actually, I might can help (for instance, you have to click on a comp before executing the script, otherwise activeItem is not set or set so something else).

 

*Martin

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

So the Script now works partially, it recognize the pre-comp. 

But it's not doing the checkForTextLayers inside it 

I do debugging with alrets to see were the code gets stuck. 

and currently writing the script on atom

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Guide ,
Apr 23, 2020

Copy link to clipboard

Copied

Try

function checkForTextLayers(activeComp){

alert(activeComp.name)

...

}

to see if the comps are passed correctly. You should get 2 alerts with different names.

 

Let us know, if you found the issue!

 

*Martin

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Adobe Community Professional ,
Apr 23, 2020

Copy link to clipboard

Copied

Also note that existing tools like Text2Spreadsheet might already do what you need

https://aescripts.com/text2spreadsheet/

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

It looks like a great script, But I need to reaplce files too, and all with one button click, So i'm trying to write somthing customize to my workflow

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0