如何使用Python生成球体、圆柱体、椭球等几何形状的STL文件?
Python生成自定义参数几何体STL实现方案
直接用numpy + numpy-stl库即可完成需求,无需手动处理点云转网格的复杂逻辑,对入门用户友好,适配Python 3.8版本。
第一步:安装依赖库
打开系统终端执行如下命令安装依赖:
pip install numpy numpy-stl
第二步:各几何体实现代码
所有代码可直接在Spyder中运行,修改对应自定义参数即可生成符合需求的STL文件。
1. 自定义高度、半径圆柱体
import numpy as np from stl import mesh # 自定义参数,可按需修改 cylinder_r = 10 # 圆柱体半径 cylinder_h = 20 # 圆柱体高度 circle_slices = 64 # 圆周切片数量,数值越高模型越光滑,文件越大 # 生成圆周坐标点 theta = np.linspace(0, 2*np.pi, circle_slices, endpoint=False) x = cylinder_r * np.cos(theta) y = cylinder_r * np.sin(theta) # 上下底面的点 bottom_points = np.column_stack([x, y, np.zeros(circle_slices)]) top_points = np.column_stack([x, y, np.full(circle_slices, cylinder_h)]) all_points = np.vstack([bottom_points, top_points, [0,0,0], [0,0,cylinder_h]]) # 加入上下底面中心点 # 生成三角面 faces = [] # 侧面三角面 for i in range(circle_slices): next_i = (i+1) % circle_slices faces.append([i, next_i, i + circle_slices]) faces.append([next_i, next_i + circle_slices, i + circle_slices]) # 下底面三角面 for i in range(circle_slices): next_i = (i+1) % circle_slices faces.append([i, next_i, 2*circle_slices]) # 上底面三角面 for i in range(circle_slices): next_i = (i+1) % circle_slices faces.append([i + circle_slices, next_i + circle_slices, 2*circle_slices + 1]) # 生成STL网格 cylinder_mesh = mesh.Mesh(np.zeros(len(faces), dtype=mesh.Mesh.dtype)) for i, f in enumerate(faces): for j in range(3): cylinder_mesh.vectors[i][j] = all_points[f[j]] # 保存STL文件 cylinder_mesh.save('cylinder.stl')
2. 自定义半径球体
import numpy as np from stl import mesh # 自定义参数,可按需修改 sphere_r = 15 # 球体半径 lat_slices = 32 # 纬度切片数 lon_slices = 64 # 经度切片数 # 球坐标生成点 phi = np.linspace(0, np.pi, lat_slices) theta = np.linspace(0, 2*np.pi, lon_slices, endpoint=False) phi, theta = np.meshgrid(phi, theta) x = sphere_r * np.sin(phi) * np.cos(theta) y = sphere_r * np.sin(phi) * np.sin(theta) z = sphere_r * np.cos(phi) # 转为一维点数组 all_points = np.column_stack([x.ravel(), y.ravel(), z.ravel()]) # 生成三角面 faces = [] for i in range(lon_slices): for j in range(lat_slices - 1): current = i * lat_slices + j next_lon = ((i+1) % lon_slices) * lat_slices + j faces.append([current, next_lon, current + 1]) faces.append([next_lon, next_lon + 1, current + 1]) # 生成STL网格 sphere_mesh = mesh.Mesh(np.zeros(len(faces), dtype=mesh.Mesh.dtype)) for i, f in enumerate(faces): for j in range(3): sphere_mesh.vectors[i][j] = all_points[f[j]] # 保存STL文件 sphere_mesh.save('sphere.stl')
3. 自定义半轴线椭球
import numpy as np from stl import mesh # 自定义参数,可按需修改 xr = 5 # x轴半轴长度 yr = 10 # y轴半轴长度 zr = 15 # z轴半轴长度 lat_slices = 32 # 纬度切片数 lon_slices = 64 # 经度切片数 # 球坐标生成椭球点 phi = np.linspace(0, np.pi, lat_slices) theta = np.linspace(0, 2*np.pi, lon_slices, endpoint=False) phi, theta = np.meshgrid(phi, theta) x = xr * np.sin(phi) * np.cos(theta) y = yr * np.sin(phi) * np.sin(theta) z = zr * np.cos(phi) # 转为一维点数组 all_points = np.column_stack([x.ravel(), y.ravel(), z.ravel()]) # 生成三角面 faces = [] for i in range(lon_slices): for j in range(lat_slices - 1): current = i * lat_slices + j next_lon = ((i+1) % lon_slices) * lat_slices + j faces.append([current, next_lon, current + 1]) faces.append([next_lon, next_lon + 1, current + 1]) # 生成STL网格 ellipsoid_mesh = mesh.Mesh(np.zeros(len(faces), dtype=mesh.Mesh.dtype)) for i, f in enumerate(faces): for j in range(3): ellipsoid_mesh.vectors[i][j] = all_points[f[j]] # 保存STL文件 ellipsoid_mesh.save('ellipsoid.stl')
注意事项
- 所有代码生成的都是标准ASCII格式STL,支持所有主流3D建模、切片软件直接读取
- 切片数量参数不要设置过大,超过100会导致STL文件体积大幅提升,普通场景下32/64的精度足够使用
- 保存文件的路径可自行修改,比如
save('D:/test/cylinder.stl')就会存到指定的D盘目录下
内容的提问来源于stack exchange,提问作者User646972
相关产品推荐
相关产品推荐

