Slicer3:Python:ProcessMRMLEvents
From Slicer Wiki
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 MRML 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 [...]