Use the Flatten node in python script

python
dynamo

#1

Hi, i try to use the Flatten node in a python script.
For some reason it didn’t work…


**Error:**NameError: name ‘Flatten’ is not defined

python code:
import clr
clr.AddReference(“RevitAPI”)
clr.AddReference(“ProtoGeometry”)
clr.AddReference(“RevitNodes”)
clr.AddReference(“RevitServices”)
clr.AddReference(‘RevitAPIUI’)

import Autodesk
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
import RevitServices
from Autodesk.Revit.DB import *
from Autodesk.DesignScript.Geometry import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
#The inputs to this node will be stored as a list in the IN variables.
test=IN[0]

result=[]
result=Flatten(test)
OUT=result

Any idea whats wrong? I use several other nodes…and they work just fine.


#2

Hi Hannes,
You need to import DSCore for DesignScript nodes to work and then use it like this, where the second argument is the amount of levels to flatten:


How to Convert dynamo nodes to python script
Python Nodes Basics
#3

Hi @Hannes,@viktor_kuzev, @T_Pover

Here is another possible way without Design Script :slight_smile:


#4

He thx for your help! @ T_Pover…With your solution i got a different error:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. 
Traceback (most recent call last):
  File "<string>", line 16, in <module>
IOError: System.IO.IOException: Could not add reference to assembly DSCoreNotes
   at IronPython.Runtime.ClrModule.AddReference(CodeContext context, String name)
   at IronPython.Runtime.ClrModule.AddReference

python code:

import clr
clr.AddReference("RevitAPI")
clr.AddReference("ProtoGeometry")
clr.AddReference("RevitNodes")
clr.AddReference("RevitServices")
clr.AddReference('RevitAPIUI') 
import Autodesk
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
import RevitServices
from Autodesk.Revit.DB import *
from Autodesk.DesignScript.Geometry import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
#The inputs to this node will be stored as a list in the IN variables.
clr.AddReference('DSCoreNotes') 
import DSCore
from DSCore import *
test=IN[0]
result=[]
result=Flatten(test,2)
OUT=result

#5

Your code works good Kulkul, but 9 code lines in every python script in addition are a lot.
But thx!


#6

@Hannes What do you mean? Could you elaborate more.


#7

Please take a good look at the lines 5 and 12 in my code and then compare with your own.
@Kulkul, nice recursion there :slight_smile:


#8

Thx T_Pover…now i got it:)


#9

@T_Pover I usually prefer “import DSCore as DS” so I can keep track of what I am using in case there are functions with the same name in different libraries.
@Kulkul. That’s really nice. I wish I had more time for learning code. Now I tend to copy code from other places.
I found this link: http://code.activestate.com/recipes/577470-fast-flatten-with-depth-control-and-oversight-over/ -
some flatten functions for python with depth control.


#10

Im not shure which method is the faster, or needs more resources. But one line is better for the clarity than 9.


#11

@ Hannes - use List Comprehensions - they are much faster than loops:

import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
dataEnteringNode = IN[0]
OUT = [i for sublist in IN[0] for i in sublist]

https://docs.python.org/2/tutorial/datastructures.html


Python Nodes Basics
#12

@T_Pover
Could you tell me why do you know we have to import DSCore into Python Script? Where do I find it or know it ?
@Kulkul
Thanks for your example. It’s useful to learn about definition in Python


#13

I don’t think it’s documented anywhere, a lot of the stuff you learn just by other people’s posts, as is the case with knowing when to import DScore :slight_smile:


#14

It’s briefly mentioned in the Primer also:
http://dynamoprimer.com/en/12_Best-Practice/12-3_Scripting-Reference.html


#15

thanks @Yna_Db exactly what i am looking for.


#16

I know this is an old post but I just bumped into it as I was searching for a question.
I found your method pretty nice and changed it a bit so it runs without error in case there are for instance also numbers in the list.


#17

Hi @tina.osia

Glad you liked it :wink: