Mesh multiple objects

With latest “curve drawing on surface” feature (to be announced next week, but already available in latest Slicer Preview Release) you can extract one side of a surface very easily:

  • Draw a closed curve on the surface
  • Make the curve snap to surface (enable Markups module / Curve settings / Curve type → shortest distance on surface, model node → model node that you would like to cut)
  • Copy-paste this code snippet to the Python console
# Get input data
curveNode = getNode('C')
modelNode = getNode('Segment_1')
extractLargestPart = False

# Get VTK data objects from MRML nodes
curvePoints = curveNode.GetCurvePointsWorld()
meshToClip = modelNode.GetMesh()  # we should apply transform to World

# Clip model with curve
loop = vtk.vtkSelectPolyData()
loop.SetLoop(curvePoints)
loop.GenerateSelectionScalarsOn()
loop.SetInputData(meshToClip)
loop.SetSelectionModeToLargestRegion()
clip = vtk.vtkClipPolyData()
clip.InsideOutOn()
clip.SetInputConnection(loop.GetOutputPort())
clip.GenerateClippedOutputOn()
clip.Update()
clippedOutput = clip.GetOutput() if extractLargestPart else clip.GetClippedOutput()

# Remove unused points
cleaner = vtk.vtkCleanPolyData()
cleaner.SetInputData(clippedOutput)
cleaner.Update()
cleanedClippedOutput = cleaner.GetOutput()

# Save result to new node
clippedModel = slicer.modules.models.logic().AddModel(cleanedClippedOutput)
clippedModel.GetDisplayNode().SetActiveScalarName("")
clippedModel.GetDisplayNode().BackfaceCullingOff()

There are of course many alternative methods to edit and combine meshes - using VTK filters and/or using numpy.

3 Likes