Help needed with vtkSelectionNode and Query

Hi,

I’m working on a Python plugin for ParaView.
I’m using the ResampleWithDataset filter and i need to deal with invalid nodes, where the field vtkValidPointMask is set to 0.
I’m trying to use vtkSelectionNode to extract these nodes, but I can’t get it to work.

Here is a simple test case:

import numpy as np
import vtk
import vtk.numpy_interface.dataset_adapter as dsa

# ------------------------------------------
# *** This part  create a simple dataset *** 

# Create a simple sphere
sphereSource = vtk.vtkSphereSource()
sphereSource.SetPhiResolution(48)
sphereSource.SetThetaResolution(32)

sphereSource.Update()
obj_sphere = sphereSource.GetOutput()

# Get points coordinate
Coords = dsa.vtkDataArrayToVTKArray(obj_sphere.GetPoints().GetData())

# A a test field named vtkValidPointMask
aval = np.arctan2(Coords[:, 0], Coords[:, 1])
tval = (2+np.cos(8*np.pi*Coords[:, 2]) - np.sin(3*aval))/4
obj_sphere.GetPointData().AddArray( dsa.numpyTovtkDataArray(tval, "vtkValidPointMask"))

## Write as vtp file
vf = vtk.vtkXMLPolyDataWriter()
vf.SetFileName("shere.vtp")
vf.SetInputDataObject(obj_sphere)
vf.Write()


## ---------------------------------------------
## *** Here start the test *** 

## lecture des fichiers
#vf = vtk.vtkXMLPolyDataReader()
#vf.SetFileName("sphere.vtp")
#vf.Update()
#obj_sphere = vf.GetOutput()

selectionNode = vtk.vtkSelectionNode()
selectionNode.SetFieldType(vtk.vtkSelectionNode.VALUES)
selectionNode.SetContentType(vtk.vtkSelectionNode.QUERY)
selectionNode.SetQueryString("vtkValidPointMask >= 0.5")

selection = vtk.vtkSelection()
selection.AddNode(selectionNode)

extractSelection = vtk.vtkExtractSelection()
extractSelection.SetInputData(0, obj_sphere)
extractSelection.SetInputData(1, selection)
extractSelection.Update()

obj_extract = extractSelection.GetOutput()

print('Sphere  : {} points'.format(obj_sphere.GetNumberOfPoints()))
print('Extract : {} points'.format(obj_extract.GetNumberOfPoints()))

Have you got any hint to make this work?

Thanks a lot!
Best regards,

Loic

I recommend using the following code instead, in your ParaView Python Shell

from paraview.selection import *
s1 = SelectThresholds(Thresholds=[.5,1], ArrayName=‘vtkValidPointMask’, Source=GetActiveSource())
a = ExtractSelection(Input=GetActiveSource(), Selection=s1)
Show(a)

QUERY type selections are not directly supported by vtkExtractSelection. You have to use vtkPVExtractSelection which internally uses vtkPythonSelector to support these queries. You can use @jfavre suggestion. If you want a pure VTK code, you may want to use other filters like vtkThreadhold filter or its ilk.