Page 1 of 1

Expression for count up with a set stop number

PostPosted: Thu Jun 24, 2021 9:38 am
by dilby00
Hi - I'm hoping someone could help with the expression for a count-up timer that stops at a given number? For example I want it to start at 0 and end at 88, but currently it keeps going. Only examples I can find are count down which gives a starting point which isn't any help. Many thanks!

Re: Expression for count up with a set stop number

PostPosted: Thu Jun 24, 2021 1:59 pm
by Bryan Ray
iif(time>=88, 88, time)

That will count frames up to frame 88, then stop. To break it down, the iif() function takes three arguments: A condition, a value to return if the condition is true, and a condition to return if it's false.

If time is greater than or equal to 88 then
return 88
else
return time

You can, of course, make the expression more complex to handle different counting tasks, and the return values can be expressions, too, even containing more iif() statements inside.

Re: Expression for count up with a set stop number

PostPosted: Fri Jul 02, 2021 8:53 pm
by banana
Well, works pretty nice. Thank you, I asked myself the same question a while ago, but I haven't needed a solution so far.

I have a continuative question:
iif(time>=88, 88, time) counts to 88 and stops, right? How can I make it go on counting at a specific time, e.g. 100.
So in other words: count to 88, stop at 88 till time reaches 100, counting 89 and onwards at time=>100

I've tried figuring it out myself. Without success so far.
Thanks & Cheers
Klaus

Re: Expression for count up with a set stop number

PostPosted: Fri Jul 02, 2021 11:32 pm
by Bryan Ray
You can nest iif statments inside one another:

iif(time >= 88, iif(time >= 100, time-11, 88), time)

Code: Select all
If time is greater than or equal to 88 then
    if time is greater than or equal to 100 then
        return time minus 11 (to resume counting from 88)
    else
        return 88 (time is greater than 88 but less than 100)
else
    return time (time is less than 88)


If you add some User Controls to the tool with the Edit Controls feature, you can insert variables. So if, for instance, you had a slider called Halt and one called Resume, you could write the expression like this:

iif(time >= Halt, iif(time >= Resume, time - (Resume - Halt - 1), Halt), time)

Re: Expression for count up with a set stop number

PostPosted: Sat Jul 03, 2021 7:05 am
by banana
Bryan Ray wrote:You can nest iif statments inside one another:

Oh my god, so good! :D

Yes, adding some User Controls is neraly always the way I'm going in these situations. I will propably connect the Resume-input to the end of the composition. So the counter resumes counting at a given time before the com ends. So everything gets nice and responsive.

Thank you again. Very helpful!
As I absolutely love (and, if things do not work, sometimes hate) to work with expressions, but I am not the biggest expressions/lua crack (unfortunatelly), I am very grateful to have this forum, this community and helpfulness of all the people supporting other people/me. ;)

Re: Expression for count up with a set stop number

PostPosted: Wed Sep 22, 2021 7:29 pm
by banana
Hey there,
I know it has been a while, but I stumpled across the project again, where I needed this nested iff statements. I tried to make the nested iff statements work in a more, lets say, complex scenario. And I failed :D It does not work and I don't know any other way to kindly ask for the community's help again.

So I carefully tried to refer to Bryans generalized nested iff statement:
Bryan Ray wrote:iif(time >= Halt, iif(time >= Resume, time - (Resume - Halt - 1), Halt), time)


And based on that I made the following:
iif(time>=floor(hold.NumberIn4/2),iff(time>=comp.RenderEnd-floor(hold.NumberIn4/2),time-comp.RenderEnd-floor(hold.NumberIn4/2)-1),floor(hold.NumberIn4/2),time)

As I mentioned it is a bit more complex, but not that bad after a few hints. Let me explain:

hold.NumberIn4 is a static positive number which is generated at the beginning of the animation by the parameters the user chooses. For simplicity we can say the Number is 10. It is held by a CustomTool called hold and sits in place NumberIn4.
That means: Halt = floor(hold.NumberIn4/2) in my statement.

As you can see, I tried to make the animation responsive to the comp length with comp.RenderEnd.
Resume = comp.RenderEnd-floor(hold.NumberIn4/2) in my statement.

My plan was that the animation runs until Halt is reached (e.g. at frame 10) and picks up again (with number 11) 10 frames before the end of the comp is reached. But unfortunatelly the expression does not work. I have tried to find some workarounds our mistakes in the expression for weeks now in my sparetime. Maybe just the strucure is faulty or the iff statement doesn't work with connections to a CustomTool, I don't know. Please, could anyone have a smart eye on my statement? Would be so helpful. And I would be so thankful!
Thank You
Klaus

Re: Expression for count up with a set stop number

PostPosted: Wed Sep 22, 2021 7:43 pm
by TheBloke
The second 'iif' is
Code: Select all
iff
Instead of
Code: Select all
iif
Also I think you have a ) in the wrong place. Try this:
Code: Select all
iif(time>=floor(hold.NumberIn4/2),iif(time>=comp.RenderEnd-floor(hold.NumberIn4/2),time-comp.RenderEnd-floor(hold.NumberIn4/2)-1,floor(hold.NumberIn4/2)),time)
PS. I checked that for syntax, but not whether it was logically correct for what you're trying to do, so I can't guarantee it'll actually do what you want :)

Re: Expression for count up with a set stop number

PostPosted: Wed Sep 22, 2021 7:58 pm
by banana
TheBloke, you are a good man and I am a dork (for not seeing this).
It now works. Thanks a lot <3
There has been a mistake with negative numbers when counting on again, I noticed, so I fixed it quick. The correct syntax for anyone who is interested would be:
iif(time>=floor(hold.NumberIn4/2),iif(time>=comp.RenderEnd-floor(hold.NumberIn4/2),time-comp.RenderEnd+floor(hold.NumberIn4),floor(hold.NumberIn4/2)),time)

Let my kindly ask you if you use some kind of editor/IDE for comfortable writing lua expressions/for Resolve? It seems that my workflow needs an update, not seeing these typo errors for weeks.
Thanks a lot, TheBloke, i owe you one. Cheers to you.
Klaus

Re: Expression for count up with a set stop number

PostPosted: Wed Sep 22, 2021 8:44 pm
by Sander de Regt
Notepad ++ is quite popular and can be set to Lua (the default internal scripting language of Fusion) which helps with indentation and color coding.

Re: Expression for count up with a set stop number

PostPosted: Wed Sep 22, 2021 9:14 pm
by TheBloke
I use Visual Studio Code but yeah the principle is the same. Something with Lua code formatting.

In this example it would look like this in a code editor:
Image

Editing expressions in Fusion is quite frustrating because of the tiny box with no code formatting. Other code boxes - like the Frame Render Script - are much larger, and do have some basic colour formatting.

As a result I've heard a couple of people recommend to not use expressions and instead use Frame Render Script. The syntax would be a bit different I think in terms of referencing controls, and it'd have the disadvantage of the computed values not showing up in the UI for the affected control(s), nor in the Spline Editor. But once the expression is set correctly that doesn't matter hugely, and the benefits in creating and editing an expression in a larger, code-coloured box might be worth it.

If that was done, it could look something like this:
Image

(I haven't tested if the expression works unaltered in a Frame Render Script, that's just an idea of how it'd look in the UI.)

Re: Expression for count up with a set stop number

PostPosted: Thu Sep 23, 2021 6:18 am
by Bryan Ray
I sometimes write an expression in a code editor with readable formatting, then collapse it to a single line to copy-paste into the expression field. I have a couple of tools that actually run while loops in an expression, and those are a bear to debug when they're just one line!