Welcome to WSL!

New to the forum? Please read this and this.

The S-Files Episode 1: if - then - else expressions in Fusion.

User avatar
SecondMan
Site Admin
Posts: 4900
Joined: Thu Jul 31, 2014 5:31 pm
Answers: 31
Location: Vancouver, Canada
Been thanked: 52 times
Contact:

The S-Files Episode 1: if - then - else expressions in Fusion.

#1

Post by SecondMan »

Hi S,


Presently I'm building a macro with quite a lot of expressions in it. They're not that hard, but they are many. As with many things it started as something really simple and it just kept growing.

I have expressions in there that reference other expressions that reference values of the same tool at different times. That and a ton of if-this-then-thats.

All those different expressions all render for every frame, even when I don't strictly need them. Currently I am at about 3fps for calculating this tool (just resulting in one number), while being about halfway where I want to be.

I just would like to understand how Fusion calculates its expressions to see if I can optimize this further.

P.


Hi P,


An expression is evaluated just like there was an = at the start, something like:

Code: Select all

result = expression

That’s the whole reason why iif() exists, so that you can do:

Code: Select all

result = iif(condition, expression1, expression2)

You can’t do:

Code: Select all

result = if condition then expression1 else expression2 end

because the expressions don’t return values back through the if. They would be
evaluated as if they were just stand-alone expressions without the implied = in front
of them.

With the iif() function (or any function), each argument is evaluated, and those results
are passed to the function. Inside the iif() function, it doesn’t see the original expressions.
It only sees the value that each one produced. Then iif() returns the appropriate one
based on the value it was given for the already evaluated condition expression.

A common method in lua to do the same thing kind of thing is using:

Code: Select all

(condition) and (expression1) or (expression2)

It does have the advantage that if condition is false, then expression1 is not
evaluated. But expression2 will always be evaluated.

It is also possible to prefix an expression with a : to prevent the
implicit return happening, then you can do full on script statements (rather
than just an expression):

Code: Select all

:if a == 2 then return 3; else return 4; end

or even something like

Code: Select all

x = <condition> and (print(“first”) or true) or (print(“second”) or true)

S.

Tags:

User avatar
SirEdric
Fusionator
Posts: 2384
Joined: Tue Aug 05, 2014 10:04 am
Answers: 12
Real name: Eric Westphal
Been thanked: 4 times
Contact:

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#2

Post by SirEdric »

Very nicely put!
"The S-Files....the truth is somewhere out there"....:-)

User avatar
SecondMan
Site Admin
Posts: 4900
Joined: Thu Jul 31, 2014 5:31 pm
Answers: 31
Location: Vancouver, Canada
Been thanked: 52 times
Contact:

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#3

Post by SecondMan »

:)

Do not underestimate the power of this method:

Code: Select all

(condition) and (expression1) or (expression2)

As an example, this is the expression from the Switch node in KAK that controls what part of the keying process is displayed in the viewer:

Code: Select all

KAK_1.FGBGCombineOutput == 2 and 1 or KAK_1.FGBGCombineOutput == 1 and 2 or
KAK_1.SpillAndNoiseOutput == 5 and 3 or KAK_1.SpillAndNoiseOutput == 4 and 4 or KAK_1.SpillAndNoiseOutput == 3 and 5 or KAK_1.SpillAndNoiseOutput == 2 and 6 or KAK_1.SpillAndNoiseOutput == 1 and 7 or
KAK_1.KeyerOutput == 5 and 8 or KAK_1.KeyerOutput == 4 and 9 or KAK_1.KeyerOutput == 3 and 10 or KAK_1.KeyerOutput == 2 and 11 or KAK_1.KeyerOutput == 1 and 12 or
KAK_1.CleanPlateOutput == 2 and 13 or KAK_1.CleanPlateOutput == 1 and 14 or
KAK_1.Output == 5 and 15 or KAK_1.Output == 4 and 16 or KAK_1.Output == 3 and 17 or KAK_1.Output == 2 and 18 or KAK_1.Output == 1 and 19 or 20

If that were wrapped in the iif() function instead, KAK would have been utterly unusable.

User avatar
SirEdric
Fusionator
Posts: 2384
Joined: Tue Aug 05, 2014 10:04 am
Answers: 12
Real name: Eric Westphal
Been thanked: 4 times
Contact:

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#4

Post by SirEdric »

Man!...:-)
I did not underestimate a single thing, and I totally appreciate the stuff you write!

But "The S-Files" reminded me of "The X-Files", which is why I put that quote on there.
(You know....exploring the dark corners of scriptology, the lost snippets of the JesusCode, etc...:-)

Cheers.

Eric.

User avatar
SirEdric
Fusionator
Posts: 2384
Joined: Tue Aug 05, 2014 10:04 am
Answers: 12
Real name: Eric Westphal
Been thanked: 4 times
Contact:

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#5

Post by SirEdric »

Actually this brings up a question.

So in a CustomVertex I have this expression in VertexColor which works as expected:

Code: Select all

if(pz<n8+n7 & pz>n8-n7, vcr*n1, if(pz<n8+n7*2 & pz>n8-n7*2, vcr*n2, vcr))
but if I try to translate it to your example it does not work anymore:

