Marching cubes created a scalar and set it active, which overwrote the color set by segmentation.
We’ll change the segmentation display in to ignore any scalar values that may be present in the input mesh. For now, you have to remove the scalar values from the mesh if you want to see correct color:
import SampleData
masterVolumeNode = SampleData.SampleDataLogic().downloadCTChest()
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes()
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
marchingCubes = vtk.vtkMarchingCubes()
marchingCubes.SetInputData(masterVolumeNode.GetImageData())
marchingCubes.SetValue(0, 1000)
marchingCubes.Update()
ijk2rasmat = vtk.vtkMatrix4x4()
masterVolumeNode.GetIJKToRASMatrix(ijk2rasmat)
transform = vtk.vtkTransform()
transform.SetMatrix(ijk2rasmat)
applyTransform = vtk.vtkTransformPolyDataFilter()
applyTransform.SetTransform(transform)
applyTransform.SetInputData(marchingCubes.GetOutput())
applyTransform.Update()
surface = applyTransform.GetOutput()
# Remove point arrays to make sure they don't affect display color
while surface.GetPointData().GetNumberOfArrays()>0:
surface.GetPointData().RemoveArray(0)
segmentationNode.AddSegmentFromClosedSurfaceRepresentation(surface, "segment", [0.0,1.0,0.0])