Centering on a segment from a script

Using a custom extension is certainly a solution, but in the long term I would recommend developers to use Slicer core functions if available.

Slicer-4.10 returning a swig pointer (such as _000001bfd4897340_p_void) is a minor inconvenience. You can still access the values using the helper function below (in the nightly version the VTK hint is there so the position is directly returned as a Python tuple).

def getListFromPointer(bufferPtr, scalarType=vtk.VTK_DOUBLE, numberOfElements=3):
  """Convert swig pointer (_..._p_void) to list object"""
  bufferArray = vtk.vtkAbstractArray.CreateArray(vtk.VTK_DOUBLE)
  bufferArray.SetVoidArray(bufferPtr, numberOfElements, True)
  buffer = [bufferArray.GetValue(i) for i in range(numberOfElements)]
  return buffer

Example usage:

>>> ptr=getNode('Segmentation').GetSegmentCenter("Segment_1")
>>> ptr
'_000001bfd4897340_p_void'
>>> getListFromPointer(ptr)
[8.254768371581932, -18.07139587402301, -10.214302062987997]
2 Likes