Help with IF Conditional Statement

#1

Hello everybody!

I’m relatively new to Dynamo and in need of a quick help. I’m trying to create a script which should do something like this:

Please find me all the Cable Trays in the model. IF for those elements in the Comments field (parameter) is written “123” than please write “456” instead of that. IF NOT, do nothing, have a beer and relax…

Of course, I did something wrong. He’s writing “456” to every single Cable Trays in the model and I think that my IF loop is not used at all! This is just a simple example for my general understanding of how IF loop really works in Dynamo. I’m guessing that there are many other ways to perform this task, but I would really like to do it with the IF loop.

Thanks!

#2

Hi,

If you really want to use the IF node, you should use it to create a new list of values where 123 is replaced with 456 and then use the Element Set Parameter by Name node.

I normally use FilterByBoolMask or AllIndicesOf and GetItemAtIndex to filter the elements.

Hope it helps
Giovanni

1 Like
Change parameter with value conditions in a list
#3

Excellent, this explained a lot! Thanks Gio!

#4

You dont need to create a new list, you can just pass those elements which is true, and then change their parameter, leaving those who fail the statement as they are…

cableTray.dyn (7.6 KB)

2 Likes
#5

Oh, really nice! Different approach… I like it! Thanks a lot erfajo! Cheers!

#6

And another option… I find it useful when needing so set a value for the “false”.

#8

As stated it shows how elements which fail the test are given a value.

It expands upon a solution in the second post making this possibility explicit.

#9

The if node has been bothering me for quite some time. I have created my Python node that works like an IF. It might not be brilliant code but it gets the job done.

"""
Replacement of Dynamo IF node
"""
__author__ = Daniel Gijsbers 
__version__ = '1.0.0'

# Enable Python support and load DesignScript library
import clr

# The inputs to this node will be stored as a list in the IN variables.
ToF = IN[0] # a boolean
List1 = IN[1]
List2 = IN[2]

Result = []

if ToF == True:
	Result.append(List1)
else:
	Result.append(List2)


flattened1 = [item for sublist in Result for item in sublist]

OUT = flattened1
1 Like
#10

Nice,

Maybe have a look at the Clockwork ScopeIf+ it contains this very nice code…

Also when you paste python, if you hit the </> button it will format it a bit better.

Hope that’s of interest,

Mark

#11

thanks!

#12

Would you mind commenting your flattened1 = I can never understand how those work :slight_smile:

Cheers,

Mark

#13

Yes I would really like to do it…
The only things is I don’t know, I copied it from someone else on the forum. Python does not seem to have a regular flatten command. But after searching around again I found this:

So with the DSCore reference i could turn it in to this:

"""
LIST: Replacement of Dynamo IF node
"""
__author__ = Daniel Gijsbers
__version__ = '2.0.0'

# Enable Python support and load DesignScript library
import clr

clr.AddReference('DSCoreNodes') 
import DSCore
from DSCore import *

# The inputs to this node will be stored as a list in the IN variables.
ToF = IN[0] # a boolean
List1 = IN[1]
List2 = IN[2]

Result = []

if ToF == True:
	Result.append(List1)
else:
	Result.append(List2)
# Making use of Design script for flattening

OUT = List.Flatten(Result,2) # The second input allows for the level of flattening<a class="attachment" href="/uploads/dynamobim/original/3X/9/0/90a184274093ef3954181ef961993ae1d8b8fdf8.dyn">DesignScripORPythonIFnode.dyn</a> (14.3 KB)
1 Like
#14

flattened1 is using list comprehension, explained in the Python documentation here.

Here is how it is being implemented in the above code:

# Each sublist has 3 items
sublist1 = [1, 2, 3]
sublist2 = [4, 5, 6]

# 'Result' contains our 2 sublists and looks like this:
# [[1, 2, 3],
#  [4, 5, 6]]
result = [sublist1, sublist2]

# List comprehension to flatten
flattened1 = [item for sublist in result for item in sublist]

# Alternate formatting
flattened2 = []
for sublist in result:
    for item in sublist:
        flattened2.append(item)

OUT = flattened1, flattened2


List Comprehension.dyn (4.9 KB)

In this example, flattened1 is essentially created using a shorthand version of the for loops for flattened2.

Edit: It should also be noted that this will only work with a list having 2 levels. If each sublist were to contain a sub-sublist, it wouldn’t return individual values. Recursion might be a better approach in order to account for all possible levels of lists.

1 Like
#15

Great thanks, the link’s point that the ‘for’ expression is the same kind of helps, I mentally insert a new paragraph after the ‘result’ and it all makes more sense :slight_smile:

1 Like
#16

It is a better idea to make a definition for flatten. A much better solution for this would then be to implement it a real oop environment, but that is another ballgame :slight_smile:

def Flatten(lst):
    for item in lst:
        if isinstance(item, list):
            for i in Flatten(item):
                yield i
        else:
            yield item
2 Likes