Use the Flatten node in python script

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.

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:

7 Likes

Hi @Hannes,@viktor_kuzev, @T_Pover

Here is another possible way without Design Script :slight_smile:

4 Likes

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

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

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

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:

1 Like

Thx T_Pover…now i got it:)

1 Like

@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.

2 Likes

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

@ 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

1 Like

@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

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:

3 Likes

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

2 Likes

thanks @Yna_Db exactly what i am looking for.

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.

x

2 Likes

Hi @tina.osia

Glad you liked it :wink:

2 Likes