Activity › Forums › Adobe After Effects › Select 100 random layers?
-
Select 100 random layers?
Posted by Don Jobs on July 13, 2017 at 10:30 amHi,
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!
Walter Soyka replied 6 years ago 4 Members · 8 Replies -
8 Replies
-
Mike Abbott
July 13, 2017 at 3:13 pm6th line down:
for (i=1; i <= myLayers.length; i++){
change to:
for (i=1; i <= 100; i++){
https://mikeabbott.info
https://vantagegraphics.co.uk -
Walter Soyka
July 13, 2017 at 4:49 pmHere’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
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive | RenderBreak [blog] | Profile [LinkedIn] -
Don Jobs
July 14, 2017 at 10:34 amI 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!
🙂 -
Walter Soyka
July 14, 2017 at 2:30 pmInstead 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
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive | RenderBreak [blog] | Profile [LinkedIn] -
Don Jobs
July 14, 2017 at 2:55 pmDear 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!
-
Walter Soyka
May 1, 2020 at 3:35 amIt’s a script, not an expression. Save it as a .jsx file, then run it through the File > Scripts menu.
Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive | RenderBreak [blog] | Profile [LinkedIn]
Reply to this Discussion! Login or Sign Up