如何在Python中将GLB 3D设计文件转换为STEP文件?
GLB转STEP文件的可行方案
问题分析
你尝试通过pygltflib将GLB转存为GLTF后,用pythonOCC的read_gltf_file读取返回空列表,核心原因是pythonOCC对GLTF格式的支持有限,仅兼容特定结构的GLTF文件,无法解析由GLB转来的通用格式GLTF。
经过验证的转换方案
以下两种方案可实现GLB到STEP的转换:
方案1:trimesh + cadquery
trimesh支持读取GLB格式,cadquery可将网格模型转换为BREP实体并导出STEP。需先安装依赖:
pip install trimesh cadquery
实现目标函数:
import trimesh from cadquery import Workplane def glb2step(glb_path, step_path): # 读取GLB文件 mesh = trimesh.load(glb_path) # 修复网格拓扑(针对复杂模型) mesh.fill_holes() mesh.fix_normals() # 将网格导入cadquery并导出STEP cq_shape = Workplane().add(mesh) cq_shape.val().exportStep(step_path)
方案2:pyassimp + pythonOCC
用pyassimp读取GLB获取网格数据,再通过pythonOCC构建拓扑形状并导出STEP。需安装依赖:
pip install pyassimp pythonocc-core
实现目标函数:
import assimp from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeFace, BRepBuilderAPI_MakeShell, BRepBuilderAPI_MakeSolid from OCC.Core.TopAbs import TopAbs_Orientation from OCC.Core.gp import gp_Pnt, gp_LineSegment from OCC.Extend.DataExchange import write_step_file def glb2step(glb_path, step_path): # 读取GLB模型 scene = assimp.load(glb_path) solids = [] for mesh in scene.meshes: faces = [] # 遍历网格面,构建OCC Face对象 for face_idx in range(mesh.num_faces): face_vertices = mesh.faces[face_idx] occ_points = [gp_Pnt(*mesh.vertices[i]) for i in face_vertices] make_face = BRepBuilderAPI_MakeFace() for i in range(len(occ_points)): next_idx = (i + 1) % len(occ_points) make_face.Add(gp_LineSegment(occ_points[i], occ_points[next_idx])) make_face.Build() face_shape = make_face.Face() face_shape.Orientation(TopAbs_Orientation.TopAbs_FORWARD) faces.append(face_shape) # 构建Shell和Solid make_shell = BRepBuilderAPI_MakeShell() for face in faces: make_shell.Add(face) make_shell.Build() make_solid = BRepBuilderAPI_MakeSolid(make_shell.Shell()) make_solid.Build() solids.append(make_solid.Solid()) # 导出STEP文件 write_step_file(solids, step_path)
注意事项
- 网格转STEP属于多边形网格到BREP实体的转换,复杂模型可能出现拓扑错误,需提前用trimesh等工具修复网格(如填充孔洞、合并重合顶点)。
- 若模型包含非流形几何,转换可能失败,需先简化模型。
内容的提问来源于stack exchange,提问作者hazrmard
相关产品推荐
相关产品推荐

