Changing mouse modes from Python

Hello guys, I am new to Slicer and I am currently developing an extension. As part of this extension, I need to be able to switch between mouse interaction modes (e.g., ruler, fiducial, angle), from my Python script. Is there an easy way to this from the API?

I have looked into the MRML interaction node, but it appears that the mouse modes are limited to: place, view transform, and select; these are not specific enough for my use case. Also, it appears that the MRML fiducial node can place markers from the script, but it doesn’t change the mouse mode itself.

Please forgive me if I am simply missing something obvious :sweat_smile:

Thanks a lot for your help!

The Markups toolbar has three keyboard shortcuts that interact with the mouse mode:

Create new markups node of current/default type: Ctrl+Shift+A
Toggle Place Mode persistence: Ctrl+Shift+T
Place point in active markup node: Ctrl+Shift+Space

You can change the interaction mode through Python by creating a new node with of the type you want (or make an existing node active) and using the place point callback to initiate placement in that node. For example:

slicer.mrmlScene.AddNewNodeByClass('vtkMRMLMarkupsROINode')
slicer.modules.markups.toolBar().onPlacePointShortcut()
2 Likes

You may be interested in SetReferenceActivePlaceNodeClassName("vtkMRMLMarkupsLineNode") as might be used below.

selection_node = slicer.mrmlScene.GetNodeByID("vtkMRMLSelectionNodeSingleton")
interaction_node = slicer.mrmlScene.GetNodeByID("vtkMRMLInteractionNodeSingleton")

slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsLineNode")
selection_node.SetReferenceActivePlaceNodeClassName("vtkMRMLMarkupsLineNode")
interaction_node.SetCurrentInteractionMode(interaction_node.Place)
1 Like

One more tip: the easiest way to add a button that places a markup node is the markups place widget. See complete example in the script repository. There are several more markups placement examples in the script repository.

1 Like

Thanks a lot guys! I’ll get back to implementing my extension :slight_smile: