Set to outline mode when a segmentation node is added, NOT WORKING

I used the follow code in .slicerrc.py to modify the default behavior of slicer 2D slice (up to 5.6.x) to display in outline mode instead of in filled mode for a segmentation.

def SetOutline(caller,event):
  for node in slicer.util.getNodes("*").values():
    if node.IsA("vtkMRMLSetmentationDisplayNode"):
      node.SetAllSegmentsVisibility2DOutline(1)
      node.SetAllSegmentsVisibility2DFill(0)

slicer.mrmlScene.AddObserver(slicer.mrmlScene.NodeAddedEvent, SetOutline)

However, I noticed that it always failed to switch to outline mode when drag-n-drop a segmentation file into the client area after a scalar volume file. However, it may switch to outline mode if I first drop a segmentation and then a volume file.

What is the root cause of such behavior? And what is the solution to this issue? A workaround is also highly appreciated!

Thx in advance!

First of all, there is a typo in this, ‘t’ => ‘g’

2 Likes

I would recommend to change this in the default segmentation display node. You can find examples in the script repository how to change default nodes in the scene.

1 Like

The code posted here was wrong. But this is not the root cause as I mentioned in the text, it works when you first drop a seg instead of a vol.

this is not working. in 4.13, there is no any hints. however, in 5.6, it nicely gives a hint: [VTK] vtkMRMLSegmentationDisplayNode::GetSegmentIDs: No segmentation node is associated to this display node

it looks like that segmentation display node has dependency issues on data: it can not be correctly constructed/configured w/o a segmentation node.

Correct. A display node controls the display properties of a data node, so without a data node it has no reason to exist. Usually you can make sure a display node exists after creating/accessing the data node by calling CreateDefaultDisplayNodes() on the data node, and then you can get it by calling GetDisplayNode().