I’m learning to translate a dynamo script and compact it into a single python node. I need to use the List.Transpose method… how to write it in python? and what references i need to add to be able to run the method?
I already tried using ironpython List but got error no transpose method
also tried import DSCore but cannot find any documentation about the class and methods of it
import clr
# import Revit Services
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# import Revit API
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("System")
from System.Collections.Generic import List
clr.AddReference('DSCoreNodes')
from DSCore import List as Dlist
# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
# Place your code below this line
if hasattr(IN[0], '__iter__'):
bbox = IN[0][0]
else:
bbox = IN[0]
minpoint = bbox.MinPoint
maxpoint = bbox.MaxPoint
combinep = [minpoint,maxpoint]
Plist = List [Point]()
for i in combinep: Plist.Add(i)
combinep.Transpose()
#Plist = List.Create(minpoint,maxpoint)
# Assign your output to the OUT variable.
OUT = bbox,
Thanks for the reply. Using the Python zip method for list manipulation can be a solution.
But can you explain the logic of the Dynamo List.Transpose method?
And how do I translate it into Python zip list and for loop to achieve the same result?
Python’s zip() function is defined as zip(*iterables). The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.
Thanks @jwroelofs for informing the zip(*iterables) method
Here is my code to emulate the result of the dynamo List.Translate node
But I still cannot figure out how to implement dynamo node input list level selection to affect the result
maybe @jacob.small can help with this problem
data = IN[0]
def maxlistdepth(list):
maxdepth = 0
for i in list:
newdepth = len(i)
if maxdepth < newdepth:
maxdepth = newdepth
return maxdepth
def addnull(list,maxdepth):
for i in list:
while len(i) < maxdepth:
i.append(None)
maxdlist = maxlistdepth(data)
addnull(data,maxdlist)
OUT = list(zip(*data))
Another solution is to import DSCore Builtin Dynamo Library to use the List.Transpose method
import sys
import clr
clr.AddReference('DSCoreNodes')
from DSCore import List as DList
data = IN[0]
transpose = DList.Transpose(data)
OUT = transpose
Thanks, @christian.stan for Design.Script solution. I learned from you that CodeBlock can be used to make functions only without any input or output and the function can be used in other CodeBlock
This is Bering a bit off topic, but there is no need for the transpose in your graph. You want the center, width, and length of a bounding box, so…
Typing off my phone, but hopefully this makes sense if it doesn’t just work with the default inputs.
bbList = IN[0]
results = []
for bb in bbList:
minPnt = bb.MinPoint
maxPnt = bb.MaxPoint
vect = Vector.ByTwoPoints(minPnt, maxPnt)
width = vect.X
length = vect.Y
height = vect.Z
cent = minPnt.Add(vect.Scale(0.5))
data = [cent, width, length, height]
results.append(data)
OUT = zip(*results) #returns the lists in order of centers, then widths, then lengths, then heights. If you want stuff groups by bounding box just use OUT = results