為什麼要在 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