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.
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.
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
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="//cdck-file-uploads-global.s3.dualstack.us-west-2.amazonaws.com/flex016/uploads/dynamobim/original/3X/9/0/90a184274093ef3954181ef961993ae1d8b8fdf8.dyn">DesignScripORPythonIFnode.dyn</a> (14.3 KB)
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
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.
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
Can I build an “If” statment with thre conditions, I am gonna try to explain the problem, I have a number slider from 1 to 3 and my problem is that I want to get these results: if its 1 than it should be 0, it its 2 it should be 1 and ift its 3 it should be 2.
I can do it only with two condtions becaus it works with true or false but with three I can not figuer it out.