VTK clipping with Python

Hi all,

I am fairly new to VTK. I am using VTK version 8.1.2, Python 3.7.5 on Windows 10. I am trying to clip a VTK image with Python similar to the screenshot below taken from Slicer:

2020-05-20 16_41_11-Изпратени елементи - nikolay.kolev@icb.bg - Outlook

Below is the code that I manage to put together, however I have the feeling that I am missing something conceptual in VTK library because the code is hanging on line 23 (clipper.Update()) without any errors.

import vtk

renderer = vtk.vtkRenderer()
render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)
render_window_interaction = vtk.vtkRenderWindowInteractor()
render_window_interaction.SetRenderWindow(render_window)

reader = vtk.vtkStructuredPointsReader()
reader.SetFileName('.\\sample.vtk')
reader.Update()

plane = vtk.vtkPlane()
plane.SetOrigin(0.25, 0, 0)
plane.SetNormal(-1, -1, 0)

clipper = vtk.vtkClipVolume()
clipper.SetInputConnection(reader.GetOutputPort())
clipper.SetClipFunction(plane)
clipper.GenerateClipScalarsOn()
clipper.GenerateClippedOutputOn()
clipper.SetValue(0.5)
clipper.Update()

volume_mapper = vtk.vtkOpenGLGPUVolumeRayCastMapper()
volume_mapper.SetInputConnection(clipper.GetOutputPort())
volume_mapper.SetBlendModeToComposite()

volume_color = vtk.vtkColorTransferFunction()
volume_color.AddRGBPoint(0,    0.0, 0.0, 0.0)
volume_color.AddRGBPoint(500,  1.0, 0.5, 0.3)
volume_color.AddRGBPoint(1000, 1.0, 0.5, 0.3)
volume_color.AddRGBPoint(1150, 1.0, 1.0, 0.9)

volume_scalar_opacity = vtk.vtkPiecewiseFunction()
volume_scalar_opacity.AddPoint(0,    0.00)
volume_scalar_opacity.AddPoint(500,  0.15)
volume_scalar_opacity.AddPoint(1000, 0.15)
volume_scalar_opacity.AddPoint(1150, 0.85)

volume_gradient_opacity = vtk.vtkPiecewiseFunction()
volume_gradient_opacity.AddPoint(0,   0.0)
volume_gradient_opacity.AddPoint(90,  0.5)
volume_gradient_opacity.AddPoint(100, 1.0)

volume_property = vtk.vtkVolumeProperty()
volume_property.SetColor(volume_color)
volume_property.SetScalarOpacity(volume_scalar_opacity)
volume_property.SetGradientOpacity(volume_gradient_opacity)
volume_property.SetInterpolationTypeToLinear()
volume_property.ShadeOn()
volume_property.SetAmbient(0.4)
volume_property.SetDiffuse(0.6)
volume_property.SetSpecular(0.2)

volume = vtk.vtkVolume()
volume.SetMapper(volume_mapper)
volume.SetProperty(volume_property)

renderer.AddViewProp(volume)

camera =  renderer.GetActiveCamera()
center = volume.GetCenter()
camera.SetFocalPoint(center[0], center[1], center[2])
camera.SetPosition(center[0] + 400, center[1], center[2])
camera.SetViewUp(0, 0, -1)

render_window.SetSize(640, 480)

render_window_interaction.Initialize()
render_window.Render()
render_window_interaction.Start()

Looking forward for your help!

Remove vtkClipVolume from your pipeline. For volume rendering, the clipping is done by the mapper:

volume_mapper.AddClippingPlane(plane)
1 Like

Also note that you can run your code in Slicer’s Python environment and then you don’t need to reimplement any existing features just add what is new or specific to your project. You can use the full Slicer GUI, a plain Python console, or Slicer Jupyter kernel (which also provides interactive 3D and slice viewers inside the notebook).

1 Like

Thank you for your replay! The screenshot from my question is actually from Slicer GUI. I was not aware that Slicer had a Python console because as I said I am fairly new. Now I found it and I will play with it.

1 Like