vtkClipVolume usage

hi,

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()
 reader.SetFileName(file_name)
 reader.Update()

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

# clip
clip = vtk.vtkClipVolume()
clip.SetInputData(ugrid)
clip.SetValue(100)  # cut at scalar value = 100
clip.InsideOutOn()
clip.Update()

# write
write = vtk.vtkXMLUnstructuredGridWriter()
writer.SetInputData(clip.GetOutput())
writer.SetFileName("file.vtu")
writer.Write()

is there anything wrong? any sugestion?
Thanks

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.SetInputData(ugrid)
clip.ThresholdByLower(value)  # cut at scalar value = 100
# Or you might want: clip.ThresholdByUpper(value)  # cut at scalar value = 100
clip.Update()

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
extracted.save('my-data.vtk')

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