Difference between revisions of "Slicer3:Python:ProcessMRMLEvents"

From Slicer Wiki
Jump to: navigation, search
Line 1: Line 1:
= Slicer3 Python: ProcessMRMLEvents =
+
= Slicer3 Python: Using AddMRMLObserverByNumber and ProcessMRMLEvents to observe MRML events=
  
 
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.
 
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.
Line 6: Line 6:
  
 
<pre>
 
<pre>
# The event ids can be found in the header files (f.e. Libs/MRML/vtkMRMLVolumeNode.h, Libs/MRML/vtkMRMLScene.h..)
+
# The event ids can be found in the MTML C++ header files (f.e. Libs/MRML/vtkMRMLVolumeNode.h, Libs/MRML/vtkMRMLScene.h..)
 
vtkMRMLVolumeNode_ImageDataModifiedEvent = 18001
 
vtkMRMLVolumeNode_ImageDataModifiedEvent = 18001
 
vtkMRMLScene_NodeAddedEvent = 66000
 
vtkMRMLScene_NodeAddedEvent = 66000
Line 18: Line 18:
 
         # first, we listen to a MRML node event
 
         # first, we listen to a MRML node event
 
         self._scalarVolumeNode = slicer.MRMLScene.GetNodeByID("vtkMRMLScalarVolumeNode1")
 
         self._scalarVolumeNode = slicer.MRMLScene.GetNodeByID("vtkMRMLScalarVolumeNode1")
 +
        self._imagedataModifiedTag = self.AddMRMLObserverByNumber(self._scalarVolumeNode,vtkMRMLVolumeNode_ImageDataModifiedEvent)
  
 
         # second, we listen to a MRML scene event
 
         # second, we listen to a MRML scene event
         self._imagedataModifiedTag = self.AddMRMLObserverByNumber(self._scalarVolumeNode,vtkMRMLVolumeNode_ImageDataModifiedEvent)
+
         self._mrmlNodeAddedTag = self.AddMRMLObserverByNumber(slicer.MRMLScene,vtkMRMLScene_NodeAddedEvent)
  
 
     [...]
 
     [...]

Revision as of 18:41, 24 February 2011

Home < Slicer3:Python:ProcessMRMLEvents

Slicer3 Python: Using AddMRMLObserverByNumber and ProcessMRMLEvents to observe MRML events

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 MTML C++ 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")
        self._imagedataModifiedTag = self.AddMRMLObserverByNumber(self._scalarVolumeNode,vtkMRMLVolumeNode_ImageDataModifiedEvent)

        # second, we listen to a MRML scene event
        self._mrmlNodeAddedTag = self.AddMRMLObserverByNumber(slicer.MRMLScene,vtkMRMLScene_NodeAddedEvent)

    [...]


    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

    [...]