ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Search for a layer in comp

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Hus Yaralioglu
Search for a layer in comp
on Feb 6, 2019 at 1:17:35 pm

I'm trying to create a script that will search for a specific layer within a selected comp.

I want the script to work like a toggle basically, so if it finds an existing layer with the name "XYZ" I want it to delete this layer. Whereas if it doesn't find a layer with this name, I want it to create a new layer with the name "XYZ"

I have managed to get the script working so it adds a layer, I just can't find a way for it to search the comp and delete any existing ones.

Any suggestions would be so helpful!

var curSelection = app.project.activeItem;

//check if selected layer is a comp
if (curSelection instanceof CompItem) {

//run code here to create layer with name XYZ

} else if (//search comp for layer with name XYZ) {
//run code to remove layer

}else
alert("this is not a comp!");


Return to posts index

Andrei Popa
Re: Search for a layer in comp
on Feb 6, 2019 at 3:05:00 pm

Here you go.

var myLayer = searchLayer ("XYZ");

function searchLayer(layerName){
var myComposition = app.project.activeItem;
if(myComposition instanceof CompItem){
for (var i = 1; i<=myComposition.numLayers;i++){
if (myComposition.layer(i).name == layerName){
myComposition.layer(i).remove();
return null;
}
}
return myComposition.layers.addText(layerName);
}else{
alert("This is not a composition");
}
}


The "myLayer" c=variable is null if it just deleted a text or it is the actual text layer if it just created one. You can change the "addText" function if you want to add different layers.

Andrei
My Envato portfolio.


Return to posts index

Hus Yaralioglu
Re: Search for a layer in comp
on Feb 25, 2019 at 2:05:02 pm

This works great! Thank you so much!

It only works if 1 comp is selected though. If I have 2 or more comps selected it doesn't work.

If I have projects with 10+ comps I don't want to have to run the script for each one separately.

Any idea how I could adapt your script to work on multiple comp selections? I've tried to get it to work but haven't had much luck.

Many thanks,
Hus


Return to posts index


Andrei Popa
Re: Search for a layer in comp
on Feb 25, 2019 at 2:17:13 pm

You just need to go through the selection and apply this function to each composition. Replace the "XYZ" in the second row with the name of your choice. This will also skip anything selected that is not a composition and allert you about it.

for (var i=0; i< app.project.selection.length; i++){
searchLayer("XYZ",app.project.selection[i]);
}

function searchLayer(layerName, myComposition){
if(myComposition instanceof CompItem){
for (var i = 1; i<= myComposition.numLayers;i++){
if (myComposition.layer(i).name == layerName){
myComposition.layer(i).remove();
return null;
}
}
//return myComposition.layers.addText(layerName);
}else{
alert(myComposition.name +" is not a composition. Will skip it.");
}
}


Andrei
My Envato portfolio.


Return to posts index

Hus Yaralioglu
Re: Search for a layer in comp
on Mar 6, 2019 at 9:39:45 am

This worked perfectly! Thank you very much!


Return to posts index

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