Blackmagic Fusion Simple Expressions-Cookbook + Tutorial

Write Metadata into Text+

I will explain the following examples in depth, if you just want the code in a neat table click here.

You will most likely never use all the examples shown below, and they do get quite tricky. So feel free to skip ahead to only the parts that you need. I personally have to look at this post myself if I need some special formatting of text.

I will show this using a Text+, but remember that you can use the Set Metadata tool to write your own metadata into your renders, too!

Read and insert Filename

This is very helpful. Burning Data into your image can be important when working with several people of simply keeping organized. For example, you want to write the Filename into your video.

Take a Text+ tool and right-click on the Text field to activate the Expression. Use the name of the Loader which carries the footage with the Metadata you want to access. In my case this is Loader2. Type this into the Expression field:

Text(Loader2.Output.Metadata.Filename)
Loader metadata

We already know what the Text() Expression does. It converts whatever other Input we give it, into a string that can be used by the Text+ tool. .Output.Metadata. is simply the path to the Metadata that is saved in the image. The metadata that we want in this case is simply called Filename. If we want some other data we simply need to change Filename to whatever the desired metadata is called.

But let’s get back to the filename. As you can see, this Expression returns this into our viewer:

Loader metadata

This is great because it’s easy to use and will show us several pieces at the same time. Like file path, filename, and frame number. But what if we don’t want such a long text. We might just need the actual name.

Format the filename

As mentioned at the beginning of the post, much of Fusion is based on the coding language Lua. This also includes Simple Expressions. So let’s use Lua to show us only the name, not the whole file path. (Don’t be intimidated, I personally didn’t understand how it actually worked before this blog post either. I just knew it was possible and wanted to understand it. Even if you don’t, you know it’s there and can copy and paste it if you need.)

First, we use something different than the Text() Expression. There is no particular reason other than that I often make a mistake due to the wrong number of brackets. Instead, I will use :return. I’ve seen this somewhere on the web once and it doesn’t need brackets as we are entering Lua. Only downside: it won’t convert the input to a string for us. But if we make it return a string, the Text+ tool will print it. For example, type this into the Expression field:

: return Return this

Doesn’t work right? Now try this:

: return "Return this"

Now it works because in Lua a string is declared using ” “. Good thing we will feed it a string anyway. I just wanted to show you what : return () does.

Next, we will use a function of Lua called string.match(string, "pattern"). This is explained in detail here. But in short, it prints out parts of a string based on a pattern that we create. So if we feed it this string “C:\Test\Text” and set the pattern to “Text” it will return only “Text”. This would look like this:

: return string.match("C:/Test/Text", "Text")

Go ahead and try this in Fusion.

string.match function

Obviously we don’t want to write the file name ourselves, we will use some of Luas pattern matching tricks.

I will show you what the Expression will look like and then explain it, remember to change the Loader name to the name of yours. The Expression should be something like this:

: return string.match(Loader1.Output.Metadata.Filename, ".+[/\\](.+)[.]")

This will correctly return this:

string.match function
I know my file name sucks, but I was testing Solaris today

What the heck is that code?! That was at least my reaction. I will break it down below, but you can also read this to learn more about it.

After the Loader1.Output.Metadata.Filename, we have the "Pattern". We use several symbols which each have their own meaning. Let’s break this pattern down.

".+[/\\](.+)[.]"

“.+” The point stands for any single character, you can test this by using a single point as your pattern. Like this:

: return string.match(Loader1.Output.Metadata.Filename, ".")

This will only return the very first character of your file path.

string.match function

The “+” means 1 or more repetitions. So if we type in

: return string.match(Loader1.Output.Metadata.Filename, ".+")

This will simply match our pattern with any one character until the end, resulting in the whole file path.

string.match function

Next we have the slash in the brackets [/\\]. These bracktes mean that we are excluding slash and backslash symbols. You need to use double backslash for Lua to recognize a single backslash. I use a slash and a backslash just because. For Windows the backslash should be enough, for other operating systems you will need the slash (or even other symbols).

