Do indicated part of script if condition is true and other one if false

Hey guys,

I was looking for this on forum but i can’t find anything. Maybe i type bad keywords, than just indicate other thread that consist my problem. I’m creating script that names equipment by coodrinates. I sort equipment coordinates in two ways: by “X” location and by “Y” location. I added also datashapes node where i can choose in which way my elements will be named. So the result of this choose is string “X” or “Y”.

Now how to determine by this two strings which way of script wil be execute? I was thinking about if else condtion, but i dont know how to do it really, and where put something to connect it. I think about two possibility places for any node or my own code.

  1. After sort by coordinates and preparing names list i join this list to value input in “Element.SetParameterByName” node. If there will be a condition that determine execute between list and this node it would works.

  1. I can place a code also before seperation for two ways of sort (by"x" and b “y”)

Or maybe there is much better solution that i am thinking about. Please help me :slight_smile:

Hi,

There is a If node in Dynamo. You plug in it your condition (which will be evaluated to true or false), and the two different values that can be outputed. Depending on the value of the condition, the If node will output the right part of the script.

There is also a CodeBlock way to do it : “condition? a : b;” means : “If condition is true, output a, otherwise, output b.”

There is also a ScopeIf node (which a do not recommend using, unless you know exactly what you are doing !) which will only evaluate the part of the script that will be outputed (more here : Node to stop / quit running dynamo graph)

The way you use those nodes/blocks will highly depend of the output you desire. If everything fails, you still have access to Python, which handle really way if statements.

I am not sure what do you mean. Can you visualize this in dynamo? I was thinking about this node but how to do it? Test input is my string output from datashapes? What now connect to true or false and how finally merge it with my two ways of script? I don’t know python and i am working about a week in dynamo so please be forgiving for me :smile:

EDIT:

And what is very important i cant put if node like true is a one way of script, and false is another. Beacuse of that i need to place if node at the beginning of workflow. I cant do it because that results of duplicate a lot of repeatable eleements and everything will be so much more complicated (about 8 times more).

So i need to place for example true results of if node between two other nodes. Like you can see on the picture i sent. There is visual example of my thinking:

What do want to sort by regarding your coordinates on picture 2?

The “if” statement in a codeblock as described ealier would look something like this:

The reason why there are two “outputs” are that two items are passed to the “x” value… :slight_smile:

EDIT:
The (if) codeblock (to the right) works as follows:

Is “a”-input equal to “x” input? If so please pass through the “GraphPass” input.
If not then pass the changed input (or a second input, if this is desired you simply write something different from the first input).

It is possible to extend with more cases like

a == x?
GraphPass:
a < x?
GraphPassLower:
a > x?
GraphPassHighter:
false;

The last statement “false” is the “else” statement if none of the above are true.

1 Like

@CVestesen I’m sorting equipment by “X” Coordintate or “Y” coordinate.

@Jonathan.Olesen

First of all one of code block doesn’t work with me with an error “kw_class expected”:

dynamo1

Second is that i don’t understand how it works, i need explain step by step.

Third is that i found a little trick that helped me. I just put if condition node. True respond is okay, but false respond connected to my setparameter node causes an error so that way of script doesn’t work at all. It’s not a good solution, but it works and it’s enough for me at the moment:

I have two ways like on screen. In one it checks that a==x. in second that a==y.

1 Like

Your first issue, with the red code block. What version of Dynamo are you using? If you are using 1.3 or earlier, lists are made with {} instead of []. So ["value","notvalue"]; should be {"value","notvalue"};

Second, please read up on general Dynamo logic/programming logic on how if statements work. That will teach you how it works, step by step.

Third, read what you are inputting into each node. The error you are getting is because if the test on the If node is false, you are feeding that code block’s false into Element.SetParameterByName’s parameterName input. No element has a parameter name of type ‘false’. That is impossible and will always be an error. What do you want to happen if the test is false?

Side note, the mark as solution button is meant for the post that helps you/solves the problem. If you solved it yourself, please show what you did to solve it and mark it as a solution. If it was another person who helped you, please mark their post that solved it.

You do not need to add [Solved] into the title, there will be a mark showing it has been solved already.

1 Like

In the picture above, it looks like you want to filter instead of using if. As in, check elements’ x == y and if it does, use it in these nodes. If it doesn’t ==, remove it.

See List.FilterByBoolMask

@kennyb6

Yes i use 1.3 version so it maybe causes an error.

I marked my post as solve and i explained it. I did it intentionally with this false respond attached to parameter name. That’s what i want to achieve. Like i said i have two ways one sort equipment by “X” coordinate, second way sort equipment by “Y” coordinate. At the end of that 2 ways there are similar nodes that names that equipment. And in my sloution if I will choose “X” for example it chekcs in one way that for “X” true is Mark respond and in this way it will execute it. But in second way i will check that for “X” it is false respond and that way of script won’t work because there is no way to execute setting paramteer value if i attached false to it.

Like I said, a FilterByBoolMask would work better than relying on errors and nulls. Especially since you already check for true or false anyways. If you post your Dynamo graph up until where the elements are, I can help you put it in.

And the part about solution, usually it is better to mark the person who helped you find/showed you the solution, since they showed you how to do it. This will help people who have the same problem as you in the future to find the right information. Both @mellouze’s and @Jonathan.Olesen’s posts explain how to solve your problem and show you the tools to do it.

1 Like

I would suggest @mellouze post as mine simply visualize his suggestion :slight_smile:

Sorry if you did not understand the idea of ‘‘if’’ statement in a code block… It works as: if this item true: then return this item, else return this item…
My dummy text was simply illustrating how it works not what you should necessarily write, but agree that a boolmask will aid you :slight_smile:

1 Like