Can you create a variable in Simple Expression?

Get answers to your questions about color grading, editing and finishing with DaVinci Resolve.
  • Author
  • Message
Offline

purplemagic

  • Posts: 58
  • Joined: Tue Jan 05, 2021 9:23 pm
  • Real Name: Justin Lewis

Can you create a variable in Simple Expression?

PostThu Jan 28, 2021 6:56 pm

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.
Offline
User avatar

bobosola

  • Posts: 166
  • Joined: Wed Nov 28, 2018 10:09 pm
  • Location: Southampton UK
  • Real Name: Bob Osola

Re: Can you create a variable in Simple Expression?

PostThu Jan 28, 2021 9:52 pm

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?
Offline

purplemagic

  • Posts: 58
  • Joined: Tue Jan 05, 2021 9:23 pm
  • Real Name: Justin Lewis

Re: Can you create a variable in Simple Expression?

PostFri Jan 29, 2021 5:27 am

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?
Offline
User avatar

bobosola

  • Posts: 166
  • Joined: Wed Nov 28, 2018 10:09 pm
  • Location: Southampton UK
  • Real Name: Bob Osola

Re: Can you create a variable in Simple Expression?

PostFri Jan 29, 2021 4:54 pm

Sorry, that’s beyond what little I know about scripting. You might get better luck asking the question in the Fusion forum.
Offline

Okke Verbart

  • Posts: 293
  • Joined: Tue Jan 17, 2017 8:40 pm

Re: Can you create a variable in Simple Expression?

PostSat Jan 30, 2021 8:53 pm

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;
www.ablackbirdcalledsue.com

Return to DaVinci Resolve

Who is online

Users browsing this forum: Bing [Bot], drake5000, DronePilot1977, jaanluis, Major Editing, Vit Reiter and 332 guests