Hi, I used the segment editor in python interactor for a list of segmentations. However, the codes work for a while and raise Error: ‘NoneType’ object has no attribute ‘setParameter.’
Below is my code:
dir_roi = '/my/segment/folder'
names = os.listdir(dir_roi)
for name in names:
path_roi = os.path.join(dir_roi, name)
path_img = path_roi.replace('roi', 'video_nii')
path_out = path_roi.replace('roi', 'roi_smooth')
volumn = slicer.util.loadVolume(path_img)
seg = slicer.util.loadSegmentation(path_roi)
seg.CreateClosedSurfaceRepresentation()
slicer.util.mainWindow().moduleSelector().selectModule('SegmentEditor')
segmentEditorWidget = (
slicer.modules.segmenteditor.widgetRepresentation().self().editor
)
segmentEditorWidget.setActiveEffectByName("Smoothing")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("SmoothingMethod", "MORPHOLOGICAL_CLOSING")
effect.setParameter("KernelSizeMm", 12)
effect.self().onApply()
effect = segmentEditorWidget.activeEffect()
effect.setParameter("SmoothingMethod", "MORPHOLOGICAL_OPENING")
effect.setParameter("KernelSizeMm", 12)
effect.self().onApply()
effect = segmentEditorWidget.activeEffect()
effect.setParameter("SmoothingMethod", "JOINT_TAUBIN")
effect.setParameter("JointTaubinSmoothingFactor", 1)
effect.self().onApply()
masterVolumeNode = slicer.mrmlScene.GetFirstNodeByClass(
'vtkMRMLScalarVolumeNode'
)
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(
seg, labelmapVolumeNode, masterVolumeNode
)
slicer.util.saveNode(labelmapVolumeNode, path_out)
slicer.mrmlScene.Clear(0)
Sometimes “setParameter” works, but most of the time, it fails.
I would appreciate any help. Thanks!