def ReadStlToObj(stl_filename, obj_filename):
'''
将stl格式转化为obj
参数:
stl_filename:stl 文件路径
obj_filename:要保存的obj文件路径
'''
import numpy as np
from stl import mesh
# 读取 STL 文件
stl_mesh = mesh.Mesh.from_file(stl_filename)
# 创建 OBJ 文件
with open(obj_filename, 'w') as obj_file:
obj_file.write("# OBJ file\n")
# 写入顶点信息
for vertex in stl_mesh.vectors.reshape(-1, 3):
obj_file.write(f"v {vertex[0]} {vertex[1]} {vertex[2]}\n")
# 写入面信息
num_faces = len(stl_mesh.vectors)
for i in range(num_faces):
# 由于 STL 文件中的面是三角形的,写入 OBJ 文件时使用的是 1-based 索引
obj_file.write(f"f {i*3+1} {i*3+2} {i*3+3}\n")