## Here's a script for sorting layers by z positions

All things .jsx

Moderator: Paul Tuersley

zold

Code: Select all

``````{
// SortByZ.jsx
//version 4 - by Christopher R. Green
// This script reorders layers in the active comp, sorted by Z Position. C'est RAPIDE!
//NOTE: It always moves locked layers*
//NOTE: It sorts the layers according to their z positions at FIRST FRAME OF COMP**
//NOTE: It ignores expressions***

var proj = app.project;
var undoStr = "Sort Layers By Z Position";

function compareNums(a, b) {return a - b;}

function sortByZ(comp_layers) {
zz = new Array;

//collect all the z positions into an array:

for (o = 1; o <= comp_layers.length; o++) {
//**value at time zero
//***false means ignore expression
zz[zz.length] = comp_layers[o].property("Position").valueAtTime(0, false)[2];
}

//sort those z positions using built-in javascript 1.1 sort method (compareNums function makes sorting numerical)
zz = zz.sort(compareNums);
allZs = zz.length;

for (n = 0;n < allZs;n++) {

clearOutput();
writeLn("Positions sorted ...");
write("Finding & moving layer " + n + " of " + allZs);

foundOne = false;
itsLocked = false;

thisZ = zz[n];

//	r is always one ahead of n because the array's first element is zero,
//	but the layers object's first element is one;

r=(n+1);

//	we increment r below because we need to ignore previous layers,
//	shorten the list on every pass
//now move layers with matching Z positions to top, in order of sorted array,

while( !foundOne ) {

if (thisZ == comp_layers[r].property("Position").valueAtTime(0, false)[2]) {
foundOne = true;

if (comp_layers[r].locked) {
itsLocked = true;//*
comp_layers[r].locked = false;//* unlocks locked layer
}

if (n == 0) {
comp_layers[r].moveToBeginning();
}else{
comp_layers[r].moveAfter(comp_layers[n]);
}

if (itsLocked) {comp_layers[n+1].locked = true;}//* re-locks layer

}else{
r=(r+1);
//just a safeguard in case, for some bizarre reason, no match is found between thisZ and all layers' z positions
if (r > allZs) {
foundOne=true;
}
}
}

}
clearOutput();

}//function sortByZ

if(proj){
var activeItem = app.project.activeItem;
if (activeItem != null && (activeItem instanceof CompItem)){
app.beginUndoGroup(undoStr);
var activeCompLayers = activeItem.layers;

sortByZ(activeCompLayers);

app.endUndoGroup();
clearOutput();
write("Sorting Complete.");
} else {
}
} else {
}
}
``````
zold
It's good to keep the info window open, by the way - especially if you have a ton of (at least several hundred) layers to sort.
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:
How complicated would it be to modify this script to work with Selected Layers instead of all the layers in the Comp?

I've been trying to sort it out myself (pun intended and have not had luck so far..
dwahlrab
Posts: 9
Joined: July 11th, 2007, 7:08 am
Location: brooklyn, ny
Hey Zold, thanks so much for this. I see this was back in 2004, but I am wondering if anyone would like to reinvigorate lloyds suggestion about selected layers in the comp, rather than the whole thing? This would be very useful to me now... thanks,
djuna
dwahlrab
Posts: 9
Joined: July 11th, 2007, 7:08 am
Location: brooklyn, ny
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:
Hey Djuna,

Try Jeff Almasol's Kinda Sorta script. It allows you to sort the selected layers in Z.

http://www.redefinery.com/ae/view.php?i ... KindaSorta

-Lloyd
dwahlrab
Posts: 9
Joined: July 11th, 2007, 7:08 am
Location: brooklyn, ny
So Jeff's script does not include an option to reorder specifically in z, but it has all the essentials... Anybody wanna take a stab at it? THANKS!
djuna
redefinery
Posts: 112
Joined: April 1st, 2005, 8:16 pm
Location: Seattle, WA
Contact:
dwahlrab wrote:So Jeff's script does not include an option to reorder specifically in z, but it has all the essentials... Anybody wanna take a stab at it? THANKS!
djuna
hi djuna,

i added a Layer Z Position option (in v1.2). note that it'll just look at the Z Position value, and won't compensate for Z Anchor Point. hope that's ok.

:jeff
dwahlrab
Posts: 9
Joined: July 11th, 2007, 7:08 am
Location: brooklyn, ny
works like a charm Jeff, thank you very much.
basem
Posts: 3
Joined: February 18th, 2014, 2:00 am
I really need this script but I don't know how to use it ..

Can you give the script as a file to add to my after effects scripts ..

I'm using after effects cc
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:
basem wrote:I really need this script but I don't know how to use it ..

Can you give the script as a file to add to my after effects scripts ..

I'm using after effects cc

Here is a newer and much more powerful sorting script: http://aescripts.com/sortie/
basem
Posts: 3
Joined: February 18th, 2014, 2:00 am
Thanks lloydalvarezØŒ

but this not what I need !

here is a quick video about what I mean

http://www.mediafire.com/watch/bllgd7sj ... roblem.mp4