Reputation: 364
Thanks in advance for your input.
I started learning Python today, because I want to define custom Python nodes in DynamoBIM (Revit) to be more flexible if there aren´t predefined / suitable nodes for my tasks in BIM.
The PythonScript gets input from input nodes, which are IN[i]
.
In my case I use 2 bool
values (IN[0]
, IN[3]
), 1x str
IN[1]
, 1x float
IN[2]
and 1x list
IN[4]
.
After processing the input via PythonScript it returns a result (OUT
), which can be used for further tasks.
I tried to attach a prefix in front of every list item, if IN[0] = True
and add a value IN[2]
to each list item, before it´s changed. The result is displayed in the watch-node.
In case of IN[3] = False
(list isn´t replaced) I get the desired result:
In case of IN[3] = True
, the custom list, doesn´t get adapted (no prefix added, no adding of values):
Code of the (integrated) PythonScript:
listing = [0,1,2,3,4,5,None,"null", "", "Text"]
praefix = IN[1]
add = IN[2]
if IN[3]:
listing = IN[4]
if IN[0]:
for x in range(len(listing)):
if isinstance(listing[x], int):
listing[x] = praefix + str(listing[x] + add)
OUT = listing
else:
for x in range(len(listing)):
listing[x] = listing[x] + add
OUT = listing
Python-code (compilable in online compiler)
listing = [0,1,2,3,4,5,None,"null", "", "Text"]
replacingList = [2,2,3,"Test",4] #IN[4]
boolPraefix = True #IN[0]
praefix = "Indexwert: " #IN[1]
add = 7 #IN[2]
customList = True #IN[3]
replacingList = [2,2,3,"Test",4] #IN[4]
if customList:
listing = replacingList
if boolPraefix:
for x in range(len(listing)):
if isinstance(listing[x], int):
listing[x] = praefix + str(listing[x] + add)
print(listing)
else:
for x in range(len(listing)):
listing[x] = listing[x] + add
print(listing)
I tried to reproduce the problem from the integrated script in a online compiler with python code, but in this case the expected result got calculated:
['Indexwert: 9', 'Indexwert: 9', 'Indexwert: 10', 'Test', 'Indexwert: 11']
Compiled with https://www.programiz.com/python-programming/online-compiler/
Expected result should be:
I don't currently have the slightest idea why there are different results between the online compiler code and the integrated PythonScript.
Upvotes: 3
Views: 614
Reputation: 364
Made some additional editing, solution is now working:
Following (integrated) PythonScript produces the expected result, when used as a node in Dynamo:
listing = [0,1,2,3,4,5,None,"null", "", "Text"]
praefix = IN[1]
add = IN[2]
custom = IN[4]
newListing = []
if IN[3]:
listing = custom
if IN[3]:
for x in range(len(custom)):
try:
listing[x] = int(custom[x])
except:
pass
if IN[0]:
for x in range(len(listing)):
if isinstance(listing[x], int):
listing[x] = praefix + str(listing[x] + add)
newListing = listing
else:
for x in range(len(listing)):
listing[x] = listing[x] + add
newListing = listing
OUT = newListing
Result can now also achieved for a custom list:
Upvotes: 2
Reputation: 532
I Previously had some issues with dynamo and python, in most times I found that the best practice is to use OUT
only once at the end of the code.
I took your sample and modified it, Try it.
I added an empty list that will be used as a container for the processed list, and assigned it to the output
listing = [0,1,2,3,4,5,None,"null", "", "Text"]
#Empty List to use as Output
NewListing =[]
praefix = IN[1]
add = IN[2]
if IN[3]:
listing = IN[4]
if IN[0]:
for x in range(len(listing)):
if isinstance(listing[x], int):
listing[x] = praefix + str(listing[x] + add)
NewListing = listing
else:
for x in range(len(listing)):
listing[x] = listing[x] + add
NewListing = listing
OUT NewListing
And don't forget to review your indentations within the Python node inside Dynamo.
Upvotes: 2