Python Seg Fault

Hi,

I’m messaging because I believe I came across a bug. I’ve found a hacky fix for it, but I don’t think my fix is the correct long-term solution. Im using vtk==8.2.0 installed from Conda.

If you filter.GetOutput() from some filter and then try to delete a cell polydata.DeleteCell(idx) of the outputted polydata you get a seg fault. However, if you simply interact with the cells in some (any?) way and then perform the delete there is no error (e.g., by doing polydata.GetCell(idx) and then do polydata.DeleteCell(idx); note that I don’t think idx has to be the same between these, and .GetCell() only needs to be done once and then you can iterate over the entire mesh and delete whatever cells you want.

Basic/reproducible example code:
ERROR:

import vtk
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0.0, 0.0, 0.0)
sphereSource.SetRadius(5.0)
sphereSource.SetPhiResolution(100)
sphereSource.SetThetaResolution(100)
sphereSource.Update()

sphere = vtk.vtkPolyData()
sphere.DeepCopy(sphereSource.GetOutput())

sphere.DeleteCell(1)

RESOLUTION:

import vtk
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0.0, 0.0, 0.0)
sphereSource.SetRadius(5.0)
sphereSource.SetPhiResolution(100)
sphereSource.SetThetaResolution(100)
sphereSource.Update()

sphere = vtk.vtkPolyData()
sphere.DeepCopy(sphereSource.GetOutput())

sphere.GetCell(1)
sphere.DeleteCell(1)

Note that it doesn’t matter if I DeepCopy the output of the filter or not.

Clearly, the fix is extremely simple and I can easily include this in any of my code. However, without knowing this is the problem it could be a pain to sort out.

Let me know if any additional information is of use.

Thanks,

Anthony.

Certain methods require that BuildLinks() is called beforehand. See the vtkPolyData docs for more info.

sphere.EditableOn()
sphere.BuildLinks()
sphere.DeleteCell(1)

Edit: The polydata should be marked Editable, too, so I’ve added that.

Thanks, David.

Appreciate the prompt reply. Im assuming that by calling .GetCell() that .BuildLinks() is called as a part of it - hence why my hack works.

I was going to ask something about having .BuildLinks() run by default with .GetOutput(), but from reading through the docs more it seems like some functions require it isn’t called yet.

I guess something you just need to learn once along the path to know to look for it as something that is needed to be done.

BuildLinks() isn’t called automatically because it is expensive and is only required in certain situations.