wolph / numpy-stl

Simple library to make working with STL files (and 3D objects in general) fast and easy.
http://numpy-stl.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
605 stars 103 forks source link

Save multi-part stls #209

Closed lionel-wilhelm closed 10 months ago

lionel-wilhelm commented 1 year ago

Hello @wolph , First of all, thanks a lot for this great package. In the same way multi-part stls can be loaded one by one with the mesh.Mesh.from_multi_file method but I didn't find a way to save multiple meshes as named parts.

I used the following :

combined_mesh = mesh.Mesh(np.concatenate([m.data for m in stl_meshes])) # stl_meshes is a list of Mesh objects
combined_mesh.names = stl_mesh_names # a list of names
combined_mesh.save("combined.stl")

but when loading it back with :

meshes = mesh.Mesh.from_multi_file("./combined.stl")
for m in meshes:
    print(m.name)

I don't get my names list back.

Is this something which is supposed to be supported or not ? Thanks in advance

version :

pip show numpy-stl
Name: numpy-stl
Version: 3.0.1
Summary: Library to make reading, writing and modifying both binary and ascii STL files easy.
Home-page: https://github.com/WoLpH/numpy-stl/
Author: Rick van Hattem
Author-email: Wolph@Wol.ph
License: BSD
Location: /home/container_user/.local/lib/python3.8/site-packages
Requires: numpy, python-utils
Required-by: 
wolph commented 1 year ago

A "multi" STL file is just a bunch of ASCII (text) STL files concatenated so you can write them by appending multiple meshes to the same file descriptor.

Your version is combining the meshes together into a single file so the names are lost.

To write the meshes separately to the same file this should work:

with open('combined.stl', 'w') as combined_mesh_fh:
    for stl_mesh in stl_meshes:
        stl_mesh.save(fh=combined_mesh_fh)
lionel-wilhelm commented 1 year ago

Great ! Thanks a lot. I was afraid this would write a header multiple times but reading carefully the source code indicated that :

  1. there is no header for ASCII stl files
  2. multi-part stl is not supported in binary file format

Is this correct ? Thanks again for the super fast answer.

wolph commented 1 year ago

That's completely correct :)

I'm not sure which libraries support the multi STL format to be honest... I haven't seen it that much in the wild so far.