vtkClipVolume usage


Im trying to perform a clip on an unstructuredGrid over an scalars value, but cannot get the clipped object (it gives empty) or probably don’t know how to use it.

My data doesn’t comes with the scalars but has a lot of array values. What i’m doing is:

 # read data
 reader = vtk.vtkXMLUnstructuredGridReader()

 # get the unstrucutred grid and set the scalar
 ugrid = reader.GetOutput()
 scalars = ugrid.GetPointData().GetArray(1)  # choose this array in particular

# clip
clip = vtk.vtkClipVolume()
clip.SetValue(100)  # cut at scalar value = 100

# write
write = vtk.vtkXMLUnstructuredGridWriter()

is there anything wrong? any sugestion?

You may want to apply a threshold filter instead of a clipping filter. The clipping filter is usually used to split a dataset around certain bounds whereas a threshold filter will remove cells from a dataset specified by a value on one of its data arrays. To do this, replace the clip section with the following:

# clip
clip = vtk.vtkThreshold()
clip.ThresholdByLower(value)  # cut at scalar value = 100
# Or you might want: clip.ThresholdByUpper(value)  # cut at scalar value = 100

FYI: if you are working in Python frequently, you should check out vtki. It is a Python package that interfaces to VTK to make the code a bit more intuitive/Pythonic. Your workflow in vtki would be:

import vtki

# Load data
ugrid = vtki.read(file_name)

# set the scalar
ugrid.set_active_scalar('Array Name')

# threshold
extracted = ugrid.threshold(100)

# write

cool, i’m going to try this option, and the vtki package sems way more usefull for python code, thanks!

anyway, to close my original question, i found that the code works with vtkClipDataSet instead of vtkClipVolume (this last one seems to work only with images and the first one with unstructured grid), replacing this solved the problem!.

Thanks all.

1 Like