在三维形状中,正多面体指各面都是全等的正多边形且每一个顶点所接的面数都是一样的凸多面体,符合这种特性的立体总共有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
)