Skip to main content
mtv077277126
Inspiring
April 5, 2017
Question

Duplicate multiple layers with the same name

  • April 5, 2017
  • 1 reply
  • 986 views

I have a Text and Solid layer that are labelled Bar 1. I want to duplicate both at the same time. I know I can highlight and hit Ctrl + D, but I am writing a script where I might as well have it as a part of my button onClick function. So that the layers duplicate and then using the script apply new data to these.

I can duplicate using this.

var myComp = app.project.activeItem;

myComp.layer("Bar 1").duplicate();

But it only recognises the first Bar 1 layer. So I tried a for loop.

var myComp = app.project.activeItem;

for ( var i = 1; i <= myComp.numLayers; i++){

   var myComp = app.project.activeItem;

    myComp.layer("Bar 1").duplicate();

    };

this worked and crashed after effects as it wouldn't stop duplicating

So I included a break.

var myComp = app.project.activeItem;

for ( var i = 1; i <= myComp.numLayers; i++){

   var myComp = app.project.activeItem;

    myComp.layer("Bar 1").duplicate();

    break;

};

But now I am back to square one, it will only do the first one in the chain rather than both.

I know I'm missing something silly, but I was hoping someone could point it out please.

This topic has been closed for replies.

1 reply

Dan Ebberts
Community Expert
Community Expert
April 5, 2017

When you duplicate layers, you're messing with the number of layers and the order. Better to harvest the layers first. Something like this:

var myComp = app.project.activeItem;

var myArray = [];

for (var i = 1; i <= myComp.numLayers; i++){

  if (myComp.layer(i).name == "Bar 1"){

    myArray.push(myComp.layer(i));

  }

}

for (var i = 0; i < myArray.length; i++){

  myArray.duplicate();

}

Dan

mtv077277126
Inspiring
April 5, 2017

Why is it that as soon as I think I am understanding this script stuff... Another intriguing puzzle befalls me.

That does the trick Dan.

The problem now is how do I get it to duplicate each one so that it goes.

1: Bar 32: Bar 2

3: Bar 1

4: Bar 3

5: Bar 2

6: Bar 1

mtv077277126
Inspiring
April 5, 2017

Hey Dan,

I still have to thank you because it was an old expression that you gave me when I wanted to organise and rename layers. So I thought to use that in the script and it works the way I want it too. If you know an easier way of scripting this, that would be awesome. But seen as the duplication includes the slider with expression that gives it the numerical value I need for it to reference and rename itself. This should do anyways.

var myComp = app.project.activeItem;

var myArray = [];

for (var i = 1; i <= myComp.numLayers; i++){

  if (myComp.layer(i).name == "Bar 1"){

    myArray.push(myComp.layer(i));

  }

}

for (var i = 0; i < myArray.length; i++){

  myArray.duplicate();

}

str1 = "Bar";

for (var i = 1; i <= myComp.numLayers; i++){  

if (myComp.layer(i).name.substr(0,str1.length) == str1){

    try {

myComp.layer(i).name = str1 +" "+ myComp.layer(i).effect("bar_index")("Slider").value;

} catch(err) {

    myComp.layer(i).name = str1 +" "+ myComp.layer(i).effect("text_index")("Slider").value;

}

}

}