Annotation in 3D view (like slice view)?

Is there a way (in python is fine, doesn’t need to be a feature add) to add an annotation to the bottom left (or right, or top; any corner, really) of the 3D view? This post: Display text in a 3D view seems to talk about a Corner Annotation module, but it looks like that has been deprecated. Mostly looking for something like the label in the slice view:

Thanks!!!

You can probably find a way to make this work. I’ve never tried changing them but here’s the code where they are implemented. If you really only need this once you could just edit the code with the annotation you want and restart Slicer.

I do this all the time to better label screenshots. Here is the code I use which could be pasted into the logic of your own module or taken apart to use in the interactor. As it is, it adds a text label in the upper left and upper right of all slice views and the first 3D view, though it’s probably obvious how to customize it to do any simple variation on that.

def addCornerAnnotation(self, textToAdd, textColor=(0,1,1)):
    ''' Add annotation to upper right and upper left of slice views and 3D view'''
    lm = slicer.app.layoutManager()
    sliceViewNames = lm.sliceViewNames()
    for sliceName in sliceViewNames:
      view = lm.sliceWidget(sliceName).sliceView()
      view.cornerAnnotation().SetText(vtk.vtkCornerAnnotation.UpperRight,textToAdd)
      view.cornerAnnotation().SetText(vtk.vtkCornerAnnotation.UpperLeft,textToAdd)
      textProperty = view.cornerAnnotation().GetTextProperty()
      textProperty.SetColor(*textColor)
      view.forceRender()
    # Same for 3D view
    view=slicer.app.layoutManager().threeDWidget(0).threeDView()
    view.cornerAnnotation().SetText(vtk.vtkCornerAnnotation.UpperRight,textToAdd)
    view.cornerAnnotation().SetText(vtk.vtkCornerAnnotation.UpperLeft,textToAdd)
    textProperty = view.cornerAnnotation().GetTextProperty()
    textProperty.SetColor(*textColor)
    view.forceRender()

  def clearCornerAnnotation(self):
    '''Removes corner annotations by placing empty strings there'''
    self.addCornerAnnotation('')
2 Likes

Perfect - THANKS!!

-Hollister

1 Like