最近使用了numpy-stl,现在对numpy-stl进行深入总结学习。 写这个文档的时候有些地方的理解还是不到位,姑且看之。
numpy-stl中文文档
numpy-stl 是一个可以让你简单快捷地处理STL(和普通的3D对象)文件的简单库。
由于 numpy-stl 的所有操作都是重度依赖于 numpy 的,所以它是Python可使用的最快的用于STL编辑的库之一。
0 3D基础和相关概念
0.1 顶点、多边形、网格
顶点(vertex):一个点在空间中的位置,描述具有x、y、z坐标的空间位置。顶点就是在一个三维坐标系中的点。
多边形 :3个以上的顶点连接形成多边形。在3DMAX中,常见的概念是三角形面(triangle),也就是3个顶点形成的三角形区域。
网格(mesh) :提供3D模型的底层结构。通常,网格看起来是由成千上万个单独的多边形框架结构组成的线框。3D模型通常至少包含一个网格,也可能包含多个网格。
3D图形是由3D网格构成的,网格也被称为模型。
0.2 材质、纹理、光源
光有网格模型看上去就是一个物品的轮廓,为了让模型具有真实感,我们就要为模型赋予材质和纹理。
纹理(texture):物体表面绘制的图案。
材质(materal):物体表面各可视属性的集合,可以看出物体是由什么材料组成的。这些可视属性是指表面的色彩、纹理、光滑度、透明度、反射率、折射率、发光度等。
光源(light):材质的表现必然依赖光源,光源不同,表现也会不同,没有光源就看不到物体,也就没有材质。
0.3 向量、矩阵
向量(vector):我们说一个模型就是一个对象。对象都有其位置和方向,在3D空间由x、y、z三个值来定义,在这里位置和方向都是向量。
矩阵(matrix):对象有了方向和位置后,要对其进行移动、缩放、旋转等处理就需要用到矩阵。
0.4 相机、透视、视口、投影
相机(camera):相当于在3D场景中的人的眼睛。
透视:相机的一种显示效果,比如物体远小近大。
视口:视觉窗口,由浏览器窗口或canvans决定。
投影(projection):3D坐标系列到2D渲染坐标的映射,影子。
0.5 着色器(shader)
承担着将顶点、变换、材质、光源、相机等相互作用生成最终图像的任务。
1 相关链接
- 源码: https://github.com/WoLpH/numpy-stl
- 项目页面: https://pypi.python.org/pypi/numpy-stl
- bug报告: https://github.com/WoLpH/numpy-stl/issues
- 文档: http://numpy-stl.readthedocs.org/en/latest/
- 作者博客: https://wol.ph/
2 依赖
- numpy 任意最近版本
- python-utils 1.6及以上版本
3 安装
pip install numpy-stl
4 初始使用
在安装完这个模块后,我们可以像使用pip命令一样,使用下面的这些命令。
$ stl2bin your_ascii_stl_file.stl new_binary_stl_file.stl
$ stl2ascii your_binary_stl_file.stl new_ascii_stl_file.stl
$ stl your_ascii_stl_file.stl new_binary_stl_file.stl
5 贡献
作者欢迎大家积极多做贡献,根据指引开始贡献。
6 快速开始
import numpy
from stl import mesh
# 读取一个本地已经存在的stl文件
your_mesh = mesh.Mesh.from_file('some_file.stl')
# 或者创建一个新的 mesh (确保引入的‘mesh’不被自定义的mesh重写覆盖):
VERTICE_COUNT = 100
data = numpy.zeros(VERTICE_COUNT, dtype=mesh.Mesh.dtype)
your_mesh = mesh.Mesh(data, remove_empty_areas=False)
# The mesh normals (自动计算)
your_mesh.normals
# mesh向量
your_mesh.v0, your_mesh.v1, your_mesh.v2
# Accessing individual points (concatenation of v0, v1 and v2 in triplets)
assert (your_mesh.points[0][0:3] == your_mesh.v0[0]).all()
assert (your_mesh.points[0][3:6] == your_mesh.v1[0]).all()
assert (your_mesh.points[0][6:9] == your_mesh.v2[0]).all()
assert (your_mesh.points[1][0:3] == your_mesh.v0[1]).all()
# 保存为stl文件
your_mesh.save('new_stl_file.stl')
7 使用 matplotlib 绘制同样简单
使用matplotlib辅助显示3D模型和我们对其的处理结果。如下图,加载了一个俺私人珍藏的一个狗头。
from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
# 创建一个plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# 加载stl文件并将向量加载到 plot
your_mesh = mesh.Mesh.from_file('dog.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
# 自动缩放网格尺寸
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
# 在屏幕上显示这个plot
pyplot.show()
8 修改网格对象
from stl import mesh
import math
import numpy
# Create 3 faces of a cube
data = numpy.zeros(6, dtype=mesh.Mesh.dtype)
# Top of the cube
data['vectors'][0] = numpy.array([[0, 1, 1],
[1, 0, 1],
[0, 0, 1]])
data['vectors'][1] = numpy.array([[1, 0, 1],
[0, 1, 1],
[1, 1, 1]])
# Front face
data['vectors'][2] = numpy.array([[1, 0, 0],
[1, 0, 1],
[1, 1, 0]])
data['vectors'][3] = numpy.array([[1, 1, 1],
[1, 0, 1],
[1, 1, 0]])
# Left face
data['vectors'][4] = numpy.array([[0, 0, 0],
[1, 0, 0],
[1, 0, 1]])
data['vectors'][5] = numpy.array([[0, 0, 0],
[0, 0, 1],
[1, 0, 1]])
# Since the cube faces are from 0 to 1 we can move it to the middle by
# substracting .5
data['vectors'] -= .5
# Generate 4 different meshes so we can rotate them later
meshes = [mesh.Mesh(data.copy()) for _ in range(4)]
# Rotate 90 degrees over the Y axis
meshes[0].rotate([0.0, 0.5, 0.0], math.radians(90))
# Translate 2 points over the X axis
meshes[1].x += 2
# Rotate 90 degrees over the X axis
meshes[2].rotate([0.5, 0.0, 0.0], math.radians(90))
# Translate 2 points over the X and Y points
meshes[2].x += 2
meshes[2].y += 2
# Rotate 90 degrees over the X and Y axis
meshes[3].rotate([0.5, 0.0, 0.0], math.radians(90))
meshes[3].rotate([0.0, 0.5, 0.0], math.radians(90))
# Translate 2 points over the Y axis
meshes[3].y += 2
# Optionally render the rotated cube faces
from matplotlib import pyplot
from mpl_toolkits import mplot3d
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Render the cube faces
for m in meshes:
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(m.vectors))
# Auto scale to the mesh size
scale = numpy.concatenate([m.points for m in meshes]).flatten()
axes.auto_scale_xyz(scale, scale, scale)
# Show the plot to the screen
pyplot.show()
9 继承扩展Mesh对象
from stl import mesh
import math
import numpy
# 创建立方体的3个面
data = numpy.zeros(6, dtype=mesh.Mesh.dtype)
# 立方体顶部
data['vectors'][0] = numpy.array([[0, 1, 1],
[1, 0, 1],
[0, 0, 1]])
data['vectors'][1] = numpy.array([[1, 0, 1],
[0, 1, 1],
[1, 1, 1]])
# 前面
data['vectors'][2] = numpy.array([[1, 0, 0],
[1, 0, 1],
[1, 1, 0]])
data['vectors'][3] = numpy.array([[1, 1, 1],
[1, 0, 1],
[1, 1, 0]])
# 左面
data['vectors'][4] = numpy.array([[0, 0, 0],
[1, 0, 0],
[1, 0, 1]])
data['vectors'][5] = numpy.array([[0, 0, 0],
[0, 0, 1],
[1, 0, 1]])
# Since the cube faces are from 0 to 1 we can move it to the middle by
# substracting .5
data['vectors'] -= .5
cube_back = mesh.Mesh(data.copy())
cube_front = mesh.Mesh(data.copy())
# Rotate 90 degrees over the X axis followed by the Y axis followed by the
# X axis
cube_back.rotate([0.5, 0.0, 0.0], math.radians(90))
cube_back.rotate([0.0, 0.5, 0.0], math.radians(90))
cube_back.rotate([0.5, 0.0, 0.0], math.radians(90))
cube = mesh.Mesh(numpy.concatenate([
cube_back.data.copy(),
cube_front.data.copy(),
]))
# Optionally render the rotated cube faces
from matplotlib import pyplot
from mpl_toolkits import mplot3d
# 创建一个 plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# 提供立方体
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(cube.vectors))
# 自动缩放尺寸
scale = cube_back.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
# 显示
pyplot.show()
10 用一个顶点和面的集合列表创建Mesh对象
import numpy as np
from stl import mesh
# 定义立方体的8个顶点
vertices = np.array([\
[-1, -1, -1],
[+1, -1, -1],
[+1, +1, -1],
[-1, +1, -1],
[-1, -1, +1],
[+1, -1, +1],
[+1, +1, +1],
[-1, +1, +1]])
# 定义一个由12个三角面构成的立方体
faces = np.array([\
[0,3,1],
[1,3,2],
[0,4,7],
[0,7,3],
[4,5,6],
[4,6,7],
[5,1,2],
[5,2,6],
[2,3,6],
[3,7,6],
[0,1,5],
[0,5,4]])
# 创建mesh
cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
cube.vectors[i][j] = vertices[f[j],:]
# 将mesh 写入文件"cube.stl"
cube.save('cube.stl')
11 获取mesh对象相关参数(体积、重心、惯量)
import numpy as np
from stl import mesh
# Using an existing closed stl file:
your_mesh = mesh.Mesh.from_file('some_file.stl')
volume, cog, inertia = your_mesh.get_mass_properties()
print("体积 = {0}".format(volume))
print("重心 (COG) = {0}".format(cog))
print("Inertia matrix at expressed at the COG = {0}".format(inertia[0,:]))
print(" {0}".format(inertia[1,:]))
print(" {0}".format(inertia[2,:]))
12 组合多个stl文件
import math
import stl
from stl import mesh
import numpy
# find the max dimensions, so we can know the bounding box, getting the height,
# width, length (because these are the step size)...
def find_mins_maxs(obj):
minx = obj.x.min()
maxx = obj.x.max()
miny = obj.y.min()
maxy = obj.y.max()
minz = obj.z.min()
maxz = obj.z.max()
return minx, maxx, miny, maxy, minz, maxz
def translate(_solid, step, padding, multiplier, axis):
if 'x' == axis:
items = 0, 3, 6
elif 'y' == axis:
items = 1, 4, 7
elif 'z' == axis:
items = 2, 5, 8
else:
raise RuntimeError('Unknown axis %r, expected x, y or z' % axis)
# _solid.points.shape == [:, ((x, y, z), (x, y, z), (x, y, z))]
_solid.points[:, items] += (step * multiplier) + (padding * multiplier)
def copy_obj(obj, dims, num_rows, num_cols, num_layers):
w, l, h = dims
copies = []
for layer in range(num_layers):
for row in range(num_rows):
for col in range(num_cols):
# skip the position where original being copied is
if row == 0 and col == 0 and layer == 0:
continue
_copy = mesh.Mesh(obj.data.copy())
# pad the space between objects by 10% of the dimension being
# translated
if col != 0:
translate(_copy, w, w / 10., col, 'x')
if row != 0:
translate(_copy, l, l / 10., row, 'y')
if layer != 0:
translate(_copy, h, h / 10., layer, 'z')
copies.append(_copy)
return copies
# Using an existing stl file:
main_body = mesh.Mesh.from_file('ball_and_socket_simplified_-_main_body.stl')
# rotate along Y
main_body.rotate([0.0, 0.5, 0.0], math.radians(90))
minx, maxx, miny, maxy, minz, maxz = find_mins_maxs(main_body)
w1 = maxx - minx
l1 = maxy - miny
h1 = maxz - minz
copies = copy_obj(main_body, (w1, l1, h1), 2, 2, 1)
# I wanted to add another related STL to the final STL
twist_lock = mesh.Mesh.from_file('ball_and_socket_simplified_-_twist_lock.stl')
minx, maxx, miny, maxy, minz, maxz = find_mins_maxs(twist_lock)
w2 = maxx - minx
l2 = maxy - miny
h2 = maxz - minz
translate(twist_lock, w1, w1 / 10., 3, 'x')
copies2 = copy_obj(twist_lock, (w2, l2, h2), 2, 2, 1)
combined = mesh.Mesh(numpy.concatenate([main_body.data, twist_lock.data] +
[copy.data for copy in copies] +
[copy.data for copy in copies2]))
combined.save('combined.stl', mode=stl.Mode.ASCII) # save as ASCII
13 已知限制
- 启用加速后,STL名会自动转为小写。