I am trying to write a ParaView Python custom plug-in filter that appends to data in the ParaView pipeline with data that is read in from a file. Since files can have multiple datasets, I am hoping to allow users to select which datasets to load in before applying the filter.
Currently, I am only able to expose the data array selector after hitting apply in ParaView. Is there a way to expose the data array selector upon selecting a file name (before hitting apply)? My current code is shown below. This filter is based on the reader example provided in https://github.com/Kitware/ParaView/blob/master/Examples/Plugins/PythonAlgorithm/PythonAlgorithmExamples.py
The python class for the filter is
@smproxy.filter(label="AppendFromFile")
@smproperty.input(name="Input")
class AppendFromCSV(VTKPythonAlgorithmBase):
def __init__(self):
VTKPythonAlgorithmBase.__init__(self,
nInputPorts=1,
nOutputPorts=1,
outputType="vtkUnstructuredGrid")
self._filename = None
self._ndata = None
self._arrayselection = vtkDataArraySelection()
self._arrayselection.AddObserver("ModifiedEvent",
createModifiedCallBack(self))
The file name is specified through the GUI using the following method. Note that the self._arrayselection.AddArray
is called here because I want the data array selector to be available upon filename selection.
@smproperty.stringvector(name="FileName")
@smdomain.filelist()
@smhint.filechooser(extensions="csv", file_description="CSV files")
def SetFileName(self, name):
if self._filename != name:
self._filename = name
# populate dataset names in data array selector.
if self._filename != 'None':
with open(self._filename, 'r') as f:
reader = csv.reader(f)
dataset_names = reader.next()
for dname in dataset_names:
self._arrayselection.AddArray(dname)
self._ndata = None
self.Modified()
The data array selector is exposed using
@smproperty.dataarrayselection(name="data values")
def GetDataArraySelection(self):
return self._arrayselection
Finally, the request data method is
def RequestData(self, request, inInfo, outInfo):
# get handle on input.
input0 = dsa.WrapDataObject(vtkUnstructuredGrid.GetData(inInfo[0]))
# get number of cells
num_cells = input0.GetNumberOfCells()
# read in data from file
raw_data = numpy.getfromtxt(self._filename, delimiter=',', names=True)
# get handle on output
output = dsa.WrapDataObject(vtkUnstructuredGrid.GetData(outInfo))
output.ShallowCopy(vtkUnstructuredGrid.GetData(inInfo[0]))
# Only add data that is selected in data array selector
for id, name in enumerate(raw_data.dtype.names):
if self._arrayselection.ArrayIsEnabled(name):
data = numpy.zeros([num_cells, ], dtype=float)
for cell in range(num_cells):
data[cell] = raw_data[cell][id]
output.CellData.append(data, name)
return 1