Page 1 of 1

Reverse Selected Layer Order script

Posted: June 7th, 2005, 4:45 am
by w_m0zart
I just made a script which reverses the order of selected layers.
It is just a little variation on the Reverse Layer Order, but with an extra selection option. Now it's possible to select these layers which have to be reversely ordered.

The difficult part was to find out that layers in a selection appear in an order, which depend on the order they have been selected. If we do not sort the index from these layers, the reordening proces could easily give a mess.

Originally I wrote:

Code: Select all

...
for(j = 1; j <= length; j++)  // collect in array SNA all layers which should be reordered
{
   SNA[j]=myComp.selectedLayers[j-1].index;
}

for(j = 1; j < length; j++)   // sort elements ascending; SNA[1] holds lowest value
{
   pL=length;
   do
   {
      if (SNA[j]>SNA[pL])     // swap elements to get lowest of this pair first
      {
         w=SNA[pL];
         SNA[pL]=SNA[j];
         SNA[j]=w;
      }
   } while (--pL > j);
}
...
Later on I found ECMAScript contains already a method to sort arrays. So I modified the code to:

Code: Select all

...
	function comparefn(x, y) {return x - y;} 
...
	function reverseSelectedLayerOrder(myComp){
		var comp_layers = myComp.layers;
		var length 	= myComp.selectedLayers.length;

		var SNA 	= new Array();
		var pL;
		var w;

		if (length > 1) 			// only process 2 or more selected layers
		{
			for(j = 0; j < length; j++)	// collect in array SNA all layers which should be reordered
			{
				SNA[j]=myComp.selectedLayers[j].index;
			}

			SNA=SNA.sort(comparefn);	// sort elements ascending; SNA[0] holds lowest value
...
The script may be downloaded from the following location:

http://www.nijdam.de/aescripts/ReverseS ... rOrder.zip

Posted: June 7th, 2005, 8:22 am
by Shinjipierre
erm.... in fact, you can do that with copy/paste... if you select your layers from the bottom ^^

Posted: June 7th, 2005, 11:09 am
by w_m0zart
I think you mean the order in which the layers have been selected, will determine in which way they will be pasted. With this script it doesn't matter in which way they are selected. Regardless of its selection order, they will be always reversely ordered after running the script.