Introduction
This post will show various ways you can use a switch node to automatically trigger when one input is empty, a very useful technique when working with HDAs.
Especially important when working with complex generators, this way you can still get a preview of your output, or even prevent the node to fail cooking or getting stuck in infinite loops when using the HDA in Unreal Engine.
Setup
For showing off the technique, I prepared a simple setup with an HDA that applies a Polywire operation on the input curve
The HDA graph:
Switch node
You can see the Switch node has 2 inputs, one is the input curve and the other one is another curve used as a fallback option.
The easiest way to make the switch activate and use the second input is to check the number of points of the first one, if they are equal to 0 we can trigger the switch.
For more complex cases you might want to check if the points are less than a certain value, in this case since I’m working with a curve I want the input to have at least more than 2 points, and use the fallback option for anything less than that.
This can be done with a simple expression:
if (npoints("../Curve") < 2, 1, 0)
The “if” function takes in 3 floats, in the first one we can input the check expression we need to do, while the second one is the output value if the expression is true and the third one when it’s false.
if(<expression>, <true_value>, <false_value>)
Now, if I remove the input to the HDA or if I feed to it something that contains only 1 point it will use the Fallback Curve which in my case it’s a simple straight line.
Simplify
We can actually simplify it further, at the moment the input for the “npoints” function is a hard reference to one of the nodes in the graph, but we can simply replace it with “0” since the node we want to check is one of the inputs. The same way you would do when using a Wrangle Node.
So the below expression will give us the same result:
if (npoints(0) < 2, 1, 0)
Trigger from Other Nodes
If you do need to drive the switch using another node you can do it using the first example shown, having the name hard coded:
if (npoints("../Check") < 2, 1, 0)
But better than that, to make it more clearly visible for anyone looking at your graph or for your future self when coming back to it, you can use a Spare Input.
Create by clicking on the cog icon:
Now if you click and drag your Check node in the Spare Input Slot you will create a reference and now you can use “-1” in the expression to access it.
if (npoints(-1) < 2, 1, 0)
Switch-If node
This node only takes in 2 inputs but it gives a lot of options and operations to trigger the switch, like checking if attributes or groups are present or equal to specific values.
I haven’t played much with it myself with these options, but the same expressions shown above work with this node as well.
Here I found you could even just use “npoints(1)” and switch the order of the inputs, because everything above 1 gets clamped.
So if there are no points in input 1, the result of the expression will be 0 and the first input will be used instead.
Conclusions
I hope you found this post useful for your future HDAs, have a great rest of the day!
Leave a Reply