为什么要在 Dynamo 的可视化编程环境中使用文本编程?可视化编程有许多优势。它使您无需在直观的可视化界面中学习特殊语法即可创建程序。但是,可视化程序可能会变得混乱,有时可能会在功能上有所降低。例如,Python 提供了更多可实现的方法来编写条件语句 (if/then) 和循环。Python 是一款功能强大的工具,可扩展 Dynamo 的功能,并允许您将许多节点替换为几行简明的代码。
# Load the Python Standard and DesignScript Librariesimport sysimport clrclr.AddReference('ProtoGeometry')from Autodesk.DesignScript.Geometry import*# The inputs to this node will be stored as a list in the IN variables.#The solid module to be arrayedsolid = IN[0]#A Number that determines which rotation pattern to useseed = IN[1]#The number of solids to array in the X and Y axesxCount = IN[2]yCount = IN[3]#Create an empty list for the arrayed solidssolids = []# Place your code below this line# Assign your output to the OUT variable.OUT = solids
在 Dynamo 中查看 Python 节点。请注意,我们使用的语法与在 Dynamo 中节点标题中看到的语法相同。查看下面注释的代码。
# Load the Python Standard and DesignScript Librariesimport sysimport clrclr.AddReference('ProtoGeometry')from Autodesk.DesignScript.Geometry import*# The inputs to this node will be stored as a list in the IN variables.#The solid module to be arrayedsolid = IN[0]#A Number that determines which rotation pattern to useseed = IN[1]#The number of solids to array in the X and Y axesxCount = IN[2]yCount = IN[3]#Create an empty list for the arrayed solidssolids = []#Create an empty list for the edge curvescrvs = []# Place your code below this line#Loop through edges an append corresponding curve geometry to the listfor edge in solid.Edges: crvs.append(edge.CurveGeometry)#Get the bounding box of the curvesbbox = BoundingBox.ByGeometry(crvs)#Get the x and y translation distance based on the bounding boxyDist = bbox.MaxPoint.Y-bbox.MinPoint.YxDist = bbox.MaxPoint.X-bbox.MinPoint.X# Assign your output to the OUT variable.OUT = solids
由于我们将平移并旋转实体模块,因此我们使用 Geometry.Transform 操作。通过查看 Geometry.Transform 节点,我们知道需要源坐标系和目标坐标系来变换实体。源是实体的上下文坐标系,而目标是每个阵列模块的不同坐标系。这意味着我们需要遍历 X 和 Y 值,以每次变换不同的坐标系。
# Load the Python Standard and DesignScript Librariesimport sysimport clrclr.AddReference('ProtoGeometry')from Autodesk.DesignScript.Geometry import*# The inputs to this node will be stored as a list in the IN variables.#The solid module to be arrayedsolid = IN[0]#A Number that determines which rotation pattern to useseed = IN[1]#The number of solids to array in the X and Y axesxCount = IN[2]yCount = IN[3]#Create an empty list for the arrayed solidssolids = []#Create an empty list for the edge curvescrvs = []# Place your code below this line#Loop through edges an append corresponding curve geometry to the listfor edge in solid.Edges: crvs.append(edge.CurveGeometry)#Get the bounding box of the curvesbbox = BoundingBox.ByGeometry(crvs)#Get the x and y translation distance based on the bounding boxyDist = bbox.MaxPoint.Y-bbox.MinPoint.YxDist = bbox.MaxPoint.X-bbox.MinPoint.X#Get the source coordinate systemfromCoord = solid.ContextCoordinateSystem#Loop through x and yfor i inrange(xCount):for j inrange(yCount):#Rotate and translate the coordinate system toCoord = fromCoord.Rotate(solid.ContextCoordinateSystem.Origin, Vector.ByCoordinates(0,0,1), (90*(i+j%seed))) vec = Vector.ByCoordinates((xDist*i),(yDist*j),0) toCoord = toCoord.Translate(vec)#Transform the solid from the source coord syste, to the target coord system and append to the list solids.append(solid.Transform(fromCoord,toCoord))# Assign your output to the OUT variable.OUT = solids