3D码农建模 - 正多面体

在三维形状中,正多面体指各面都是全等的正多边形且每一个顶点所接的面数都是一样的凸多面体,符合这种特性的立体总共有5种: 正四面体, 正六面体, 正八面体, 正十二面体, 和正二十面体。

以下Python代码给出了在Blender 4中实现这些正多面体的方法,其中正六面体和正二十面体使用Blender基本形状实现,其它的则计算各个顶点的坐标然后连点成面以构造三维形状。

正四面体, 每面都是正三角形

Python 复制代码
def tetrahedron():
  edge_length = 4

  a = edge_length / (2.0 * math.sqrt(2.0))
  verts = [
      ( a,  a,  a),
      ( a, -a, -a),
      (-a,  a, -a),
      (-a, -a,  a),
  ]

  faces = [[0,1,2],[0,2,3],[0,3,1],[1,3,2]]

  mesh = bpy.data.meshes.new('mesh_tetrahedron')
  obj  = bpy.data.objects.new('tetrahedron', mesh)
  bpy.context.collection.objects.link(obj)

  mesh.from_pydata(verts, [], faces)
  mesh.update(calc_edges=True)

正六面体, 每面都是正四边形

Python 复制代码
def cube():
  ledge_length = 4

  bpy.ops.mesh.primitive_cube_add(
    size = ledge_length
  )

正八面体, 每面都是正三角形

Python 复制代码
def octahedron():
  ledge_length = 4
  a = ledge_length/sqrt(2)

  verts = [
    (a, 0, 0),
    (-a, 0, 0),
    (0, a, 0),
    (0, -a, 0),
    (0, 0, a),
    (0, 0, -a)
  ]

  faces = [[4,0,2],[4,2,1],[4,1,3],[4,3,0],[5,2,0],[5,1,2],[5,3,1],[5,0,3]]
  
  mesh = bpy.data.meshes.new('mesh_octahedron')
  obj  = bpy.data.objects.new('octahedron', mesh)
  bpy.context.collection.objects.link(obj)

  mesh.from_pydata(verts, [], faces)
  mesh.update(calc_edges=True)

正十二面体, 每面都是正五边形

Python 复制代码
def dodecahedron():
  ledge_length = 4

  golden_ratio = (1 + sqrt(5)) / 2

  s = ledge_length * golden_ratio / 2
  t = s / golden_ratio
  u = s * golden_ratio

  verts = [(s,s,s),(s,s,-s),(s,-s,s),(s,-s,-s),(-s,s,s),(-s,s,-s),(-s,-s,s),(-s,-s,-s),
             (t,u,0),(-t,u,0),(t,-u,0),(-t,-u,0),(u,0,t),(u,0,-t),(-u,0,t),(-u,0,-t),(0,t,u),
             (0,-t,u),(0,t,-u),(0,-t,-u)]

  faces = [[0,8,9,4,16],[0,12,13,1,8],[0,16,17,2,12],[8,1,18,5,9],[12,2,10,3,13],
            [16,4,14,6,17],[9,5,15,14,4],[6,11,10,2,17],[3,19,18,1,13],[7,15,5,18,19],
            [7,11,6,14,15],[7,19,3,10,11]]

  mesh = bpy.data.meshes.new('mesh_dodecahedron')
  obj  = bpy.data.objects.new('dodecahedron', mesh)
  bpy.context.collection.objects.link(obj)

  mesh.from_pydata(verts, [], faces)
  mesh.update(calc_edges=True)

正二十面体, 每面都是正三角形

Python 复制代码
def icosahedron():
  edge_length = 4

  bpy.ops.mesh.primitive_ico_sphere_add(
    radius = (edge_length / 4) * sqrt(10 + 2 * sqrt(5)),
    subdivisions = 1
  )
相关推荐
AI视觉网奇1 分钟前
pycharm F2 修改文件名 修改快捷键
ide·python·pycharm
酷爱码3 分钟前
Java -jar命令运行外部依赖JAR包的深度场景分析与实践指南
java·python·jar
WilliamCHW5 分钟前
Pycharm 配置解释器
ide·python·pycharm
abments13 分钟前
基于ReAction范式的问答系统实现demo
开发语言·python
belong_to_you1 小时前
python模块——tqdm
python
L_cl1 小时前
【Python 算法零基础 4.排序 ⑪ 十大排序算法总结】
python·算法·排序算法
Vertira1 小时前
Pytorch安装后 如何快速查看经典的网络模型.py文件(例如Alexnet,VGG)(已解决)
人工智能·pytorch·python
老歌老听老掉牙2 小时前
使用 SymPy 进行向量和矩阵的高级操作
python·线性代数·算法·矩阵·sympy
DX_dove2 小时前
pytorch3d+pytorch1.10+MinkowskiEngine安装
人工智能·pytorch·python
且慢.5892 小时前
Python——day46通道注意力(SE注意力)
python·深度学习·机器学习