iterating through selected layers in reverse order

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
ScottG
Posts: 38
Joined: March 19th, 2006, 2:45 am

sounds simple, but is confusing me.
i thought something like this.

var layerIndices = new Array();
for (var i = layerIndices[layerIndices.length]; i >= 1; i--)
{
layerIndices[layerIndices.length] = myLayers.index;
myLayers.copyToComp(layerComp);
}

but of course i wasn't thinking properly. this is twisting my brain inside out.

can anyone give me an example of how to iterate through selected layers in reverse order?
thankyou in advance...

in the meantime, i shall keep searching/trying to work out the logic behind this.
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

Should it not look like this? I think you just need to set your "i" var to the length of the layers and set the "layerIndices" to the selected layers instead of any empy array.

Code: Select all

var layerIndices = app.project.selection;
for (var i = layerIndices.length; i >= 1; i--)
{
layerIndices[layerIndices.length] = myLayers[i].index;
myLayers[i].copyToComp(layerComp);
} 
I'm not sure what you are trying to do with the code inside the for loop. Is that part giving you trouble too?
ScottG
Posts: 38
Joined: March 19th, 2006, 2:45 am

hey byron,

nope, that code doesn't do it.
what i'm trying to do is make my precomp to eact duration script more robust.

i need to take the selected layers, and copyToComp, and i want them copied from the bottom up, so that they maintain their original order in the new comp (as when you copy, it places things on the top of the stack, so by iterating normally, my layer order is reversed).

the problem is that if i have non-contigous layers selected, ie, layers 5, 12, 18, 23, my layerIndices.length will be 4, so the script only runs throuhg indices up to 4, ignoring that the selected layers have indices going much higher than that.

ergh, this is still really cofusing me.

i was creating the array as i did, because i was taking dan ebberts as a model.
http://www.motionscript.com/ae-scriptin ... r-dur.html

i also use a similar method (again taken from dan ebberts) to find the in point of the earliest layer in the timeline, and the outpoint of the latest layer.

i only hit problems when i wanted to iterate in reverse order. still trying to make it work...
ScottG
Posts: 38
Joined: March 19th, 2006, 2:45 am

also, from looking at redefinery.com, iterating through items in reverse isn't a problem for me, as long as i'm iterating through ALL items in reverse.

jeff gives an example of iterating in reverse here.
http://www.redefinery.com/ae/fundamenta ... omp_delete

as i said, it's iterating through a selection of items with non-contiguous indexes that's stumping me.
ScottG
Posts: 38
Joined: March 19th, 2006, 2:45 am

this works fine for selected layers that are non-contigous:

Code: Select all

var layerIndices = new Array();
					for (var i = 0; i < myLayers.length; i++)
					{
						layerIndices[layerIndices.length] = myLayers[i].index;
						myLayers[i].copyToComp(layerComp);
					} 
except that once i open up the comp "layerComp", the layers' order is reversed.

we create an array, and build the array by looping through the collection of selected layers and picking up their layer index values and adding them to the array.
so why doesn't it work in reverse??
googling is getting me nowhere.
ScottG
Posts: 38
Joined: March 19th, 2006, 2:45 am

I GOT IT!!!!!!!!!!
and it was by no means intuitive. for me anyway, but i've already said that i don't have a programming brain.

Code: Select all

var layerIndices = new Array(); 
               for (var i = 0; i < myLayers.length; i++) 
               { 
                  layerIndices[layerIndices.length] = myLayers[i].index; 
               } 
then

Code: Select all

for (var i=(layerIndices.length -1); i >= 0; i--)
{
foo = layerIndices[i];
myComp.layers[foo].copyToComp(layerComp);
}
success!!!!!!!!!!!!!!!!!!!
Post Reply