Activity › Forums › Adobe After Effects Expressions › Randomize Layer Order
-
Dan Ebberts
February 22, 2011 at 1:04 amThe only thing I can think of is that some editors have trouble with the “<=” when it’s copied from a web page. Other than that, I have no idea.
Dan
-
S.j. Deluise
July 30, 2011 at 3:47 pmWow, first time ever using the script editor, didn’t even know it existed.
I cut and pasted below, saved it and ran it.
It’s highlighting line 7 with a message at the bottom “expected ;”Cut and past from the browser to textedit, then to the script editor
{
var myComp = app.project.activeItem;
var n = myComp.numLayers;
var myLayers = [];
var myIdx = [];
for (var i = 1; i
myIdx[i-1] = i;
myLayers[i-1] = myComp.layer(i);
}var idx;
var temp;
for (var i = 0; i
idx = i + Math.floor(Math.random()*(myIdx.length - i));
temp = myIdx[i];
myIdx[i] = myIdx[idx];
myIdx[idx] = temp;
}
for (var i = 0; i
myLayers[myIdx[i]-1].moveToBeginning();
}
}
People say my motion graphics are moving.
-
Dan Ebberts
July 30, 2011 at 3:59 pmYou only have part of the script pasted in. Pay particular attention to lines 6, 13, and 19 because those are likely to get mangled along the way.
{
var myComp = app.project.activeItem;
var n = myComp.numLayers;
var myLayers = [];
var myIdx = [];
for (var i = 1; i <= n; i++){
myIdx[i-1] = i;
myLayers[i-1] = myComp.layer(i);
}var idx;
var temp;
for (var i = 0; i < myIdx.length; i++){
idx = i + Math.floor(Math.random()*(myIdx.length - i));
temp = myIdx[i];
myIdx[i] = myIdx[idx];
myIdx[idx] = temp;
}
for (var i = 0; i < myIdx.length; i++){
myLayers[myIdx[i]-1].moveToBeginning();
}
}
Dan
-
S.j. Deluise
July 30, 2011 at 4:11 pmThat works great. So incredible. Sure beats randomly dragging layers around.
Thanks Dan!People say my motion graphics are moving.
-
Scott Skaja
December 10, 2012 at 7:05 pmtried this but had an error in line 7. Any thoughts?
scott skaja
tangletown post – edit•animate•design•composite•color correct
tangletownpost.com -
Dan Ebberts
December 10, 2012 at 7:59 pmWhat was the error message? Did you have a comp selected?
Dan
-
Juan Ibanez
June 6, 2013 at 11:35 pmHello Dan, maybe you know how to order the layers by taking into account their “in” time, I mean, I want to order my layers so that the ones that are “new born” are on top. I know new layers are created on top, but the thing is that I want to do this after creating a bunch of layers and having them arranged in time.
Thank you so much.
-
Dan Ebberts
June 7, 2013 at 12:05 amSee if this does what you want:
{
function compareInPoints(a,b){
return a.inPoint - b.inPoint;
}
var myComp = app.project.activeItem;
var myLayers = [];
for (var i = 1; i <= myComp.numLayers; i++){
myLayers.push(myComp.layer(i));
}
myLayers.sort(compareInPoints);
for (var i = 0; i < myLayers.length; i++){
myLayers[i].moveToBeginning();
}
}
Dan
-
Peter Stone
July 25, 2014 at 8:59 amHow would I use this to randomize the order of groups within the contents of a shape layer instead of layers within a comp? Seems like it should be the same principle but I am new to scripting.
Thanks,
Peter{
var myComp = app.project.activeItem;
var n = myComp.numLayers;
var myLayers = [];
var myIdx = [];
for (var i = 1; i<= n; i++){
myIdx[i-1] = i;
myLayers[i-1] = myComp.layer(i);
}var idx;
var temp;
for (var i = 0; i < myIdx.length; i++){
idx = i + Math.floor(Math.random()*(myIdx.length – i));
temp = myIdx[i];
myIdx[i] = myIdx[idx];
myIdx[idx] = temp;
}
for (var i = 0; i < myIdx.length; i++){
myLayers[myIdx[i]-1].moveToBeginning();
}
} -
Dan Ebberts
July 25, 2014 at 5:23 pmIf it’s a property type that can be moved, you should be able to use moveTo(). For example, I just used this to move a rectangle to the top of a stack of shapes:
app.project.item(1).layer(1).property(“ADBE Root Vectors Group”).property(2).moveTo(1)
Dan
Reply to this Discussion! Login or Sign Up