# Changing mouse modes from Python

**URL:** <https://discourse.slicer.org/t/changing-mouse-modes-from-python/22371>\
**Category:** Development\
**Created:** [March 8, 2022, 3:03pm UTC](https://discourse.slicer.org/t/changing-mouse-modes-from-python/22371 "2022-03-08T15:03:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanlangk](https://avatars.discourse-cdn.com/v4/letter/v/47e85d/32.png) [@vanlangk](https://discourse.slicer.org/u/vanlangk)\
**Post date:** [March 8, 2022, 3:03pm UTC](https://discourse.slicer.org/t/changing-mouse-modes-from-python/22371/1 "2022-03-08T15:03:02Z")

</div>

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](https://apidocs.slicer.org/v4.8/classvtkMRMLInteractionNode.html), 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](https://apidocs.slicer.org/v4.8/classvtkMRMLFiducial.html) 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 😅

Thanks a lot for your help!

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [March 8, 2022, 10:09pm UTC](https://discourse.slicer.org/t/changing-mouse-modes-from-python/22371/2 "2022-03-08T22:09:31Z")

</div>

The Markups toolbar has [three keyboard shortcuts](https://github.com/Slicer/Slicer/blob/537cda915f1662b871be68575ef3a82814f4ddbe/Modules/Loadable/Markups/Widgets/qMRMLMarkupsToolBar.cxx#L82) 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:

```auto
slicer.mrmlScene.AddNewNodeByClass('vtkMRMLMarkupsROINode')
slicer.modules.markups.toolBar().onPlacePointShortcut()

```

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [March 8, 2022, 10:14pm UTC](https://discourse.slicer.org/t/changing-mouse-modes-from-python/22371/3 "2022-03-08T22:14:54Z")

</div>

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

```python
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)

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 9, 2022, 7:00am UTC](https://discourse.slicer.org/t/changing-mouse-modes-from-python/22371/4 "2022-03-09T07:00:02Z")

</div>

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](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#add-a-button-to-module-gui-to-activate-control-point-placement). There are several [more markups placement examples](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#switching-to-markups-control-point-placement-mode) in the script repository.

---

<div class="post-metadata">

**Author:** ![vanlangk](https://avatars.discourse-cdn.com/v4/letter/v/47e85d/32.png) [@vanlangk](https://discourse.slicer.org/u/vanlangk)\
**Post date:** [March 10, 2022, 12:08am UTC](https://discourse.slicer.org/t/changing-mouse-modes-from-python/22371/5 "2022-03-10T00:08:55Z")

</div>

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