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
624 stars 105 forks source link

How to save stl file with color #150

Closed ClarkDinh closed 3 years ago

ClarkDinh commented 3 years ago

Dear Rick van Hattem, I would like to ask you how can I save the stl file with color. I have vertices and faces. I save the mesh in the same way as your description: ---Create the mesh cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype)) for i, f in enumerate(faces): for j in range(3): cube.vectors[i][j] = vertices[f[j],:] --- Write the mesh to file "cube.stl" cube.save('cube.stl')

However, it just saves the grey stl file as the following attachment.

stl without color

How can I save the stl with color as produced by plot_trisurf.

matplotlib

Thank you very much for your support.

wolph commented 3 years ago

The stl format has no support for color at all. It only consists of a list of triangles and a list of combinations of those triangles to build the structures.

The plot appears to be basing the color on the z height.

ClarkDinh commented 3 years ago

Thanks for your feedback.

eudoxos commented 3 years ago

The stl format has no support for color at all. It only consists of a list of triangles and a list of combinations of those triangles to build the structures.

Binary STL does have limited (RGB) color support, though it is not standardized.

wolph commented 3 years ago

That's an interesting new addition. If there's enough interest I can add it to the library but it looks like it would break most STL software

eudoxos commented 3 years ago

it would break most STL software

No, it uses the 2 attr bytes which are normally unused anyway — you are already reading and writing them, too :

https://github.com/WoLpH/numpy-stl/blob/6f2b6f831f3e372878fb2d19bbfe6c029a2001aa/stl/base.py#L175

wolph commented 3 years ago

I read this and assumed it could be problematic:

in the standard format, this should be zero because most software does not understand anything else.

But you're right, many libraries probably won't care about it.