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 get mesh vertices? #93

Closed odgiv closed 5 years ago

odgiv commented 5 years ago

Hello, I am quite new in this area. I have a .stl file. I want to get vertices of this object. The vertices should be in shape of (Mx3). How to do that?

biga123 commented 5 years ago

Hi @odgiv , a .stl-file is basically a list of triangles. your_mesh.vectors gives you all triangles defined by their vertices. You can also access the vertices by using: your_mesh.v0,your_mesh.v1and your_mesh.v2 Note that for example your_mesh.v0[0],your_mesh.v1[0],your_mesh.v2[0] are the vertices of the first triangle (position 0) .

If you only want all vertices of your mesh in one array you could maybe do this:

import numpy as np
from stl import mesh

# Using an existing stl file:
your_mesh = mesh.Mesh.from_file('some_file.stl')

# Creating an array with all vertices of all triangles:
allvertices = np.array([your_mesh.v0,your_mesh.v1,your_mesh.v2])

# Numpy-Concatenate to connect the three arrays with each other 
allvertices = np.concatenate(allvertices)

# Removing duplicate vectors
allvertices = np.unique(allvertices,axis=0)
print(allvertices)

maybe this can also help you: https://pythonhosted.org/numpy-stl/usage.html#quickstart

I hope i could help

odgiv commented 5 years ago

@biga123 thank you.

wolph commented 5 years ago

I guess the question has been answered. If you need more help feel free to request a reopen or create a new issue.