【问题标题】:How can i extrude a stl with python我怎样才能用python挤出一个stl
【发布时间】:2022-09-29 01:29:03
【问题描述】:

我正在尝试将 png 图像转换为 3d stls。我终于找到了一种方法来做到这一点。但有一个问题。现在导出的图像没有 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

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:

  • 您需要两个 z 值。
  • 你好。我试图给出两个 z 值。我调用了 define_faces 函数两次并将它们的输出初始化为 4 个单独的变量。然后我尝试将这些输出与附加功能结合起来。但它引发了这个错误。 \"IndexError: index 20506 is out of bounds for axis 0 with size 20506\" 在 create_mesh 函数的第 6 行。这一行:“surface.vectors[i][j] = faces_numpy[i][j]\”。

标签: python image-processing


【解决方案1】:

您可以使用带有 python 绑定的开源库MeshLib 来解决您的任务。

在 Windows 上,该库安装在 python 3.10 中,使用

py -3.10 -m pip install --upgrade meshlib

那么挤压的代码如下:

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.value(), 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"))

结果将如下所示

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-16
    • 2021-03-04
    • 2022-08-11
    • 2019-09-01
    • 2014-04-26
    • 2022-11-21
    • 2021-11-12
    • 2016-11-25
    相关资源
    最近更新 更多