FORUMS: list search recent posts

Select 100 random layers?

FAQ   •   VIEW ALL
 Select 100 random layers? by Don Jobson Jul 13, 2017 at 10:30:31 am

Hi,

I have this script and it selects random number of layers:

function selectLayers() {
var myLayers = myComp.layers;
min=1;
max=myLayers.length;
var myRandom = new Array();
for (i=1; i <= myLayers.length; i++){
myRandom[i-1] = Math.round((max-min)*Math.random());
myLayers[i].selected = false;
}
for (j=0; j <= myRandom.length-1; j++){
myLayers[myRandom[j]].selected = true;
}
}
selectLayers();

How can I make it to select only 100 random layers?

Thank You very much for your help! ☺

Regards!

 Re: Select 100 random layers?on Jul 13, 2017 at 3:13:59 pm

6th line down:

for (i=1; i <= myLayers.length; i++){

change to:

for (i=1; i <= 100; i++){

http://mikeabbott.info
http://vantagegraphics.co.uk

 Re: Select 100 random layers?on Jul 13, 2017 at 4:49:32 pm

Here's how I would approach it:

function randomIndexSequence(numIndices) {
// create a blank array
indexArray = [];

// fill the array with all the values from 1 to the numIndices argument to this function
for (i = 0; i < numIndices; i++) indexArray.push(i+1);

// shuffle the array by stepping through and swapping the value at each sequential position with a random position
for (i = 0; i < indexArray.length; i++) {
j = Math.floor(Math.random() * indexArray.length);
swap = indexArray[j];
indexArray[j] = indexArray[i];
indexArray[i] = swap;
}

return indexArray;
}

function selectRandomLayers(layers, numLayers) {
// get shuffled list of layers
shuffledLayers = randomIndexSequence(layers.length);

// step through the list of shuffled layers, and select the first n layers in that list, where n is the numLayers argument passed to this function
for (i = 0; i < numLayers; i++) {
if (shuffledLayers[i] <= layers.length) layers[(shuffledLayers[i])].selected = true;
}
}

myComp = app.project.activeItem;
selectRandomLayers(myComp.layers, 100);

Walter Soyka
Motion Graphics, Widescreen Events, Presentation Design, and Consulting

 Re: Select 100 random layers?by Don Jobson Jul 13, 2017 at 7:38:18 pm

I love you guys!!!

This is beautiful!

Thanks a lot! :)

 Re: Select 100 random layers?by Don Jobson Jul 14, 2017 at 10:34:04 am

I have one more issue please ☺

Now I see that it works with all the layers in the composition including layers with the Shy switch On (Invisible in the list)
I would love to make it work with only visible layers in the composition list.

If it's too much work, this is already perfect!

Thanks again!
:)

 Re: Select 100 random layers?on Jul 14, 2017 at 2:30:17 pm

Instead of building an array of all the layer indices to shuffle, let's build a list of only non-shy layer indices. Then we'll shuffle that:

function shuffleNonShyLayerIndices(layers) {
// create a blank array
indexArray = [];

// add each non-shy layer index to the array
for (i = 1; i <= layers.length; i++) {
if (layers[i].shy == false) indexArray.push(i);
}

// shuffle the array of non-shy layer indices by stepping through and swapping the value at each sequential position with a random position
for (i = 0; i < indexArray.length; i++) {
j = Math.floor(Math.random() * indexArray.length);
swap = indexArray[j];
indexArray[j] = indexArray[i];
indexArray[i] = swap;
}

return indexArray;
}

function selectRandomLayers(layers, numLayers) {
// get shuffled list of layer indices
shuffledLayerIndices = shuffleNonShyLayerIndices(layers);

// step through the list of shuffled layers indices, and select the first n layers in that list, where n is the numLayers argument passed to this function
for (i = 1; i <= numLayers; i++) {
if (shuffledLayerIndices[i] <= layers.length) layers[(shuffledLayerIndices[i])].selected = true;
}
}

myComp = app.project.activeItem;
selectRandomLayers(myComp.layers, 100);

Walter Soyka
Motion Graphics, Widescreen Events, Presentation Design, and Consulting

 Re: Select 100 random layers?by Don Jobson Jul 14, 2017 at 2:55:09 pm

Dear Walter,

thank you very very much!!!

I was really stuck and now I can proceed with my project...

It's working!!! (Off course ;))

Thanks again!