Skip to main content
Dhal_Naka
Inspiring
January 18, 2018
Answered

For loop only ones because layers.add, why?

  • January 18, 2018
  • 1 reply
  • 631 views

I have some layers, and I want to add, from the project stack, a null before each layer so I type this:

#target aftereffects

var theComp = app.project.activeItem;

var theLayer;

var dummyNull;

var dummyLayer;

{

    for (var i = 1 ; i <= app.project.numItems; i++)

    {

        if (app.project.item(i).name == "DUMMY") dummyNull = app.project.item(i);

    }

    for (var k = 0; k < theComp.selectedLayers.length; k++)

    {

            theLayer = theComp.selectedLayers;           

            dummyLayer = theComp.layers.add(dummyNull);

            dummyLayer.moveBefore(theLayer);

            //THIS LOOP ONLY RUNS ONES, WHY?

    }

}

why does only do one loop?

This topic has been closed for replies.
Correct answer Dhal_Naka

Thanks for your reply Dan!

The same still happening but now I understand why... Even if "myLayers.length" is i.e. 4,"dummyLayer = theComp.layers.add(dummyNull);" always makes me lose my selected layers so I'll need to figure out how to select the layers again everytime I add that new layers...


fixed

#target aftereffects

var theComp = app.project.activeItem;

var theLayer;

var dummyNull;

var dummyLayer;

var selectedLayersAmount;

var layerSelection = [];

{

    //we run through the project stack looking for a certain asset

    for (var i = 1 ; i <= app.project.numItems; i++)

    {

        if (app.project.item(i).name == "DUMMY") dummyNull = app.project.item(i);

    }

    //we gather all the selected layers name to later select them by name

    for (var a = 0; a < theComp.selectedLayers.length; a++)

    {

        theLayer = theComp.selectedLayers;

        layerSelection.push(theLayer.name);

    }

    //we run through the layerSelection array making what we want

    for (var k = 0; k < layerSelection.length; k++)

    {

            theLayer = theComp.layer(layerSelection.toString()); // we run through all the selected layers    

            dummyLayer = theComp.layers.add(dummyNull); //we add the DUMMY null to the scene

            dummyLayer.moveBefore(theLayer); //we move it before the selected layer

            dummyLayer.name = (theLayer.name + "_new"); //we change the name of the new layer to have the name of the ball

            dummyLayer.parent = theComp.layer("MAIN_NULL"); // we set up the parent

    }

}

Dhal_Naka
Dhal_NakaAuthor
Inspiring
January 18, 2018

Maybe it's because when I add the new layer, the selectedLayers.length change from X to 1?

Dan Ebberts
Community Expert
Community Expert
January 18, 2018

Yes. Save your selected layers into a variable, then loop through that:

var theComp = app.project.activeItem; 

var theLayer; 

var dummyNull; 

var dummyLayer; 

 

 

    for (var i = 1 ; i <= app.project.numItems; i++) 

    { 

        if (app.project.item(i).name == "DUMMY") dummyNull = app.project.item(i); 

    } 

 

   var myLayers =   theComp.selectedLayers;

    for (var k = 0; k < myLayers.length; k++) 

    { 

            theLayer = myLayers;             

            dummyLayer = theComp.layers.add(dummyNull); 

            dummyLayer.moveBefore(theLayer); 

    } 

}

Dan

Dhal_Naka
Dhal_NakaAuthor
Inspiring
January 18, 2018

Thanks for your reply Dan!

The same still happening but now I understand why... Even if "myLayers.length" is i.e. 4,"dummyLayer = theComp.layers.add(dummyNull);" always makes me lose my selected layers so I'll need to figure out how to select the layers again everytime I add that new layers...