Jump to: Board index » General » Fusion

Scripting a Text Source (number counter)

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

davidsikes

  • Posts: 30
  • Joined: Mon Nov 10, 2014 9:21 pm
  • Location: Kansas City, MO

Scripting a Text Source (number counter)

PostSat Apr 15, 2017 1:21 pm

Hey all. Okay, a common AE thing I do that I'd like to be able to reproduce in Fusion is scripting the source of text—specifically in this project's case, a number counter that goes from 0% to 53%. I'd also like to have a better idea of how to script source text from something else - another object's x position, or the current frame number, etc. Can anyone point me in the right direction? Thanks!
Offline
User avatar

Andrew Hazelden

  • Posts: 536
  • Joined: Sat Dec 06, 2014 12:10 pm
  • Location: West Dover, Nova Scotia, Canada

Re: Scripting a Text Source (number counter)

PostSat Apr 15, 2017 1:38 pm

If you right click on the Text+ node's "Styled Text" field and choose the "Expression" option in the contextual menu you will be able to feed in any variable you want to the Text node.

Text-Node-Expressions.png
Expressions on a Text+ Node
Text-Node-Expressions.png (48.29 KiB) Viewed 13702 times


The current frame number is defined using the expression:

Text(time)

expresssion-frame-number.png
Frame Number
expresssion-frame-number.png (14.48 KiB) Viewed 13604 times


Here is a copy/pastable example node where I am using the Fusion expression value of Text(time) to display the current frame number in the Text+ node:

Code: Select all
{
   Tools = ordered() {
      Text1 = TextPlus {
         CtrlWZoom = false,
         Inputs = {
            GlobalOut = Input { Value = 144, },
            Width = Input { Value = 3840, },
            Height = Input { Value = 2160, },
            ["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2" }, },
            Size = Input { Value = 0.5, },
            Font = Input { Value = "Open Sans", },
            StyledText = Input {
               Value = "91",
               Expression = "Text(time)",
            },
            Style = Input { Value = "Bold", },
            ManualFontKerningPlacement = Input {
               Value = StyledText {
                  Array = {
                  },
                  Value = ""
               },
            },
            ShadingGradient1 = Input {
               Value = Gradient {
                  Colors = {
                     [0] = { 0, 0, 0, 1 },
                     [1] = { 1, 1, 1, 1 }
                  }
               },
            },
         },
         ViewInfo = OperatorInfo { Pos = { 1104, 118 } },
      }
   },
   ActiveTool = "Text1"
}
Last edited by Andrew Hazelden on Wed Apr 19, 2017 5:09 pm, edited 2 times in total.
Mac Studio M2 Ultra / Threadripper 3990X | Fusion Studio 18.6.4 | Kartaverse 6
Offline
User avatar

Andrew Hazelden

  • Posts: 536
  • Joined: Sat Dec 06, 2014 12:10 pm
  • Location: West Dover, Nova Scotia, Canada

Re: Scripting a Text Source (number counter)

PostSat Apr 15, 2017 2:17 pm

For writing a frame range counter as a text expression you could use:

Text("Frame: " .. time .. " of " .. ((comp.RenderEnd-comp.RenderStart)) )

expression-frame-range.png
Frame Range
expression-frame-range.png (9.76 KiB) Viewed 13605 times


Code: Select all
{
   Tools = ordered() {
      Text2 = TextPlus {
         CtrlWZoom = false,
         Inputs = {
            GlobalOut = Input { Value = 144, },
            Width = Input { Value = 3840, },
            Height = Input { Value = 1920, },
            ["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2" }, },
            Font = Input { Value = "Open Sans", },
            StyledText = Input {
               Value = "Frame: 144 of 144",
               Expression = "Text(\"Frame: \" .. time .. \" of \" .. ((comp.RenderEnd-comp.RenderStart)) )",
            },
            Style = Input { Value = "Bold", },
            ManualFontKerningPlacement = Input {
               Value = StyledText {
                  Array = {
                  },
                  Value = ""
               },
            },
            ShadingGradient1 = Input {
               Value = Gradient {
                  Colors = {
                     [0] = { 0, 0, 0, 1 },
                     [1] = { 1, 1, 1, 1 }
                  }
               },
            },
         },
         ViewInfo = OperatorInfo { Pos = { 1265, 115.5 } },
      }
   },
   ActiveTool = "Text2"
}


For percentages you could use the expression:

Text("Progress: " .. math.ceil(((time)/comp.RenderEnd)*100) .. "%")

expression-percentage.png
Percentage
expression-percentage.png (9.61 KiB) Viewed 13605 times


