ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Trigger at marker

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Darren Lee
Trigger at marker
on Jun 11, 2019 at 3:51:30 pm

Is there a method for triggering a function call once when hitting a marker?

I found Dan Ebberts's script: https://www.motionscript.com/design-guide/marker-sync.html

But it seems to call multiple times after or before a marker
I want something like:

function markerTriggered () {
//Trigger once
}

if (nearestMarker) {
markerTriggered();
}


Return to posts index

Kalleheikki Kannisto
Re: Trigger at marker
on Jun 12, 2019 at 6:06:53 am

You want to include the marker's time there and compare to current time. While you should be able to do it by checking if marker time equals current time and running the function if so, might be more foolproof to check whether current time is equal or more than marker time and current time minus one frame is less than, and if both conditions are true then run function.

Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index

Darren Lee
Re: Trigger at marker
on Jun 12, 2019 at 8:49:41 am

Thanks for you reply, I should probably give more detail on what I'm trying to achieve

I'm using the script from here https://forums.creativecow.net/docs/forums/post.php?forumid=227&postid=3718... to shuffle an array of numbers.

I have the below on a text Source layer, but I want it to shuffle every time it hot a marker, and stay shuffled

I can achieve this with something like:

if (time == 1) {
shuffle(a);
"["+a.toString()+"]";
}

But then the array is reset immediately afterwards because it's being set every frame

function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;


}
return array;
}

var a = new Array(20, 30, 40);

shuffle(a);
"["+a.toString()+"]";


Return to posts index


Kalleheikki Kannisto
Re: Trigger at marker
on Jun 12, 2019 at 10:03:02 am

Ok, in that case you want to count the number of markers that you have passed and use that number (marker index) for randomseed. Expressions don't have a memory of what has happened in earlier frames, so you have to calculate the shuffle for every frame. The same randomseed will ensure the random calculation is the same random calculation. So you would change that at every marker.
A bit of an inconvenience is that the nearest marker can be before or after current time, so you have to check whether the marker time is after current frame, and if so, subtract one to get the index of the last marker passed.

Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index

Darren Lee
Re: Trigger at marker
on Jun 13, 2019 at 10:15:19 am

Thanks for this, what that might look like in the code? Something like this?:

randomIndex = Math.floor(randomSeed() * currentIndex);


Return to posts index

Kalleheikki Kannisto
Re: Trigger at marker
on Jun 13, 2019 at 2:19:03 pm

Something like this

nMarker = thisComp.marker.nearestKey(time);
mTime = nMarker.time;
mIndex = nMarker.index;
if (mTime>time){
mIndex--
}
seedRandom(mIndex,true);


Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index


Darren Lee
Re: Trigger at marker
on Jun 13, 2019 at 10:26:27 pm

Works brilliant, thanks!


Return to posts index

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