trimesh icon indicating copy to clipboard operation
trimesh copied to clipboard

Trimesh to .npy conversion

Open eyildiz-ugoe opened this issue 2 years ago • 1 comments

This would be a nice feature as a function. I know that Trimesh is made of trackedarrays but a function that directly returns the item as it is, without involving any array manipulation, would be desirable. We could then save it as .npy file and use it in further processes such as skeletonization.

P.S.: I am sorry if this exists as a feature already, I just could not find it in the docs.

eyildiz-ugoe avatar Aug 08 '22 10:08 eyildiz-ugoe

Hey, TrackedArray is a subclass of numpy.ndarray, you can use normal numpy functions on them:

In [1]: import trimesh
 
In [2]: m = trimesh.creation.icosphere()

In [3]: m
Out[3]: <trimesh.Trimesh(vertices.shape=(642, 3), faces.shape=(1280, 3))>

In [4]: m.vertices
Out[4]: 
TrackedArray([[-0.52573111,  0.85065081,  0.        ],
              [ 0.52573111,  0.85065081,  0.        ],
              [-0.52573111, -0.85065081,  0.        ],
              ...,
              [-0.91298249, -0.39960705,  0.08232358],
              [-0.9663926 , -0.13279248,  0.22011703],
              [-0.96386126, -0.2664047 ,  0.        ]])

In [7]: import numpy as np

In [8]: with open('vertices.npy', 'wb') as f:
   ...:     np.save(f, m.vertices)
   ...: 

# or you can view the array back into a vanilla ndarray
In [10]: m.vertices.view(np.ndarray)
Out[10]: 
array([[-0.52573111,  0.85065081,  0.        ],
       [ 0.52573111,  0.85065081,  0.        ],
       [-0.52573111, -0.85065081,  0.        ],
       ...,
       [-0.91298249, -0.39960705,  0.08232358],
       [-0.9663926 , -0.13279248,  0.22011703],
       [-0.96386126, -0.2664047 ,  0.        ]])

mikedh avatar Aug 08 '22 16:08 mikedh