Code: Select all
{
   Tools = ordered() {
      Text3 = TextPlus {
         CtrlWZoom = false,
         Inputs = {
            GlobalOut = Input { Value = 144, },
            Width = Input { Value = 3840, },
            Height = Input { Value = 1920, },
            ["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2" }, },
            Font = Input { Value = "Open Sans", },
            StyledText = Input { Expression = "Text(\"Progress: \" .. math.ceil(((time)/comp.RenderEnd)*100) .. \"%\")", },
            Style = Input { Value = "Bold", },
            ManualFontKerningPlacement = Input {
               Value = StyledText {
                  Array = {
                  },
                  Value = ""
               },
            },
            ShadingGradient1 = Input {
               Value = Gradient {
                  Colors = {
                     [0] = { 0, 0, 0, 1 },
                     [1] = { 1, 1, 1, 1 }
                  }
               },
            },
         },
         ViewInfo = OperatorInfo { Pos = { 1375, 115.5 } },
      }
   },
   ActiveTool = "Text3"
}


Note: For expressions that are mixing together math operations and text strings at the same time you have to separate the different elements with the Lua style .. command.

You can check out page 440-442 of the "Fusion 8 User Manual.pdf" that is in the Fusion 8 applications folder for more details on using simple expressions:

/Applications/Blackmagic Fusion 8/Fusion 8 User Manual.pdf (macOS)

C:\Program Files\Blackmagic Design\Fusion 8\Help\Fusion 8 User Manual.pdf (Windows)


The really really old Fusion VFXPedia resource also has a page on expressions that can be viewed here:
https://www.steakunderwater.com/VFXPedia/96.0.243.189/index4aa9.html?title=Simple_Expressions
Last edited by Andrew Hazelden on Wed Apr 19, 2017 5:06 pm, edited 1 time in total.
Mac Studio M2 Ultra / Threadripper 3990X | Fusion Studio 18.6.4 | Kartaverse 6
Offline

davidsikes

  • Posts: 30
  • Joined: Mon Nov 10, 2014 9:21 pm
  • Location: Kansas City, MO

Re: Scripting a Text Source (number counter)

PostSun Apr 16, 2017 2:23 pm

This is very helpful, thanks!
Offline
User avatar

Ilir Beqiri

  • Posts: 4
  • Joined: Wed Apr 19, 2017 2:46 pm
  • Location: Tirana, Albania

Re: Scripting a Text Source (number counter)

PostWed Apr 19, 2017 4:20 pm

Very informative indeed. Do you have any idea on bow to add a new line on the styled text window via expressions? For example, I have a Text+ node and I need to write the resolution of the node on one line and the frames number below it, on a separate line. Is there any special type of syntax in Lua for that?
Offline
User avatar

Ilir Beqiri

  • Posts: 4
  • Joined: Wed Apr 19, 2017 2:46 pm
  • Location: Tirana, Albania

Re: Scripting a Text Source (number counter)

PostWed Apr 19, 2017 4:55 pm

Very lazy of me not to read through the manual section dedicated to easy expressions on the Fusion 8 User Manual. For those coming later to the thread, to add text on a new line, simply add "/n" before it. Anything after the /n symbol will be shifted to a new line. Thank you Andrew Hazelden, for the hint!
Offline
User avatar

Andrew Hazelden

  • Posts: 536
  • Joined: Sat Dec 06, 2014 12:10 pm
  • Location: West Dover, Nova Scotia, Canada

Re: Scripting a Text Source (number counter)

PostWed Apr 19, 2017 4:59 pm

Ilir Beqiri wrote:Do you have any idea on bow to add a new line on the styled text window via expressions? For example, I have a Text+ node and I need to write the resolution of the node on one line and the frames number below it, on a separate line.


TextPlus onscreen expressions.png
Expressions on a TextPlus Node Demo
TextPlus onscreen expressions.png (36.75 KiB) Viewed 13605 times


Here is a snippet of text you can paste into the Text+ node's "Styled Text" expression field that will show how LUA commands can be used:

Code: Select all
Text("[Frame #] " .. (time) .. "\n" .. "[Resolution] " .. Text4.Width ..  "x" .. Text4.Height .. "px" .. "\n" .. "[Font Size] " .. Text4.Size .. "\n" .. "[Foreground Color RGBA] " .. tonumber(string.format("%.3f", Text4.Red1)) .. "/" .. tonumber(string.format("%.3f", Text4.Green1)) .. "/".. tonumber(string.format("%.3f", Text4.Blue1)) .. "/".. tonumber(string.format("%.3f", Text4.Alpha1)) .. "\n" .. "[Background Color RGBA] " .. tonumber(string.format("%.3f", Text4.Red)) .. "/" .. tonumber(string.format("%.3f", Text4.Green)) .. "/".. tonumber(string.format("%.3f", Text4.Blue)) .. "/".. tonumber(string.format("%.3f", Text4.Alpha)) .. "\n")


