Entry and exit events from MRML slice views

I’m hoping to perform actions in my scripted module when the user’s mouse enters or exits the Red and Yellow MRML slice views. I know this involves observing events, but I’m not sure what event to observe, and where to use AddObserver. How can I accomplish this?
Thanks.

These are standard VTK interaction events that you can get by observing low-level VTK objects (probably the interactor; see examples of accessing these low-level objects in the script repository).

However, I would recommend to use higher-level API, such as observing the crosshair node. You can get the current slice node using vtkMRMLCrosshairNode::GetCursorPositionXYZ method.

1 Like

Excellent, thank you. I’m able to get the slice node and its name that way.
I am using a ctkWorkflow, and I’ve also set it up to add the observer when advancing to the relevant step, and remove the observer afterwards:

def onEntry(self):
    ...
    self.crosshairNode=slicer.util.getNode('Crosshair')
    self.crosshairNodeObserverTag = self.crosshairNode.AddObserver(slicer.vtkMRMLCrosshairNode.CursorPositionModifiedEvent, self.onMouseMoved)
#end onEntry

def onExit(self):
    if(self.crosshairNodeObserverTag is not None):
        self.crosshairNode.RemoveObserver(self.crosshairNodeObserverTag)
#end onExit
1 Like

I am creating a crosshairnode but still the program shows erroe crosshairnode not defined

self.crosshairNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLCrosshairNode")
self.observeid = self.crosshairNode.AddObserver(slicer.vtkMRMLCrosshairNode.CursorPositionModifiedEvent, self.onMouseMoved)

There is a singleton crosshair node, which is used by the application. You need to retrieve and use that node, as shown in the examples (do not create a new crosshair node).