Page 1 of 1

Here's a script for sorting layers by z positions

Posted: April 25th, 2005, 12:04 pm
by 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 {
			alert("Please select an active comp to use this script");
		}
	} else {
		alert("Please open a project first to use this script.");
	}
}

sorting layers by z positions - postscript

Posted: April 26th, 2005, 2:33 pm
by 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.

Posted: July 7th, 2006, 8:57 am
by lloydalvarez
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..

Re: Here's a script for sorting layers by z positions

Posted: April 27th, 2008, 1:27 pm
by dwahlrab
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

Re: Here's a script for sorting layers by z positions

Posted: April 27th, 2008, 1:28 pm
by dwahlrab
oops, 2006. my bad.

Re: Here's a script for sorting layers by z positions

Posted: April 27th, 2008, 1:34 pm
by lloydalvarez
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

Re: Here's a script for sorting layers by z positions

Posted: April 27th, 2008, 4:44 pm
by dwahlrab
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

Re: Here's a script for sorting layers by z positions

Posted: April 27th, 2008, 8:59 pm
by redefinery
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

Re: Here's a script for sorting layers by z positions

Posted: April 28th, 2008, 8:30 am
by dwahlrab
works like a charm Jeff, thank you very much.

Re: Here's a script for sorting layers by z positions

Posted: February 18th, 2014, 9:51 am
by basem
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

Re: Here's a script for sorting layers by z positions

Posted: February 18th, 2014, 10:03 am
by lloydalvarez
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/

Re: Here's a script for sorting layers by z positions

Posted: February 18th, 2014, 1:32 pm
by basem
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