The complex expression will create a formatted output like this:
[Frame #] 0
[Resolution] 3840x1920px
[Font Size] 0.08
[Foreground Color RGBA] 1/1/1/1
[Background Color RGBA] 0/0/0/0


Since the Foreground/Background color values are stored in 0-1 number ranges I am using the string.format() command to round the color value numbers to three digits of floating point precision so it doesn't take up too much space on screen.

This is the complete node block you can paste into a Fusion comp to see the premade example:
Code: Select all
{
   Tools = ordered() {
      Text4 = TextPlus {
         CtrlWZoom = false,
         NameSet = true,
         Inputs = {
            Width = Input { Value = 3840, },
            Height = Input { Value = 1920, },
            ["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2" }, },
            Font = Input { Value = "Open Sans", },
            StyledText = Input {
               Value = "[Frame #] 0\n[Resolution] 3840x1920px\n[Font Size] 0.08\n[Foreground Color RGBA] 1/1/1/1\n[Background Color RGBA] 0/0/0/0\n",
               Expression = "Text(\"[Frame #] \" .. (time) .. \"\\n\" .. \"[Resolution] \" .. Text4.Width ..  \"x\" .. Text4.Height .. \"px\" .. \"\\n\" .. \"[Font Size] \" .. Text4.Size .. \"\\n\" .. \"[Foreground Color RGBA] \" .. tonumber(string.format(\"%.3f\", Text4.Red1)) .. \"/\" .. tonumber(string.format(\"%.3f\", Text4.Green1)) .. \"/\".. tonumber(string.format(\"%.3f\", Text4.Blue1)) .. \"/\".. tonumber(string.format(\"%.3f\", Text4.Alpha1)) .. \"\\n\" .. \"[Background Color RGBA] \" .. tonumber(string.format(\"%.3f\", Text4.Red)) .. \"/\" .. tonumber(string.format(\"%.3f\", Text4.Green)) .. \"/\".. tonumber(string.format(\"%.3f\", Text4.Blue)) .. \"/\".. tonumber(string.format(\"%.3f\", Text4.Alpha)) .. \"\\n\")",
            },
            Style = Input { Value = "Bold", },
            ManualFontKerningPlacement = Input {
               Value = StyledText {
                  Array = {
                  },
                  Value = ""
               },
            },
            Alignment = Input { Value = 1, },
            ShadingGradient1 = Input {
               Value = Gradient {
                  Colors = {
                     [0] = { 0, 0, 0, 1 },
                     [1] = { 1, 1, 1, 1 }
                  }
               },
            },
         },
         ViewInfo = OperatorInfo { Pos = { 827, 82.5 } },
      }
   },
   ActiveTool = "Text4"
}



As a text formatting tip, the "\n" element is used to add a newline character in LUA, and "\t" adds a tab character to the output. This formatting approach is derived from how the C code language printf() function works.
Mac Studio M2 Ultra / Threadripper 3990X | Fusion Studio 18.6.4 | Kartaverse 6
Offline
User avatar

Ilir Beqiri

  • Posts: 4
  • Joined: Wed Apr 19, 2017 2:46 pm
  • Location: Tirana, Albania

Re: Scripting a Text Source (number counter)

PostWed Apr 19, 2017 5:42 pm

I totally get it now. Very well explained. I am returning to Fusion since the days of version 3 and I must say, I'm impressed with the changes in the expressions field. Not at all difficult and not much different from After Effects Javascript expressions. I'm trying to do with expressions what I already know how to do in Ae. Thank you again!
Offline
User avatar

Ilir Beqiri

  • Posts: 4
  • Joined: Wed Apr 19, 2017 2:46 pm
  • Location: Tirana, Albania

Re: Scripting a Text Source (number counter)

PostThu Apr 20, 2017 7:19 am

Is it possible to print out a point type into the styled text window? Say, for example that you want to ad a line that shows the pixel aspect of the node. Can you add a function to the expression (Point(PixelAspect.X,PixelAspect.Y)) and expect it to print it's return as a string? I already did it in this form:
Code: Select all
Text(PixelAspect.X .." : " ..PixelAspect.Y)

... but is there a way that I can write it as a short function? I mean:
Code: Select all
Point(Width/Height,1)

It is possible to write functions that print out single numbers, but how can I print out a function that returns an array?
Offline

Tomek Pawlowicz

  • Posts: 80
  • Joined: Wed Feb 13, 2013 9:28 am
  • Location: Cisownica, Poland

