Ben, how can I update Random Seed values in a template, automatically?

Categories Nuke, Quick Tip, TCL

Some nodes in Nuke, such as the Particle Emitter or your favourite Camera Shake gizmo, have a random seed knob. This allows you to create a different variation of a setup using the same parameters on a given node. When these setups are used throughout a sequence, we often want to change up the random seed value so every shot matches an approved look, but doesn’t behave in exactly the same way.

When setting up a sequence template, you could expression link all random seed knobs to a NoOp-based control panel, but there is a better, more automated way to get a “random” value by default.

An individual shot in a sequence already has a unique number associated with it — its name! This is a perfect default value for a random seed. In this example, I have shot 0120 and 0140 from the ABC sequence. We are able to write some TCL to isolate the shot’s number from the Read node’s file knob. To demonstrate, I’ll use a Text node to print the progress of how we break down this file name.

The first thing we’ll utilize is topnode. Topnode is an inbuilt TCL function that crawls up your B-pipe until it reaches the first node in the tree — this should always be a Read node. Typing [topnode] in the Text field returns a gibberish node index number that isn’t useful to us yet, so let’s keep going…

To return the file path, we can use [value [topnode].file]. Each time we wrap some text in a pair of square brackets, we’re asking Nuke to evaluate a new bit of TCL code. So here, we’re evaluating which node equates to topnode, and then we’re finding the value of its file knob.

Another useful TCL function is basename. We can use it to only return the filename from a given filepath, like this: [basename [value [topnode].file]]

We’re getting close! Now, how do we predictably return just the shot number from the filepath? First, we can split this string into a list of values, based on a defined set of characters. This sounds super confusing, but I promise it’s not once you get the hang of how it works… Typing [split [basename [value [topnode].file]]] will start the expression, and we can tell Nuke to create a new list item wherever our string is separated by an underscore, like this: [split [basename [value [topnode].file]] _].

We can view each list item individually by using TCL’s list index, or lindex function. [lindex [split [basename [value [topnode].file]] _] 0]. In most programming languages, list indexes start at 0.

  • Using a list index value of 0 returns SHO, because our string is split up into parts separated by the underscores.
  • 1 returns ABC, our sequence name.
  • 2 returns the shot name, as well as the frame number, and file extension.

[lindex [split [basename [value [topnode].file]] _] 2] We’re getting close!

TCL’s split function allows us to use more than 1 character to split the string. So right after the underscore, we can also add a period [lindex [split [basename [value [topnode].file]] _.] 2]. Just like that, we have our shot number!

To better explain what is happening, I’ll temporarily remove the index number. Prior to adding the period to our split function, we were separating the string by the underscore, so index:

  • 0 = SHO
  • 1 = ABC
  • 2 = 0120.1001.exr
  • 3 = nothing, because we’re at the end of the list.

When we use both an underscore AND period to split the string, index:

  • 0 = SHO
  • 1 = ABC
  • 2 = 0120
  • 3 = 1001
  • 4 = exr
  • 5 = nothing, because we’re at the end of the list.

Now that we have figured out the expression needed, I’ll add it to the random seed of my Camera Shake gizmo. As we copy the Camera Shake gizmo to the next shot, the value updates!

Notice that as I add more nodes in-between the plate & the Camera Shake gizmo, the expression doesn’t break? This is the power of topnode! The one gotchya is if you use a Switch or Dissolve node — no matter the value, or if the node is disabled, topnode will only crawl up the 0 pipe.

In cases where this is an issue, we’re able to use TCL to get the shot number from our Nuke script’s filename instead. To do this, we can replace [value [topnode].file] with [value] in our existing expression: [lindex [split [basename [value]] _] 2].

As you can see, using TCL is a great, predictable way of automatically updating the random seed knob on any node when setting up a template for your team.