我怎样才能用python挤出一个stl

2024-03-10

我正在尝试转换png图像转 3Dstls。我终于找到了一种方法来做到这一点。但有一个问题。现在导出的图像没有 z 值,也没有厚度。我这样做的方式是针对图像上的白色像素,将三角形绘制到表面上。

def define_faces(numpy_array, column_number, row_number, z_value):
    print("Vertices Initializing.")
    vertices = np.zeros((row_number , column_number , 3))
    for x in range(0, column_number):
        for y in range(0, row_number):
            z = z_value
            vertices[y][x] = (x,y,z)
    print("Vertices Initialized")
    faces = []
    print("Initializing Faces.")
    for x in range(0, column_number - 1):
        for y in range(0, row_number - 1):
            if numpy_array[y][x] >= PIXEL_COLOR_FILTER:
                vertice1 = vertices[y][x]
                vertice2 = vertices[y+1][x]
                vertice3 = vertices[y+1][x+1]
                face1 = np.array([vertice1, vertice2, vertice3])

                vertice1 = vertices[y][x]
                vertice2 = vertices[y][x+1]
                vertice3 = vertices[y+1][x+1]
                face2 = np.array([vertice1,vertice2,vertice3])
                faces.append(face1)
                faces.append(face2)
    print("Faces Initialized")
    return np.array(faces) , faces

and

def create_mesh(faces_numpy, faces, output_name):
    print("Creating Mesh.")
    surface = mesh.Mesh(np.zeros(faces_numpy.shape[0], dtype = mesh.Mesh.dtype))
    for i ,f in enumerate(faces):
        for j in range(3):
            surface.vectors[i][j] = faces_numpy[i][j]
            
    surface.save(output_name)
    print("Mesh created succesfully.")

这些是这段代码的核心功能。代码最初是为了Lithophane一代。我有一个想法,而不是给出三角形并将它们变成stl,直接在预期像素上生成立方体。所以它变成了3d。但在此之前关于如何挤出最终的任何想法stl?

这是PNG:

这是STL:


您可以使用开源库MeshLib https://github.com/MeshInspector/MeshLib#python-integration使用 python 绑定来解决您的任务。

那么挤压的代码如下:

import meshlib.mrmeshpy as mr
# load image as Distance Map object:
dm = mr.loadDistanceMapFromImage(mr.Path("your-image.png"), 0)
# find boundary contour of the letter:
polyline2 = mr.distanceMapTo2DIsoPolyline(dm, isoValue=127)
# triangulate the contour
mesh = mr.triangulateContours(polyline2.contours2())
# extrude itself:
mr.addBaseToPlanarMesh(mesh, zOffset=30)
# export the result:
mr.saveMesh(mesh, mr.Path("output-mesh.stl"))

The result will look as enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我怎样才能用python挤出一个stl 的相关文章

随机推荐