Annotation Node comes up as Model Node

Hi all,

I was trying to make some code to pick the last model node which was created in the scene. This is what I tried:

noOfModelNodes = slicer.mrmlScene.GetNumberOfNodesByClass('vtkMRMLModelNode')
lastModel = slicer.mrmlScene.GetNthNodeByClass(noOfModelNodes - 1, 'vtkMRMLModelNode')

this code works fine for any other node type but for models it has errors. I decided to try and investigate. So I opened up a blank slicer instance and typed in the first line of code above. The noOfModelNodes is equal to 3 which is fine as there seem to be some model nodes present from startup in the node list (seen in the data module).

I then placed a ruler in the scene and ran the first line of code again and noticed that now noOfModelNodes = 4. Why is the ruler node coming up as a vtkMRMLModelNode?

To double check I put in the code:

model = slicer.mrmlScene.GetNthNodeByClass(3, 'vtkMRMLModelNode')
model.GetClassName()

which gave me ‘vtkMRMLAnnotationRulerNode’. My question is why does slicer think that the ruler node is a model node? ROI nodes seem to be included as model nodes as well. I have tried this on 4.10.2 and the latest nightly build and the behavior is the same.

Also if anyone has any better suggestions of how to get the most recent model node in the scene then I would be keen to hear

Hi -

An annotation is a special type of model (see this diagram). You can just iterate until you get to a node that is a model node (check the class name).

Thanks a lot for the clarification Steve. This was confusing me. I have updated my code and the following worked for me:

# get most recent model
noOfModelNodes = slicer.mrmlScene.GetNumberOfNodesByClass('vtkMRMLModelNode')
for i in range(0, noOfModelNodes):
  currentModel = slicer.mrmlScene.GetNthNodeByClass(i, 'vtkMRMLModelNode')
  if currentModel.GetClassName() == 'vtkMRMLModelNode':
    mostRecentModel = currentModel