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: 25
  • 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: 127
  • 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 858 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 760 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.
Offline
User avatar

Andrew Hazelden

  • Posts: 127
  • 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 761 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 761 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.
Offline

davidsikes

  • Posts: 25
  • 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: 127
  • 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 761 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.
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?

Return to Fusion

Who is online

Users browsing this forum: No registered users and 6 guests