2

is there a way how to load a stl files from zip by numpy-stl?

I can read stl files normaly from file system by numpy-stl but Im not able to connect it to opened zip file.

It is python 2.7

For example:

this is code for reading scene.xml from example.zip file, in memory

from zipfile import ZipFile

opened_zipfile = ZipFile("example.zip", 'r')
tree = ET.fromstring(opened_zipfile.read('scene.xml'))
...

And this is how I can read cube.stl file from file system

from stl.mesh import Mesh

mesh = Mesh.from_file("cube.stl")
...

Stl files are in binary or ascii format.

But I dont know how to read .stl files from zip without decompress files to file system and reading from it.

Thanks for help.

Tibor
  • 81
  • 1
  • 7

1 Answers1

0

Firstly, this isn't exactly an answer to your question and I am sorry for that in advance. But a much better way is to use the open3d package to 3D visualize stl files. It's completely intractable with generated triangle_mesh.

Here's how you can do it:

  • Installation
pip install open3d
  • Usage
from zipfile import ZipFile
import open3d as o3d

# specifying the zip file name
file_name = "Body_Kylo_Ren_fixed.zip"
  
# opening the zip file in READ mode
with ZipFile(file_name, 'r') as zip:
    # printing all the contents of the zip file
    zip.printdir()
    # extracting all the files
    zip.extractall()
    
    mesh = o3d.io.read_triangle_mesh("Body_Kylo_Ren_fixed.stl")
    mesh = mesh.compute_vertex_normals()
    o3d.visualization.draw_geometries([mesh],window_name="STL",
                                    left=1000, top=200,
                                    width=800, height=650) 

Output: A completely interactable stl object Output 3D stl

Check out the open3d-documentation to know more

Musabbir Arrafi
  • 744
  • 4
  • 18