ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Is It possible to sequentially step through a set of values each time a condition is met?

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Paul Beckett
Is It possible to sequentially step through a set of values each time a condition is met?
on Jun 27, 2020 at 7:43:12 pm

Hi there Expressionists!

A question....

Is It possible to sequentially step through a predefined set of values each time a condition is met?

For example, if I had four stored values, say, 90, 180, 270, 360, and each time an input value was met, say anything over 50, it would step to the next value, one after the other, then loop back to the start value.

There is no specific reason for these example values and the expression could pull them form a variable or an array or the expression could do some math to get the result, at this point I'm just trying to figure it out.

My first thought was maybe using the "for loop" was the direction to head in but after searching I can't find anything which would really show how this would function as an example.

Am I going in the right direction by exploring the "for loop" to achieve this or is there a better / simpler way? Also would anyone be able to point me in the direction of example code / reference material which might help solve the problem?

Many thanks!

P.


Return to posts index

Dan Ebberts
Re: Is It possible to sequentially step through a set of values each time a condition is met?
on Jun 27, 2020 at 8:25:22 pm

The trick is that your code has to count how many events have occurred prior to the current time. If you're monitoring a value, you can use the brute-force technique described here:

https://motionscript.com/design-guide/audio-count.html

If there are keyframes or markers associated with the events it becomes much simpler.

Dan



Return to posts index

Paul Beckett
Re: Is It possible to sequentially step through a set of values each time a condition is met?
on Jul 6, 2020 at 5:33:08 am

Hi Dan,

Sorry for slow reply and thank you for the pointers.....

So over the last week I have been playing around with the expression from your site which you recommended, plus your "Triggering an Expression With Audio" expression. Both of which I was able to get some interesting results from.

However as per your warnings the expression would start to take a long time to update when scrubbing through the timeline, with the update time becoming quite an issue if the comp was plus two minutes or more. Although this didn't seem to be an issue during ram preview.

I was able to workaround this by wrapping the main expression in an if statement linked to a checkbox control, then I could switch off the expression if not using it at the time. This led mo to think that there was probably a way to automate the switching on or off of the expression - although that wouldn't resolve the issue of slow update times when the expression was switched on.

In turn this led me to wonder if there was a way to limit how far back the expression looked when making the calculations - maybe using the in and out points of the Work Area, a layers in and out points, or markers, or even hard code it into the expression so it would only look at the previous sixty frames for example. I imagine though that this isn't doable as I would have found some examples or tutorials on the subject.

My next step is to see if I can do something using Scripting or bake the expression to key frames, although I'm not sure if this would work or be any quicker because my initial experiments so far have also suffered from the issue of it taking a long time to write the keyframes or update.

Anyway, onwards and upwards.... and thanks for the advice so far.

P.


Return to posts index


Filip Vandueren
Re: Is It possible to sequentially step through a set of values each time a condition is met?
on Jul 6, 2020 at 7:30:55 am

If you know that the conditions for re-calculating aren't going to change, and you're working on other things, but the expression slows you down, then you can also convert the expression to keyframes. You'll only have to wait once for the whole timeline to be calculated, and from then on it works the same.

Setting a limit to how far back the expression has to look works great for reducing the time, but is not a viable strategy in your case because you specifically need to count every occurrence.
Only counting occurrences in the last n seconds would cause the counter to stagnate and even count backwards



Return to posts index

Paul Beckett
Re: Is It possible to sequentially step through a set of values each time a condition is met?
on Jul 6, 2020 at 8:25:00 pm

Hi Filip,

Thank you for your comments.

Yeah I see how limiting the expression to count beats could cause more issues. I've played around with baking the expression to keyframes and while it does work, it takes a long time to write the keyframes, which would be fine, if, as you you mention I needed to do it only once, but I do need to make small adjustments occasionally and currently the amount of time it takes to re-bake the expression after a change is a problem (the comp is about seven minutes long).

That said I was also experimenting with Dan's other expression which triggers at a certain threshold which gave me some interesting results but still I have the issue of it being slow to calculate when scrubbing through the timeline. It was with this expression I was wondering if limiting how far back it looked would work. Dan describes the expression as looking backwards in time frame by frame until a threshold transition is found - if that is the case, could it be possible to limit the expression to say twice the length of time between the current time and time of the previous transition? Or would this cause the same problems of stagnation which you described?

At the moment I'm just trying to understand the limitations of the these expressions or expression built on them before go too far in one direction.

Thanks again!

P.


Return to posts index

Filip Vandueren
Re: Is It possible to sequentially step through a set of values each time a condition is met?
on Jul 7, 2020 at 9:20:28 am

You' ll find that running a script to create keyframes for you is a lot faster than running these kinds of expressions over such a long time.

I don't have the complete context on what your "testing condition" is, so going back to Dan's original example of analyzing Audio Amplitude:

In a comp that already has an Audio Amplitude analysis layer,
I've added a Null ("counter") with a Slider named ("hit counter")

I then run this script:

// extendscript code, not an expression!
thisComp = app.project.activeItem;

threshold = 50.0;
audioLev = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");

// property that will get numbered keyframes
p = thisComp.layer("counter").effect("hit counter")("Slider");

// remove old keyframes of prop
if (p.numKeys>0) {
for (i=p.numKeys; i>0; i--) {
p.removeKey(i);
}
}


above = false;
n = 0;
p.setValueAtTime(0,n);
for (t=0; t<=thisComp.duration; t+=thisComp.frameDuration) {
if (above){
if (audioLev.valueAtTime(t,false) < threshold){
above = false;
}
}else if (audioLev.valueAtTime(t,false) >= threshold){
above = true;
n++;
p.setValueAtTime(t,n);
p.setInterpolationTypeAtKey(p.numKeys, KeyframeInterpolationType.HOLD, KeyframeInterpolationType.HOLD);

}
}

It creates Hold-keyframes on that slider every time the condition is met.
Other expressions can now just reference this Slider's value to get an instant number

For example in the property where you wanted to step through the possibilities,
this expression does the trick:

possibilities=[90, 180, 270, 360];
c=thisComp.layer("counter").effect("hit counter")("Slider").value;

c %= possibilities.length;
possibilities[c];


If your conditions change, instead of rebaking the expression, re-run the script.
In my test running the above Script on a 15-minute composition took a few seconds.



Return to posts index


Paul Beckett
Re: Is It possible to sequentially step through a set of values each time a condition is met?
on Jul 7, 2020 at 7:24:27 pm

Hi Filip,

Thank you for the help - Hopefully I'll get the chance to play with your script this week and see what results I can get, and report back.

Thanks again!

P.


Return to posts index

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