Here's a script for sorting layers by z positions

All things .jsx

Moderator: Paul Tuersley

Post Reply
zold

April 25th, 2005, 12:04 pm

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 {
			alert("Please select an active comp to use this script");
		}
	} else {
		alert("Please open a project first to use this script.");
	}
}
zold

April 26th, 2005, 2:33 pm

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.
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

July 7th, 2006, 8:57 am

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

April 27th, 2008, 1:27 pm

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

April 27th, 2008, 1:28 pm

oops, 2006. my bad.
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

April 27th, 2008, 1:34 pm

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

April 27th, 2008, 4:44 pm

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
User avatar
redefinery
Posts: 112
Joined: April 1st, 2005, 8:16 pm
Location: Seattle, WA
Contact:

April 27th, 2008, 8:59 pm

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

April 28th, 2008, 8:30 am

works like a charm Jeff, thank you very much.
basem
Posts: 3
Joined: February 18th, 2014, 2:00 am

February 18th, 2014, 9:51 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
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

February 18th, 2014, 10:03 am

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

February 18th, 2014, 1:32 pm

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
Post Reply