Jump to: Board index » General » Fusion

Adding a Number as string into Text3D-node via Consol

Learn about 3D compositing, animation, broadcast design and VFX workflows.
  • Author
  • Message
Offline

Thomas Mueller

  • Posts: 6
  • Joined: Sat May 28, 2022 3:54 pm
  • Real Name: Thomas Mueller

Adding a Number as string into Text3D-node via Consol

PostSat May 28, 2022 4:16 pm

Hello,

I have some trouble using the Text3D-node in combination with an expression and the consol:

I'm trying to show a special character (for the sake of this post it's a €) and link it to a number (e.g., Pi) up to a certain decimal place, (e.g., 4) in a Text3D-node. The certain number of decimal place is importent, since I don't want a value like precisly 3 to be shown as 3 but as 3.000.

If I put the

Text([[€]]..string.format("%.4f",3.1415926))

into an expression of a Text3D-node this works fine. As I need this very often, I would like to implement this in a script with the Text3D.StyledText:SetExpression("xyz") which I tried with

Code: Select all
comp.Text3D1.StyledText:SetExpression("Text([[€]]..string.format("%.4f",3.1415926))")

This leads to the error in the consol

[string "???"]:1: malformed number near '.4f'

I tried several things with the synthax of Text("xyz") as well trying to understand why string.format seems not to work without success. Do you have any suggestion what the problem here is or how to archive the discribed goal?

Looking forward to hear your suggestions,
Thank you,
Thomas
000085.png
000085.png (47.62 KiB) Viewed 553 times

000086.png
000086.png (17.4 KiB) Viewed 553 times
Offline
User avatar

Bryan Ray

  • Posts: 2491
  • Joined: Mon Nov 28, 2016 5:32 am
  • Location: Los Angeles, CA, USA

Re: Adding a Number as string into Text3D-node via Consol

PostMon May 30, 2022 4:37 pm

Thomas Mueller wrote:
Code: Select all
comp.Text3D1.StyledText:SetExpression("Text([[€]]..string.format("%.4f",3.1415926))")



The problem is the quotation marks. If you have quote marks inside quote marks, you need a way to distinguish between marks that are inside and outside the string. It looks like you already addressed that issue once, with the string literal surrounding your €. You just needed to do it again with the "%.4f" string.

There are two ways of doing it: Either you use different types of string enclosures, or you escape the marks you want to treat differently.

Here's an example of the former:

Code: Select all
comp.Text3D1.StyledText:SetExpression("Text([[€]]..string.format('%.4f',3.1415926))")


Using single quotes for the interior string prevents it from prematurely ending the exterior string.

Or, as I alluded above, you could use the same string literal notation:

Code: Select all
comp.Text3D1.StyledText:SetExpression("Text([[€]]..string.format([[%.4f]],3.1415926))")


I usually use the literal on the outside with normal quote marks in the interior, but either way works.


The other method is to use the escape character \ to instruct the script interpreter to ignore the quotation marks:

Code: Select all
comp.Text3D1.StyledText:SetExpression("Text([[€]]..string.format(\"%.4f\",3.1415926))")


When the interpreter encounters the \", it replaces it with ", but otherwise ignores it for the purpose of parsing the code.

See the Strings chapter of Programming in Lua for more information:
https://www.lua.org/pil/2.4.html
Bryan Ray
http://www.bryanray.name
http://www.sidefx.com
Offline

Thomas Mueller

  • Posts: 6
  • Joined: Sat May 28, 2022 3:54 pm
  • Real Name: Thomas Mueller

Re: Adding a Number as string into Text3D-node via Consol

PostTue May 31, 2022 5:27 am

Thank you very much Bryan for your comprehensive answer! This helps a lot.

Return to Fusion

Who is online

Users browsing this forum: No registered users and 52 guests