If-statement gives wrong result

I dont understand why the if-node outputs a wrong result, when running through chosen one of two data inputs.
I can manage to do it in python, but would like the dynamo node to work correctly, as the next person looking at this script might not be familiar with python.

Seems like the list len is based on the false statement, but the items on the list differentiates between the lists.
(Is there any way to show more of the watch node than the ‘default’ size)

Try searching the forums for similar scenarios first next time. This question comes up quite frequently. This issue is with how the If node handles list structure with its inputs. The inputs must match in structure and length. In order to pass an entire list as an output option, you have to use a DesignScript workaround.

i = test?0:1;
[returnTrue,returnFalse][i];

What this does is turn your true and false inputs into a list of two possible outputs and returns the output you want, based on the test result you pass through the node. A “true” test returns the first option, returnTrue, and a “false” test returns the second option, returnFalse.

As for your second question about Watch nodes… Version 2.11 does have resizable Watch nodes, but the previous versions do not.

1 Like

Not the false, the shortest list…irrespective if its plugged into the true or false

Thanks for the quick answer and sorry for not being thorough enough searching the forums.

Any idea why the behavior works like this and what would is the use for the if statement node then?

As I mentioned, it expects matching list structure and length. So every true/false option should have a condition (test) to determine which respective output it returns.

3 Likes

I was recently doing something similar and I just realized a way around this limitation of equal list length requirement. I wanted the if node to be a sort of switch toggled by the user inputting a single bool. My idea is to feed a 0 and 1 list as the true/false inputs for the if node, then the output is used as an index fed into a get item at index node which can then call either list despite the mismatch. Hope this is helpful to any looking.

1 Like

This is actually the standard workaround for filtering lists with uneven lengths. Typically it’s written in shorthand like this:

i = test?0:1;
[returnTrue,returnFalse][i];

or

[returnTrue,returnFalse][test?0:1];

1 Like