Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Search for a layer in comp

  • Search for a layer in comp

    Posted by Hus Yaralioglu on February 6, 2019 at 1:17 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!");

    Hus Yaralioglu replied 7 years, 6 months ago 2 Members · 4 Replies
  • 4 Replies
  • Andrei Popa

    February 6, 2019 at 3:05 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.

  • Hus Yaralioglu

    February 25, 2019 at 2:05 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

  • Andrei Popa

    February 25, 2019 at 2:17 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.

  • Hus Yaralioglu

    March 6, 2019 at 9:39 am

    This worked perfectly! Thank you very much!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy