Simple example to add a vtk object to Slicer 3D view

Operating system: Ubuntu 18.04
Slicer version: 4.10

This is not a question but a simple example for beginners. The sphere can be replaced by other polydata object.

 import vtk
 source = vtk.vtkSphereSource()
 source.SetRadius(50.0)
 source.SetCenter(0,0,0)
 source.Update() 
 pd = source.GetOutput() # a polydat aobject
 slicer.modules.models.logic().AddModel(pd)
1 Like

Thanks for taking the time to share this. We maintain a list of code snippets in the script repository. It would be great if you could add it there.

I also have a suggestion for possible simplification of the code, using the source’s output port:

import vtk
source = vtk.vtkSphereSource()
source.SetRadius(50.0)
source.SetCenter(0,0,0)
modelNode = slicer.modules.models.logic().AddModel(source.GetOutputPort())