Now we have another .+ but this time between normal brackets (.+). These indicate a subpattern, and what we are actually searching for. Meaning all characters.

Then we close the pattern by using [.]. Which excludes all character again.

This might seem weird but in actual words, the pattern is like this: We are searching for a pattern that starts with any characters, which are not a backslash, it contains any characters, and is between a backslash and any characters.

This only leaves us with the filename.

We can delete the last bit and we would see the filetype as well. You can learn more about customizing the pattern here.

string.match function

Kind of weird, I know, but very helpful. But guess what that’s not all! We can make this even prettier.

Show your MetaData with style (Line breaks and more formatting)

This time I am using the Text() Expression again, just to show that it works, but remember to end it with “)”. Because guess what, while writing this, I forgot the ending bracket.

If we type this into the Expression:

Text("Name: " .. string.match(Loader1.Output.Metadata.Filename, ".+/\[.]") .. "\n" .."Current Frame: " .. time .. "\n" .. "Comp Name: " .. comp.Name .. "\n" .. "Resolution: " .. Loader1.Output.OriginalWidth .. "x".. Loader1.Output.OriginalHeight )

We will get this:

string.match function

I’ll quickly run over the different parts, there is really nothing complicated going on here. We declare the string "Name: ", which will be printed just like that. Then we use .. to tell the Expression that there is more coming. Next, we have our Loaders Filename, as explained above, and then use a "\n" which simply forces a line break.

The rest is pretty self-explanatory. time prints out the current frame number, comp.Name the name of the current composition, and Loader1.Output.OriginalWidth simply tells us the width resolution of our incoming footage. Just remember to use the two dots .. between the different pieces, and "\n" for line breaks.

Getting User Data (like Font) in a simple Expression

Let’s say you want the client to decide on a font for your project. The best way would be to have the actual text in the image and the name of the font. In a Text+, right-click the text field and hit Expression, now take the pick whip and drag it to the Font.

Fusion Font in Expression

This s what we get:

Great! Let’s add our sample text and call it a day.

Huh! This doesn’t work. Using a Text() Expression won’t help either. But why doesn’t it work?

Type in this:

Text(Font)

This is what we get:

As you can see, we don’t actually get a string back from this Input, but user data. This is why it doesn’t work with different types of Expressions, we were mixing data types. To circumvent this we can ask the Font Input for its value (which will be a string). We do that like this:

Text(Font.Value) 

Knowing this, we can finally create our font showcase.

I was very bold and chose Times New Roman
Text("Lorem ipsum dolor sit amet " .."\n".."Font: "..Font.Value .. "\n".."Style: " .. Style.Value)

The Style Selection Box is also a dynamic user data field which is why we also need to use Style.Value.

More formatting with string.format

Sometimes you want to make numbers pretty. In Lua you can use string.format for that. A whole list of possible formations is available here. This is a reference for C and not Lua, but as I’ve recently learned, Lua uses C as a base for the string.format.

In this example, I am referencing an input from another Text+ tool three times in the same Expression but I format it differently:

string.match function

This is the Expression I’ve used:

Text(string.format("Float: %.3f, \nExponent: %.1e \nInteger: %.3d", Text1.LineSpacingClone, Text1.LineSpacingClone, Text1.LineSpacingClone))

The idea here is simple. First we declare what kind of formatting we want. %.1f will format our number into a float with the formatting we choose.

Table of Expressions from this Page

Loader1.Output.Metadata.Filenamereturns the whole File path of the footage inside the Loader
string.match(Loader1.Output.Metadata.Filename, ".+[/\\](.+)[.]")returns the Filename of the Loader
"\n"adds a line break
timereturns the current frame number
Comp.Namereturns the composition name
Loader1.Output.OriginalWidthreturns the width resolution
of your footage
Loader1.Output.OriginalHeightreturns the height resolution
of your footage
"Type anything"creates a string with Type anything
Input.Valuereturns the value of a user-control (like the Font Input)

On the next page we will deal with sinus and cosinus.

Quickly share this!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to top
0
Would love your thoughts, please comment.x