Probably the easiest is to describe this using a short Python code snippet. You can generate the code snippet with any AI assistant, you don’t need to know programming or much about Slicer.
For example, I got a working code with giving the following prompts to the free Microsoft Copilot:
write a Python code snippet that converts a markup fiducial list to a segmentation so that at each markup control point position a small sphere shape segment is added
It almost gave correct result, but it hallucinated a non-existent function, so I tried to make correct itself just by copying the error message that I saw in the Python console in Slicer.
I got the error: AttributeError: ‘vtkSlicerSegmentationsModuleLogicPython.vtkSlicerS’ object has no attribute ‘CreateLabelmapVolumeFromModel’
I got the error: AttributeError: module ‘slicer.util’ has no attribute ‘createLabelVolumeFromModel’
I got the error: AttributeError: ‘vtkSegmentationCorePython.vtkSegmentation’ object has no attribute ‘AddSegmentFromClosedSurfaceRepresentation’
I got the error: AttributeError: module ‘slicer.util’ has no attribute ‘updateSegmentFromModelPolyData’
It still did not find the correct converter function, so at this point I decided that I will point it to the Slicer script repository.
Use examples on this page: Script repository — 3D Slicer documentation
It still did not get it right, so I pinpointed the relevant section.
Use the code snippet in the section “Create segmentation from a model node”
This finally generated working code! It was not perfect in that it created an empty segment for each point, so I asked it to correct itself.
It created an extra empty segment for each point. Please update the code so that it does not generate those extra empty segments.
Now this generated code that worked perfectly:
import slicer
import vtk
# Create a segmentation node
segmentation_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentation = segmentation_node.GetSegmentation()
# Get the markup fiducial node
markup_node = slicer.util.getNode('F')
n_control_points = markup_node.GetNumberOfFiducials()
# Define sphere parameters
sphere_radius = 5.0 # Adjust the radius as needed
sphere_resolution = 50
# Create a model node for the sphere
sphere_model_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode")
sphere_model_node.SetName("SphereModel")
for i in range(n_control_points):
# Get the control point position
position = [0.0, 0.0, 0.0]
markup_node.GetNthFiducialPosition(i, position)
# Create the sphere
sphere = vtk.vtkSphereSource()
sphere.SetCenter(position)
sphere.SetRadius(sphere_radius)
sphere.SetThetaResolution(sphere_resolution)
sphere.SetPhiResolution(sphere_resolution)
sphere.Update()
# Update the model node with the sphere polydata
sphere_model_node.SetAndObservePolyData(sphere.GetOutput())
# Add model to segmentation as a new segment
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(sphere_model_node, segmentation_node)
# Remove temporary model node
slicer.mrmlScene.RemoveNode(sphere_model_node)
# Ensure the segmentation displays correctly
segmentation_node.CreateClosedSurfaceRepresentation()
While having to copy-paste error messages was slightly annoying and ultimately I gave some hints to the chatbot, the whole thing did not take more than 5 minutes and the generated code works and the code quality is not bad at all. And the nice thing is that if you want to do something more or different then you can just keep asking the chatbot to further modify the code.