3MFConsortium / lib3mf

lib3mf is an implementation of the 3D Manufacturing Format file standard
http://3mf.io
BSD 2-Clause "Simplified" License
231 stars 93 forks source link

How to Export Mesh Objects to STL Using Python #393

Open glonor opened 1 week ago

glonor commented 1 week ago

Hello,

I am using the Python bindings (version: 2.3.2) and trying to extract individual MeshObject instances from a 3MF file. The 3MF file contains multiple mesh objects within a single build, and my goal is to export each one of them into separate STL files.

Code Example:

from lib3mf import get_wrapper

file_path = "example.3mf"

wrapper = get_wrapper()
model = wrapper.CreateModel()

reader = model.QueryReader("3mf")
reader.ReadFromFile(file_path)

object_iterator = model.GetObjects()

while object_iterator.MoveNext():
    mesh_object = object_iterator.GetCurrentObject()
    if mesh_object.IsMeshObject():
        print(f"Mesh Object: {mesh_object.GetResourceID()}")
        # mesh_object.export()  # <--- Hypothetical function

Currently, I am manually iterating over each mesh’s vertices and faces using the GetVertex and GetTriangle methods. I was wondering if there is a more direct approach in lib3mf to export an individual MeshObject as an STL file.

Thank you!

vijaiaeroastro commented 6 days ago

@glonor The current architecture of lib3mf only allows calling writer on a model and not on individual mesh objects or components.

glonor commented 4 days ago

Thank you for the clarification! It would be a very useful feature to have the ability to export individual MeshObject instances directly, without needing any workarounds. I hope this is something planned for future updates, as it would greatly simplify the process.