pythonpyvista

How to pick point with out key press in pyvista


I am using pyvista to visualize stl file. To get point information, i used below code to achieve this

import pyvista as pv

p = pv.Plotter()

mesh = pv.read(filename)

p.add_mesh(mesh) 

def callback(x, y) :return

p.enable_point_picking(callback=callback) 

p.show()

I could pick point by keeping cursor on point and press key 'P'.

I want this function without key press by clicking on mouse. is it possible?


Solution

  • I got one solution for this action. as pyvista is base from vtk, i used vtk function and it is working fine. if anyone found better solution, please post.

    mouse=pv._vtk.vtkInteractorStyleTrackballCamera()
    mouse.SetDefaultRenderer(plotter.ren_win.GetRenderers().GetFirstRenderer())
    plotter.SetInteractorStyle(mouse)
    
    def clicked(m,event):
        try:
            modifiers = QApplication.keyboardModifiers()
            if modifiers==QtCore.Qt.ControlModifier:pass
            else:
                clickPos = m.GetInteractor().GetEventPosition()
                picker = pv._vtk.vtkPointPicker()
                picker.Pick(clickPos[0], clickPos[1],0, m.GetDefaultRenderer())
                actor=picker.GetActor()
                if actor!=None:print(actor)
                m.OnLeftButtonDown()
        except Exception as e:print(e)
    mouse.AddObserver("LeftButtonPressEvent", clicked)
    

    Thanks biomode. i edited my code as per your suggestion. it looks better

    def clicked(event):
        picker = pv._vtk.vtkPropPicker()
        picker.PickProp(event[0], event[1],plotter.ren_win.GetRenderers().GetFirstRenderer())
        actor=picker.GetActor()
        if actor!=None:print(actor)
    plotter.track_click_position(callback=clicked,side='left', viewport=True)