pythonmeshlabpymeshlab

How to select an array of vertices in pymeshlab?


I want to remove some vertices stored in a NumPy array from a mesh. How can I select these vertices in pymeshlab based on vertex index or coordinates? Thanks!

import pymeshlab
from scipy.spatial import KDTree

def remove_background(subdir, ms):
    # load joint points
    joint_arr = load_joint_points(subdir)

    # get a reference to the current mesh
    m = ms.current_mesh()

    # get numpy arrays of vertices of the current mesh
    vertex_array_matrix = m.vertex_matrix()
    print(f'total # of vertices: {m.vertex_number()}')

    # create a KD tree of the joint points
    tree = KDTree(joint_arr)

    selected_vertices = []

    for vertex in vertex_array_matrix:
        # if the closest joint pt is farther than 500mm from the vertex, add the vertex to list
        dd, ii = tree.query(vertex, k=1)
        if(dd > 500):
            selected_vertices.append(vertex)

    print(f"delete {len(selected_vertices)} vertices")
    
    #how to select 'selected vertices' in pymeshlab?

    ms.delete_selected_vertices()

Solution

  • yes! You can conditionally select vertices using indexes using the conditional selection filter

    import pymeshlab as ml
    ms = ml.MeshSet()
    # just create a simple mesh for example
    ms.sphere(subdiv=0)
    # select the vertex with index 0
    ms.conditional_vertex_selection(condselect="vi==0") 
    # delete selected stuff
    ms.delete_selected_vertices() 
    

    Even better you can do all the background removal inside pymeshlab. If you have two meshes/pointclouds A B and you want remove from B all the vertices that are close to A less than a given threshold you can just load both meshes, use the Hausdorff Distance filter that will store into the "quality" for each vertex of A the distance from the closest vertex of B; then you can just use the conditional selection filter again this time by testing against quality.