Automated Defocusing Using Plate Metadata In Nuke
⭐ How to take the guesswork out of defocusing…

⭐ How to take the guesswork out of defocusing…
Taking Out The Guesswork
When it comes to defocus and depth of field, matching your CG objects to your plate can sometimes be quite challenging – especially when there’s a focus pull, or when objects move in and out of focus.
You’ll have to juggle and balance three main factors:
- Focus distance – e.g. the focus plane (C) setting in the ZDefocus node.
- Blur size – e.g. the size (and related: the maximum) setting in the ZDefocus node.
- Bokeh kernel – e.g. the filter settings in the ZDefocus node.
– And getting all three right just by eye can be difficult.
So, let’s take out the guesswork.
Many people will already know that we’re able to automate the settings in the second and third points:
The blur size can be calculated based on the camera/lens information and the distance to the plane in focus, and there are great tools available which perform these calculations for you, such as PxF_ZDefocus and OpticalZDefocus.
Take for instance the PxF_ZDefocus node. By ticking the Enable Lens Simulation checkbox in the Lens Simulation tab of the node’s properties, you can enter the world scale that you’re working with, and the focal length, f-stop, and filmback width of your camera/lens, and it will output the blur size for you automatically.
You can also extract a bokeh kernel directly from your plate, and use that as the filter input for an automatically perfect bokeh match. I’ve shown how to do this in my Compositing Photorealistic Lens Effects In Nuke guide.
So, in this guide, I’m going to be focusing on tackling the automation of the setting in the first point: the focus distance.
Using Metadata
Modern cinema cameras capture tons of metadata, which is encoded into the images that we receive to work with in VFX.
One such metadata entry is – you’ve guessed it – focus distance.
By connecting a ViewMetaData node to your scan, you can view this metadata.
In the properties of the ViewMetaData node, scroll down or search for the metadata key that corresponds with focus distance – often just called FocusDistance, or some variation of that.

An example of focus distance metadata.
In the value field next to it, you can see what the focus distance was on that frame. This field will update for each frame, and so what you have is the focus distance on every frame across the whole shot.
But how can we actually use it?
We can extract this information from the metadata by using Nuke expressions:
First, create a NoOp node and connect it to your scan (so that the metadata from the scan flows through to the NoOp node).
Then, in the properties of the NoOp node, create a Floating Point Slider.
You can do this by clicking on the little pen icon at the top of the properties panel, and then dragging a Floating Point Slider (the first icon next to the pen) down onto the properties of the NoOp node.
Name and label the slider for example FocusDistance, and hit OK.
Next, right click on the slider that you just made, and select Add expression.
Here, we’ll add the expression which pulls the focus distance value from the metadata.
In this case (as you can see in the previous image above), there are two options to choose from – because the focus distance was recorded in both imperial and metric units.
You could use either – it’s easy to convert between them – but I’ll pick metric since that’s what I’m most familiar with. The important thing is that you know (or find out) what unit the focus distance was recorded in.
So, the metadata key that I will use in this case is exr/FocusDistanceMetric.
To get the corresponding value, type in the following expression:
[metadata exr/FocusDistanceMetric]
(Replace exr/FocusDistanceMetric with the name of your metadata key).
However, we quickly run into a problem.
The value is not a number, because it also contains letters ( mm). It’s a string: a data type that consists of alphanumeric characters (letters and numbers) – which a floating point slider doesn’t understand. (It expects to receive numbers, only).
So, we’ll have to remove that suffix from the metadata key before we continue. Let’s place our expression into another expression:
[string trimright [metadata exr/FocusDistanceMetric] " m"]
Above, the word string means that we’re manipulating a string.
The word trimright means that we’re removing (trimming) any trailing characters (i.e. the suffix; on the right side of the string) from the string, based on what we have specified within the quotes.
Note that a space is also counted as a character. So, from our metadata value, i.e. “1746 mm”, we have to remove the space and the two m letters.
With trimright, we only have to specify that we’re removing a character once in order to remove all instances of it from the suffix – hence the " m" in the expression above. (Notice the one space and the one m).
Hit OK, and the expression will now output the animated focus distance from the metadata.
– If your metadata value has a prefix, you can use trimleft instead. For example, if the metadata value was “mm: 1746”, you could write:
[string trimleft [metadata exr/FocusDistanceMetric] "m: "]
Next, if you’re working with a different world unit than what’s in the metadata, you can simply convert the output directly within the expression.
For example, if I wanted my original expression to output the focus distance in centimetres instead of millimetres, I would divide it by 10 (or multiply it by 0.1):
[string trimright [metadata exr/FocusDistanceMetric] " m"] * 0.1
If you look at the imperial version of the focus distance in the picture above, you’ll see that its value is “68740 inch”. If you convert that to millimetres, it’s 1000 times more than the 1746 mm that we’ve got in the metric version of the focus distance.
By looking at the scan and using common sense, you can tell whether the focus point is ~1.7 metres (~69 inches) away or 1.7 kilometres (~69,000 inches) away. And, here, the imperial units are clearly listed in 1/1000ths of an inch, and should be divided by 1000 in order to be in inches.
Yours may or may not be in 1/1000ths of an inch. So, just be careful and check that your values are correct.
– To output the focus distance in inches using the FocusDistanceImperial key above, you could write:
[string trimright [metadata exr/FocusDistanceImperial] " inch"] / 1000
(Notice that we’re trimming the letters i, n, c, and h, and space, this time).
And, if you for example wanted to further convert that into centimetres, you would multiply it by 2.54:
[string trimright [metadata exr/FocusDistanceImperial] " inch"] / 1000 * 2.54
Okay, whichever version of the metadata value that you have extracted, and however you have converted it, the animated focus distance values are still “locked” within an expression. The NoOp node still has to be connected to the scan in order to output the values. So, let’s bake them down into keyframes which we can use elsewhere.
Right click on the field containing the expression, and select Edit → Generate. Select your frame range and hit OK to bake the expression down into keyframes.
Then, right click again on the same field (now containing the baked keyframes), and select Copy → Copy Animation.
Finally, paste the animation into the focus distance field in the defocusing node of your choice.
Now, you’ve got the animated focus distance for “free”, perfectly matching what’s happening in the plate.
Combine this with the other two points mentioned earlier, and you’ve pretty much got a fully automated defocus.
I hope you found this tutorial useful. For more Nuke tips & tricks, see Nuke.