Re: Scripting a Text Source (number counter)

PostSun Oct 22, 2017 6:47 am

Hi there.

Is there a way to round numbers to two digits after the coma?
---------------
Tomek Pawlowicz
Offline
User avatar

Andrew Hazelden

  • Posts: 536
  • Joined: Sat Dec 06, 2014 12:10 pm
  • Location: West Dover, Nova Scotia, Canada

Using string.format() to Format Your Numbers

PostSun Oct 22, 2017 7:56 am

Tomek Pawlowicz wrote:Hi there.

Is there a way to round numbers to two digits after the coma?


Hi Tomek. You can use Lua's string.format() function to truncate how many digits of floating point precision are shown in your Text+ node expression field output:

Code: Select all
Text(string.format("%.02f", math.pi))


For a person running Fusion on a North American EN-US style localized operating system, the final result of this expression is the text:

Code: Select all
3.14


If you are on a localized operating system that uses a comma for the decimal mark character instead of the period character, then output from string.format() should be handled and displayed correctly for your region when you run the same code snippet.

For this example string.format() based expression the key thing to experiment with is how the text "%.02f" works as a format specifier.

The variable is being treated as if it is a float number by writing in (%f) in the function.

The initial "%.02" part of the formatting string represents that 2 digits will be printed beyond the decimal mark giving an output like "0.00" or "0,00" depending in on your localized region. If you wanted four digits of precision beyond the decimal mark you would write in (%.04f) which would give you "0.0000" or "0,0000" for the output.

In this demo the variable for "math.pi" is merely used to write in a sample floating point number which represents the value of pi.

Here is a code snippet you can paste into the flow area of your Fusion composite to see the result:

Code: Select all
{
   Tools = ordered() {
      Text1 = TextPlus {
         CtrlWZoom = false,
         Inputs = {
            Width = Input { Value = 3840, },
            Height = Input { Value = 2160, },
            ["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2" }, },
            Size = Input { Value = 0.3376, },
            Font = Input { Value = "Open Sans", },
            StyledText = Input {
               Value = "3.14",
               Expression = "Text(string.format(\"%.02f\", math.pi))",
            },
            Style = Input { Value = "Bold", },
            ManualFontKerningPlacement = Input {
               Value = StyledText {
                  Array = {
                  },
                  Value = ""
               },
            },
         },
         ViewInfo = OperatorInfo { Pos = { 1485, 280.5 } },
      }
   },
   ActiveTool = "Text1"
}



The string.format function is very similar to c-code's printf() function in how it lets you control the way variables are formatted and printed. For more information on the string.format() function, it is described in the Lua documentation here:

https://www.lua.org/pil/20.html

http://lua-users.org/wiki/StringsTutorial
Mac Studio M2 Ultra / Threadripper 3990X | Fusion Studio 18.6.4 | Kartaverse 6
Offline

Crayox

  • Posts: 4
  • Joined: Thu Feb 16, 2023 12:56 pm
  • Real Name: Hrvoje Licitar

Re: Scripting a Text Source (number counter)

PostFri Dec 01, 2023 2:53 pm

Hi there,
I too need this but did they change the Text() syntax because when I put my formula "floor((time/50)+7)" in it like Text(floor ((time/50)+7) ":00") it's not working. Just goes black.
Offline
User avatar

roger.magnusson

  • Posts: 3354
  • Joined: Wed Sep 23, 2015 4:58 pm

Re: Scripting a Text Source (number counter)

PostFri Dec 01, 2023 4:15 pm

You need to use the string concatenation operator, "..".

Code: Select all
Text(floor((time/50)+7)..":00")
Offline
User avatar

Andrew Hazelden

  • Posts: 536
  • Joined: Sat Dec 06, 2014 12:10 pm
  • Location: West Dover, Nova Scotia, Canada

Re: Scripting a Text Source (number counter)

PostFri Dec 01, 2023 4:16 pm

You could try an approach like this:
Code: Select all
Text(string.format("%0.2d:00", 7 + (time / 50)))


Fusion Expressions.png
Fusion Expressions.png (724.48 KiB) Viewed 1393 times
Mac Studio M2 Ultra / Threadripper 3990X | Fusion Studio 18.6.4 | Kartaverse 6
Offline

Crayox

  • Posts: 4
  • Joined: Thu Feb 16, 2023 12:56 pm
  • Real Name: Hrvoje Licitar

Re: Scripting a Text Source (number counter)

PostSat Dec 02, 2023 9:45 am

The two dots! Thanks :)
The second one works too, thanks guys!

Return to Fusion

Who is online

Users browsing this forum: No registered users and 29 guests