Page 1 of 1

Syntax of expressions

PostPosted: Wed Feb 06, 2019 12:46 am
by d3zd3z
I would like to animate the r, g, and b values of the color of a background node with an expression based on time.

I selected "modify with", and chose "expression", and got a large block under the Modifiers tab. I can put simple expressions in the "Number Out" field "sin(time*20)/2+.5", but I haven't been able to figure out what language these expressions are supposed to be in.

One part of the manual suggests I should be able to use "iff(expr, a, b)" for a conditional, another suggests "if(expr, a, b)", and my experience is that neither of these works. If anything is wrong, I just get 0 out of the expression, and a very uninteresting black.

The words "and" and "or" cause this error behavior, and I was able to get "&&" and "||" to work for and and or, but I wasn't able to get an if/else type conditional to work with this.

Is there somewhere I'm missing where these are documented.

What I'm trying to do is have a pulsing pattern during some frame ranges, and during others have it held to a constant. I'd rather do this with an expression so that the parameters can easily be changed (pulse rate, how long it is on for, etc).

Thanks,
David

Re: Syntax of expressions

PostPosted: Wed Feb 06, 2019 6:12 am
by Okke Verbart
Hi David - the syntax of the expressions is pretty much LUA based, but as you've noticed there are some weird things. Indeed, in most nodes "iif" works, whereas for others (like in the pCustom, in for instance the particles tab) "if" is required. Also, I noticed that in some places things like if(a=b,c,d) work, whereas you would expect this to error (i.e. you would expect "==").

In your case, if you put a modify with expression on say the red channel, doesn't the expression below work?

Code: Select all
if(time>10 && time<20,1,0)


For me this works, so the background turns red between frames 10 & 20.

Example composition:

Code: Select all
{
   Tools = ordered() {
      Background1 = Background {
         CtrlWZoom = false,
         Inputs = {
            Width = Input { Value = 1920, },
            Height = Input { Value = 1080, },
            ["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2" }, },
            TopLeftRed = Input {
               SourceOp = "Expression1",
               Source = "NumberResult",
            },
         },
         ViewInfo = OperatorInfo { Pos = { 165, 181.5 } },
      },
      Expression1 = Expression {
         CtrlWZoom = false,
         Inputs = {
            NumberExpression = Input { Value = "if(time>10 && time<20,1,0)", },
         },
      }
   },
   ActiveTool = "Background1"
}

Re: Syntax of expressions

PostPosted: Wed Feb 06, 2019 6:14 am
by Okke Verbart
or for some pulsing between frames 10 & 80:

Code: Select all
if(time>10 && time<80,sin(time*20),0)

Re: Syntax of expressions

PostPosted: Mon Aug 29, 2022 7:22 pm
by Cesar Tejada
Okke Verbart wrote:Hi David - the syntax of the expressions is pretty much LUA based, but as you've noticed there are some weird things. Indeed, in most nodes "iif" works, whereas for others (like in the pCustom, in for instance the particles tab) "if" is required. Also, I noticed that in some places things like if(a=b,c,d) work, whereas you would expect this to error (i.e. you would expect "==").


This kind of thing is very confusing for new users trying to learn expressions.

I was using an Expression Modifier using this Syntax: iif(n1==0,0,1) and I expend like an Hour trying to know why isn't working like the expression field for each control.

Re: Syntax of expressions

PostPosted: Mon Sep 19, 2022 4:50 am
by theyaoster
whereas for others (like in the pCustom, in for instance the particles tab) "if" is required

Wow thank you for noting this - I couldn't find this documented anywhere in the reference manual (though, the SimpleExpression documentation in the manual is barebones to the max). No wonder my pCustom expressions were doing nothing. :)

But I'm curious now, is this a bug, or is there some other known reason the syntax is different for certain nodes?

Re: Syntax of expressions

PostPosted: Mon Sep 19, 2022 8:27 am
by UserNoah
The reason why iif() exists is because if() doesn't return a value.

I wrote about this some time ago https://noahhaehnel.com/blog/fusion-sim ... iif_not_if

But I also had to learn it the hard way lol