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

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