In the previous article you finished learning all the useful attributes in a particle system. And as promised, you’ll begin to learn about expressions in this article, but nothing you haven’t done before!
Anyone that’s worked with Nuke for a good while have undoubtedly come across this expression:
$gui?1:0
It checks if Nuke has its graphical user interface loaded, returns 1 if it is, and 0 if it isn’t.
You might not be aware of what this is called.
This is a very basic example of a TERNARY OPERATOR.
The term “ternary” means “of three parts”, that’s why the ternary operator takes three arguments. And they can be found in many programming languages.
This is what a ternary operator looks like:
condition ? A : B
- “condition” is a an expression that returns a boolean value
- ” ?” indicates the end of the first argument
- “A” is the return value if “condition” is true
- “:” indicates the end of the second argument
- ” B ” is the return value if “condition” is false
In its entirety, this expression means:
- If “condition” is True
- return “A“
- If “condition” is False
- return “B“
“Though a conditional statement in nature, the ternary operator varies from if-else statements in that a value must be returned.”
With jargon out of the way, let’s look at some practical uses of ternary operators by writing some particle expressions!
I’d like to say that I know the examples you have and are going to see are quite bland and often unnecessary because the same things can be done with other particle nodes. But efficiency or flare is not my goal with this series of articles.
My goal is for you to have true understanding.
Example 1:
age > 20 ? 1 : 0

If age is not greater than 20, set color to 0.

This expression checks to see if the age of the particles are greater than 20:
age > 20 ?
Then returns the first value if true or the second value if false:
1 : 0
Thus, when the age is greater than 20, the particles turn to white, otherwise they stay black.
Can you guess what the same expression does if it’s applied to the opacity attribute?
Example 2:
vel > 0.5 ? ( vel*1.1 ) : vel


If velocity is not greater than 0.5, set velocity to the original velocity.

Particles with velocities greater than 0.5 have their velocities increased by 10%.
Particles with velocities below 0.5 are unchanged.
This one is a little more complicated, but it’s using the same principles as the example above, let’s see how it breaks down!
This expression checks to see if the velocity of particles are above 0.5:
vel > 0.5 ?
Then returns the first value if true or second value if false:
( vel*1.1 ) : vel
The first value in this case is the velocity multiplied by 1.1, and the second value is the original velocity.
I’ve intentionally included brackets for clarity in this example, but it’s not necessary and the expression could be rewritten to be:
vel > 0.5 ? vel * 1.1 : vel
Ternary operators can also be nested, which means you can use a ternary operator as an argument in another ternary operator in another ternary operator in another ….
For the next example, I want you to think about what the ternary operator is doing!
Example 3:

What is this expression doing?

Did you find the nested ternary operator?
This expression checks if the age is greater than 10.
age > 10 ?
And returns the first value if true and the second value if false:
age <= 15 ? 8 : 0 .5 : 2
Hurts the eyes to look at, but let’s use brackets to make things a little more pleasant:
( age <= 15 ? 8 : 0.5 ) : 2
The first value is the nested ternary operator and it breaks further down into:
Is age lesser or equal to 15? Return 8 if true, or 0.5 if false.
And finally if the original condition evaluated false, the expression would return the second value, which is 2.
To bring it together:
If the age is greater than 10 but lesser or equal to 15, set size to 8.
If the age is greater than 15, set size to 0.5.
Finally, if the age is less than 10, set size to 2.
That wraps up the introduction to ternary operators, once you get past its cryptic nature, it’ll be a powerful tool in controlling not just particles but much more in Nuke.
We’ll write more intricate ternary expressions a few articles from now, for the next article, you’ll learn how particles streams can be grouped and how you can use these groups make particle systems even more complicated!

