Author Topic: Numpy (or vectorized) access to Model's attributes  (Read 1746 times)


  • Newbie
  • *
  • Posts: 15
    • View Profile
Numpy (or vectorized) access to Model's attributes
« on: March 05, 2022, 02:22:27 PM »

I am using Metashape in python as part of a bigger python project.

I would like to access to the Models attributes (vertices, faces, texture coordinates)  in numpy ndarrays.

For the moment, I loop over the vertices/faces/texture_coords like so:

Code: [Select]
# Vertices
verts = []
for v in model.vertices:
    c = v.coord
    verts.append([c.x, c.y, c.z])
verts = np.array(verts)

# Faces
tris = []
for f in model.faces:
    tris .append(f.vertices)
tris = np.array(tris )

# Texture coordinates
uvs= []
for tv in model.tex_vertices:
uvs= np.array(uvs)

But it is not optimized at all, so I would like to know if there is a way to access this data in faster way ?

As subsidiary question, when aligning the chunks for example :
Code: [Select]
doc.alignChunks(reference=0, method=2, fit_scale=True)the vertices of the Model are not affected by the alignment.
In fact, I have to apply the transforms manually, vertex by vertex :

Code: [Select]
for chunk in doc.chunks:
    model = chunk.models[0]
    matrix = chunk.transform.matrix
    for i, v in enumerate(model.vertices):
        model.vertices[i].coord = matrix.mulp(v.coord)

Is there a better solution for this too ?


« Last Edit: March 05, 2022, 02:24:34 PM by glennk »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14971
    • View Profile
Re: Numpy (or vectorized) access to Model's attributes
« Reply #1 on: March 21, 2022, 05:30:51 PM »
Hello Glenn,

In the " align model to model" script example you can see, how the external model is read from PLY format using numpy module:
Best regards,
Alexey Pasumansky,
Agisoft LLC


  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Numpy (or vectorized) access to Model's attributes
« Reply #2 on: March 29, 2022, 01:09:06 PM »
Hello Alexey, thank you for your answer.

I am afraid this is not exactly related to my question which is more Python-oriented. In fact, even if I did not test your solution, I imagine that it will not be any faster since the model is temporarily stored on disk.

I lowered the vertex density of my models and my solution now takes 0.5 seconds for the transfer Metashape -> Numpy (Trimesh). It is more acceptable in my case than the 15 seconds I was experiencing with bigger models.

It is common for 3D libraries with Python bindings to have a Numpy compatible interface (e.g. Trimesh, Open3D). I guess Metashape's Python bindings are designed for easy script automation, rather than as a Python 3D library.
Therefore, I see why there is no need to have such compatibility in Metashape.

Best regards,