前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >三维软件与python运用系列--Dynamo节点的应用

三维软件与python运用系列--Dynamo节点的应用

作者头像
用户6692829
发布2024-03-02 17:07:49
2430
发布2024-03-02 17:07:49
Python 是一种广泛使用的编程语言,其流行程度与其语法风格有很大关系。它的可读性很高,因此比许多其他语言更易于学习。
Python 是一种广泛使用的编程语言,其流行程度与其语法风格有很大关系。它的可读性很高,因此比许多其他语言更易于学习。

Python 是一款功能强大的工具,可扩展 Dynamo 的功能,并允许您将许多节点替换为几行简明的代码。

和blender的脚本开发类似,python可引用强大的数学库、几何库完成可视化编程,高效地输出二三维的数据。

创建实体模块

第一步:创建底面

Rectangle.ByWidthLength:创建一个矩形,它将作为实体的基础。 Surface.ByPatch:将矩形连接到“closedCurve”输入以创建底部曲面。
Rectangle.ByWidthLength:创建一个矩形,它将作为实体的基础。 Surface.ByPatch:将矩形连接到“closedCurve”输入以创建底部曲面。

第二步:根据偏移多段线填充曲面

Geometry.Translate:将矩形连接到“geometry”输入以向上移动它,从而使用代码块指定实体的基础厚度。 Polygon.Points:查询平移的矩形以提取角点。 Geometry.Translate:使用代码块创建与四个点对应的一列四个值,从而向上平移实体的一个角。 Polygon.ByPoints:使用平移的点来重建顶部多边形。 Surface.ByPatch:连接多边形以创建顶部曲面。
Geometry.Translate:将矩形连接到“geometry”输入以向上移动它,从而使用代码块指定实体的基础厚度。 Polygon.Points:查询平移的矩形以提取角点。 Geometry.Translate:使用代码块创建与四个点对应的一列四个值,从而向上平移实体的一个角。 Polygon.ByPoints:使用平移的点来重建顶部多边形。 Surface.ByPatch:连接多边形以创建顶部曲面。

第三步:两轮廓放样创建实体

两个轮廓之间放样来创建实体的侧面

List.Create:将底部矩形和顶部多边形连接到索引输入。 Surface.ByLoft:放样两个轮廓以创建实体的侧面。 List.Create:将顶面、侧面和底面连接到索引输入以创建曲面列表。 Solid.ByJoinedSurfaces:连接曲面以创建实体模块。
List.Create:将底部矩形和顶部多边形连接到索引输入。 Surface.ByLoft:放样两个轮廓以创建实体的侧面。 List.Create:将顶面、侧面和底面连接到索引输入以创建曲面列表。 Solid.ByJoinedSurfaces:连接曲面以创建实体模块。

创建脚本模块

新建脚本

定义输入和输出

要向节点添加其他输入,请关闭编辑器,然后单击节点上的“+”图标。输入命名为 IN[0]、IN[1] 等,以指示它们表示列表中的各项目。
要向节点添加其他输入,请关闭编辑器,然后单击节点上的“+”图标。输入命名为 IN[0]、IN[1] 等,以指示它们表示列表中的各项目。

添加业务实现代码

由于我们将平移并旋转实体模块,因此我们使用 Geometry.Transform 操作。通过查看 Geometry.Transform 节点,我们知道需要源坐标系和目标坐标系来变换实体。源是实体的上下文坐标系,而目标是每个阵列模块的不同坐标系。这意味着我们需要遍历 X 和 Y 值,以每次变换不同的坐标系。

代码语言:python
代码运行次数:0
复制
# Enable Python support and load DesignScript library
import clr
clr.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 arrayed
solid = IN[0]
#A number that determines which rotation pattern to use
seed = IN[1]
#The number of solids to array in the X and Y axes
xCount = IN[2]
yCount = IN[3]

#Create an empty list for the arrayed solids
solids = []
# Create an empty list for the edge curves
crvs = []

# Place your code below this line
#Loop through edges and append corresponding curve geometry to the list
for edge in solid.Edges:
	crvs.append(edge.CurveGeometry)
#Get the bounding box of the curves
bbox = BoundingBox.ByGeometry(crvs)

#Get the X and Y translation distance based on the bounding box
yDist = bbox.MaxPoint.Y-bbox.MinPoint.Y
xDist = bbox.MaxPoint.X-bbox.MinPoint.X
#get the source coordinate system
fromCoord = solid.ContextCoordinateSystem

#Loop through X and Y
for i in range(xCount):
	for j in range(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 system 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

连接输入输出节点

单击 Python 节点上的“运行”将允许代码执行。

输出内容:

参考文档:

Python 节点 | Dynamo Primer (dynamobim.org)

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系外文翻译前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建实体模块
    • 第一步:创建底面
      • 第二步:根据偏移多段线填充曲面
        • 第三步:两轮廓放样创建实体
        • 创建脚本模块
          • 新建脚本
            • 定义输入和输出
              • 添加业务实现代码
                • 连接输入输出节点
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档