Slicer3:Python:ProcessMRMLEvents

From Slicer Wiki
Jump to: navigation, search
Home < Slicer3:Python:ProcessMRMLEvents

Slicer3 Python: ProcessMRMLEvents

In a Python Scripted Module, it is possible to observe MRML events. The observed MRML events can be fired from the MRML scene or from MRML nodes with a known ID.

The following example of a ScriptedModuleGUI class shows how to listen to MRML events:

# The event ids can be found in the header files (f.e. Libs/MRML/vtkMRMLVolumeNode.h, Libs/MRML/vtkMRMLScene.h..)
vtkMRMLVolumeNode_ImageDataModifiedEvent = 18001
vtkMRMLScene_NodeAddedEvent = 66000


class AtlasCreatorGUI(ScriptedModuleGUI):

    [...]

    def AddGUIObservers(self):
        # first, we listen to a MRML node event
        self._scalarVolumeNode = slicer.MRMLScene.GetNodeByID("vtkMRMLScalarVolumeNode1")

        # second, we listen to a MRML scene event
        self._imagedataModifiedTag = self.AddMRMLObserverByNumber(self._scalarVolumeNode,vtkMRMLVolumeNode_ImageDataModifiedEvent)

    [...]


    def ProcessMRMLEvents(self,callerID,event,callDataID = None):
    ''' gets called, when an observed MRML event was fired '''
    
        # observe MRMLNode events
        if callerID == "vtkMRMLScalarVolumeNode1" and event == vtkMRMLVolumeNode_ImageDataModifiedEvent:
            print "ImageData of vtkMRMLScalarVolumeNode1 was modified."
        
        # observe MRMLScene events
        if callerID == "MRMLScene" and event == vtkMRMLScene_NodeAddedEvent and callDataID:
            
            callDataAsMRMLNode = slicer.MRMLScene.GetNodeByID(callDataID)
            
            if isinstance(callDataAsMRMLNode, slicer.vtkMRMLScalarVolumeNode):
                print "A new vtkMRMLScalarVolumeNode was added: " + callDataID

    [...]