Code: Select all

(pz<n8+n7 & pz>n8-n7)  and  (vcr*n1) or (pz<n8+n7*2 & pz>n8-n7*2) and (vcr*n2) or vcr
Am I lost in translation (again...:-))?

Cheers.

Eric.

User avatar
SecondMan
Site Admin
Posts: 4900
Joined: Thu Jul 31, 2014 5:31 pm
Answers: 31
Location: Vancouver, Canada
Been thanked: 52 times
Contact:

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#6

Post by SecondMan »

SirEdric wrote: Thu Jan 25, 2018 9:15 pm Man!...:-)
I did not underestimate a single thing, and I totally appreciate the stuff you write!

But "The S-Files" reminded me of "The X-Files", which is why I put that quote on there.
(You know....exploring the dark corners of scriptology, the lost snippets of the JesusCode, etc...:-)

Cheers.

Eric.

Haha - no that's not what I meant, Eric. The smiley was a reaction to your quote, the example was just a continuation of my post :D

Oh and maybe I didn't write it at all. Maybe this is all a little mystery...

User avatar
SecondMan
Site Admin
Posts: 4900
Joined: Thu Jul 31, 2014 5:31 pm
Answers: 31
Location: Vancouver, Canada
Been thanked: 52 times
Contact:

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#7

Post by SecondMan »

SirEdric wrote: Thu Jan 25, 2018 9:59 pm Actually this brings up a question.

So in a CustomVertex I have this expression in VertexColor which works as expected:

Code: Select all

if(pz<n8+n7 & pz>n8-n7, vcr*n1, if(pz<n8+n7*2 & pz>n8-n7*2, vcr*n2, vcr))
but if I try to translate it to your example it does not work anymore:

Code: Select all

(pz<n8+n7 & pz>n8-n7)  and  (vcr*n1) or (pz<n8+n7*2 & pz>n8-n7*2) and (vcr*n2) or vcr
Am I lost in translation (again...:-))?

Cheers.

Eric.

Ah, no, inside of a CustomVertex this won't work. This applies to Scripting and SimpleExpressions in tools. Inside a CustomVertex scripting is lesser in scope and functions. Note that iif() isn't available in there either.

User avatar
SecondMan
Site Admin
Posts: 4900
Joined: Thu Jul 31, 2014 5:31 pm
Answers: 31
Location: Vancouver, Canada
Been thanked: 52 times
Contact:

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#8

Post by SecondMan »

After reading this post I thought I'd post a really simple example of the (condition) and (expression1) or (expression2) method:

Code: Select all

{
	Tools = ordered() {
		Blur1 = Blur {
			CtrlWZoom = false,
			Inputs = {
				XBlurSize = Input { Value = 0, },
				BlurLargerThan50 = Input { Expression = "(XBlurSize>50) and 0 or 1", },
			},
			ViewInfo = OperatorInfo { Pos = { 715, 148.5 } },
			UserControls = ordered() {
				BlurLargerThan50 = {
					LINKS_Name = "Blur Larger Than 50",
					LINKID_DataType = "Number",
					INPID_InputControl = "MultiButtonControl",
					INP_Integer = false,
					{ MBTNC_AddButton = "YES" },
					{ MBTNC_AddButton = "NO" },
					MBTNC_ShowBasicButton = true,
					MBTNC_ShowToolTip = false,
					MBTNC_ShowName = true,
					MBTNC_StretchToFit = true,
					ICS_ControlPage = "Controls"
				}
			}
		}
	}
}

Drag the Blur Size slider and see what happens to the button down below.

icierva
Fusioneer
Posts: 153
Joined: Sat Apr 08, 2017 3:46 am

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#9

Post by icierva »

Deleted, wrong thread.

Ban me, please.

User avatar
SecondMan
Site Admin
Posts: 4900
Joined: Thu Jul 31, 2014 5:31 pm
Answers: 31
Location: Vancouver, Canada
Been thanked: 52 times
Contact:

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#10

Post by SecondMan »

icierva wrote: Tue Jan 30, 2018 2:43 pm Deleted, wrong thread.

Ban me, please.

Never :D

User avatar
TekTurek
Posts: 6
Joined: Mon Apr 19, 2021 12:27 am
Real name: Bartlomiej Turek

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#11

Post by TekTurek »

Hi, sorry for digging up three-years old thread, but i got a elseif question, and I don't think it's necessary to create new topic as my question is pretty basic.

I created two expressions build with iff, when I simplify them they look something like that:

iif(time1<20, A, X)
iif(time2>40, B, X)


So I need the same X value between the two time markers but two separate expressions before 20frame, and after 40frame.

How can I put this stuff together in one expression, so i get three value states, depending on time? The value i need will be calculated then like
A -> 20frame -> X -> 40frame -> B

User avatar
Midgardsormr
Fusionator
Posts: 2169
Joined: Wed Nov 26, 2014 8:04 pm
Answers: 28
Location: Los Angeles, CA, USA
Been thanked: 40 times
Contact:

Re: The S-Files Episode 1: if - then - else expressions in Fusion.

#12

Post by Midgardsormr »

The statements can be nested:

iif(time1<20, A, iif(time>40, B, X))