Page 1 of 1

Can you create a variable in Simple Expression?

PostPosted: Thu Jan 28, 2021 6:56 pm
by purplemagic
I have a long line of Simple Expression code in a parameter that would be much shorter if I could store the interim result in a custom variable.

Can you create a variable in a parameter's Simple Expression? If not, can you create a variable in a node's Frame/Start Render Script field and then access it in its parameters' Simple Expression code?

Thanks in advance.

Re: Can you create a variable in Simple Expression?

PostPosted: Thu Jan 28, 2021 9:52 pm
by bobosola
Yes, but I'm not sure how far you can take it if things start getting complex. For example, a simple expression to display the current frame position in seconds for a Text+ control might look like this:
Code: Select all
os.date("%M:%S",time/24)
where the frames per second value is hard-coded to 24. (If you are not familiar with the other expressions, os.date() is a Lua method for formatting a time value and time is a built-in variable which returns the current frame number)

You can instead name a variable to hold the fps value, but there are some rules:
  • the variable name must be preceded by a colon where it is declared
  • the variable declaration must be separated from the remaining code by a semi-colon
  • you have to add a return statement to make the whole thing work
Same example using a variable:
Code: Select all
:fps=24; return os.date("%M:%S",time/fps)
This stuff is seriously badly documented - maybe others can improve or expand on this?

Re: Can you create a variable in Simple Expression?

PostPosted: Fri Jan 29, 2021 5:27 am
by purplemagic
Thank you. Worked like a charm.

Do you know by any chance if there's a way to create variables in Frame/Start Render Script and use them from parameters?

Re: Can you create a variable in Simple Expression?

PostPosted: Fri Jan 29, 2021 4:54 pm
by bobosola
Sorry, that’s beyond what little I know about scripting. You might get better luck asking the question in the Fusion forum.

Re: Can you create a variable in Simple Expression?

PostPosted: Sat Jan 30, 2021 8:53 pm
by Okke Verbart
bobosola wrote:Yes, but I'm not sure how far you can take it if things start getting complex. For example, a simple expression to display the current frame position in seconds for a Text+ control might look like this:
Code: Select all
os.date("%M:%S",time/24)
where the frames per second value is hard-coded to 24. (If you are not familiar with the other expressions, os.date() is a Lua method for formatting a time value and time is a built-in variable which returns the current frame number)

You can instead name a variable to hold the fps value, but there are some rules:
  • the variable name must be preceded by a colon where it is declared
  • the variable declaration must be separated from the remaining code by a semi-colon
  • you have to add a return statement to make the whole thing work
Same example using a variable:
Code: Select all
:fps=24; return os.date("%M:%S",time/fps)
This stuff is seriously badly documented - maybe others can improve or expand on this?


Just a bit of nit picking, about the colon: Reason really is that any multi-statement expression needs to be preceded with a colon, it's not so much about the variable declaration. So, for instance, the following works

Code: Select all
:a=1; b=2; return a+b;