ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Sort array/list by Markers

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Allan Ricarte
Sort array/list by Markers
on Apr 29, 2019 at 11:52:46 pm

Hi guys,

Basically I'm trying to sort the array/list of layers in the order that they triggered by the marker, but put them at the end of the array/list

i've tried to use ".push" in the code below, but probably i'm missing something, it didnt work as intended.

If you guys have any idea to fix it, i'll really be grateful for the help.

numPage = 10;
for (k = 1; k &lt;= numPage ; k++){
book = [];

for (i = 1; i &lt;= numPage; i++){
m = thisComp.layer("page "+i).marker
mL = thisComp.layer("page "+i).name
n = 0;
if (m.numKeys > 0){
n = m.nearestKey(time).index;
if (m.key(n).time > time) n--;
}

if ( n > 0 && m.key(n).comment == "page"){
book.push(mL);
break;
}
}
}

book


Return to posts index

Filip Vandueren
Re: Sort array/list by Markers
on May 1, 2019 at 1:01:12 pm

It's not entirely clear what you are trying to do,

You want a list of layers ordered by at what time in the composition their first marker that has a comment equal to "page" is ?

Whatever the case, you should not need the outer loop (k) :
Mind that everytime this loop runs "books" get reset to [] and the rest of the loop will just run 10 times without using the variable k anywhere ?

Maybe there are 10 books with 10 pages each, and you want book=[] outside of both loops and use the variable k somehwere ?

a bit more info or a screenshot of your compositiona nd I'm sure we can figure this out.



Return to posts index

Allan Ricarte
Re: Sort array/list by Markers
on May 5, 2019 at 4:43:39 pm

So, basically what i'm trying to do is to use the markers as reference to the Z position. Like a page on top of each other but using markers as a guide for it.

For loop is checking which markers is coming first, and then will give it (1 * zPosition) to that exactly layer, afterwards the loop will check which markers is coming after the first one, then (2 * zPosition), and so on.

Another solution that i thought it was check all markers that crossed the timeline and then break it when it cross itself, and holding that value.
I've got everything done (code below), but i can't hold the value, it's still counts the other markers afterwards.

I've tried everything and i know I'm missing something, but i couldn't figure it out, unfortunately.

Any idea will help, Thanks for the reply!

book = "";
myArray = [];

for (i = 1; i &lt;= 10; i++){
mk = thisLayer.marker
m = thisComp.layer("Page "+i).marker

n = 0;
if (m.numKeys > 0){
n = m.nearestKey(time).index;
if (m.key(n).time > time) n--;}

k = 0;
if (mk.numKeys > 0){
k = mk.nearestKey(time).index;
if (mk.key(k).time > time) k--;}


if ( n > 0 && m.key(n).comment === "page"){
myArray.push(myArray.length);
}

if ( k > 0 && m.key(k).comment === "page"){
len = myArray.length;}

else{
len = 0;}
}

book = len


Return to posts index


Filip Vandueren
Re: Sort array/list by Markers
on May 6, 2019 at 7:48:57 am

If I understand correctly, you only want to list the markers that are before the current time. That part seems to check out.

although shouldn't this line:

if ( k > 0 && m.key(k).comment === "page"){

be:

if ( k > 0 && mk.key(k).comment === "page"){

and shouldn't that entire if/then/else statement be outside and after your for-loop ?

But what are you pushing into the array ? myArray.length ?
You want an array that reads [0,1,2,3]… ?
But you only use it's length…

You check for markers on 10 different layers (one for each page)
but what is thisLayer ? It also has markers ? Is it just a null ? What are you applying this expression to ?

is it on every page ?

I'm pretty sure if you just tell us what you want to calculate it will be much easier:
e.g. "I want a number that tells me how many markers have been passed on layers (1-10)z tells me more than "I want to control the Z-depth of pages"



Return to posts index

Allan Ricarte
Re: Sort array/list by Markers
on May 6, 2019 at 3:42:21 pm

yeah, it seems a bit confusing to understand what i'm trying to do, I'll try my best to explain it as simple as possible

I'm using a "txt" as list array for it, It just a txt + 10 layers named "page 1" to "page 10".

So, what i want to calculate is how many markers have been cross between the pages (1 to 10) until it cross itself , and break it with that value. This is an example video.







Why do i need this number?

it's to match that number with the "txt" array to calculate to z-depth. (Piece of code below)

This is a final example project, what i'm trying to reach. Just to have a overall view about it.







Thanks for the answer!

list = thisComp.layer("txt").text.sourceText.split(",");
match = "match the markers what i need here";
for (i = 0; i<list.length; i++) {
if ( match == list[i] ){
arr = i;
break;
} else { arr = match}
}


Return to posts index

Filip Vandueren
Re: Sort array/list by Markers
on May 6, 2019 at 7:39:57 pm

Thanks for clarifying ☺

For now I'll assume that you will only do 1 page_in and one page_out over the course of the whole animation ?
If so:
the expression for the sourceText of your txt-layer and the expression for each of the book-layers positions are below.

Mind the &lt; and &gt; characters which should be < and >

/* for SOURCETXT: */
books=[];

// list the books and their in-time
for (i=1; i&lt;=10; i++) {
bookName = ";Book "+i;
l=thisComp.layer(bookName);
if (l.marker.key("page_in").time &lt;=time && l.marker.key("page_out").time&gt;=time) {
books.push([bookName, l.marker.key("page_in").time]);
}
}
// we"ll get a list like [["Book 10", 1.44], ["Book 5", 2.24], ["Book 7", 3.32]];
orderedBooks = books.sort(function(a,b){ return a[1]-b[1]; });
// the books are now ordered by their in-markertime
orderedBooks.toSource();


/* FOR POSITION : */
// import the array from the txtlayer
books = eval(thisComp.layer("txt").text.sourceText.value);

// loop through the array and see if this layer is in it.
// a sort of custom indexOf() function
var z=-1, l=books.length;
while (++z&lt;l && books[z][0]!==thisLayer.name);
z = z&lt;l ? z : -1;

if (z&gt;-1) {
// the position if the book is "IN"
[1400,600,0] + [20,-20,-10]*z;
} else {
// the position if the book is "OUT"
value;
}




Return to posts index


Filip Vandueren
Re: Sort array/list by Markers
on May 6, 2019 at 7:50:57 pm

If there are multiple ins and outs, we can't just reference the markers by their name, but have to find nearestKey and all that Jazz you did.
Here's my implementation (sourceText expression only)

books=[];

// list the books and their in-time
for (i=1; i<=10; i++) {
bookName = "Book "+i;
l=thisComp.layer(bookName);

nk=l.marker.nearestKey(time);
nki = nk.time>time ? nk.index-1 : nk.index;
if (nki>0 && l.marker.key(nki).comment==='page_in') {
books.push([bookName, l.marker.key(nki).time]);
}
}
// we'll get a list like [["Book 10", 1.44], ["Book 5", 2.24], ["Book 7", 3.32]];
orderedBooks = books.sort(function(a,b){ return a[1]-b[1]; });
// the books are now ordered by their in-markertime
orderedBooks.toSource();




Return to posts index

Allan Ricarte
Re: Sort array/list by Markers
on May 7, 2019 at 4:27:47 am

Wow, i really don't know what to say, Thank you so much for the help! That's exactly what i was looking for. I was literally trying to put this idea to work for months (yeah, months). Hope this general idea can help everyone that is looking for something similar to that. (Looping, Markers and arrays). Once again Filip, thank you!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]