If statement on empty branch

Hello everyone,
premise: I’m very noob with if statements in grasshopper so if anyone knows some good source to learn them in general I’d be grateful.
Said that, I need to do the following:
-read a tree
-if a branch is empty add a given element

Attached you can find an example file to work on

test if gate.gh (9.3 KB)

Hi !

If a branch is empty, its length is 0, which can be tested with List Length.
Then depending on the result, you can weave the output.

However, for your specific case (shattering curves), I’d recommend the second option of always adding the start and end bounds of the curve domain to the shattering parameters. This way you are guaranteed to retrieve the entire curve in case you don’t have intermediate values.

test if gate.gh (16.8 KB)

thanks for the fast answer, you are right in this particular case what you suggest is surely the best option.

I’m still interested to know how “if statements” work in general in grasshopper, do you know any documentation that explains that?

I used to teach if/then/else statements using the Pick’n’Choose component like this :

You have components that allow you to establish some conditions (True/False). Equality, Smaller Than, Larger Than, Match Text are among the most useful.
If you have several conditions to test, you can use logical operators (and/or/xor/not) to combine them.

The thing that changes in Grasshopper versus classic programming is that you need to evaluate both the then and else branches for each item in your list/tree, and the condition result is only there to pick the correct one. Basically, in your example, even though List Length = 0, you still compute the shattering operation on the curve (which returns an empty branch), only to ignore that specific result, and pick the original curve instead.

Note that Pick’n’Choose can also be used as an equivalent to a switch/Select Case operating on an integer value, since you can add as many inputs as you want.

Sometimes, for convoluted mathematical expressions (e.g. x>2.43 y + sqrt(z1/2) + …), it’s best to use Expression, which accepts the syntax if( condition , then , else).

1 Like

That is a precious help! You solved all my doubts in 20 minutes thanks a lot!

For simple if statements, where I just want to switch between 2 options I use the Stream Gate. Works great with Boolean toggle inputs. You can also add more potential inputs by zooming in. Then you would switch between cases using an integer.

In general in GH true can either be boolean or 1 and false can be boolean or 0.

If I want to do something “per branch” of a tree, I would usually use Pick’n’choose like mentioned or Weave.

If you want to do things with trees in GH, which you eventually will, I can also recommend the Treefrog set of commands, which is now part of the Human plugin for Grasshopper. Human | Food4Rhino It is amazing what it can do and is easily my most used plugin. It can make a lot of the more painful tasks of handling trees a lot easier.

1 Like