# Handle Markups events from an external module

**URL:** <https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332>\
**Category:** Development\
**Created:** [March 28, 2019, 8:37pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332 "2019-03-28T20:37:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirclem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mirclem/32/3356_2.png) [@mirclem](https://discourse.slicer.org/u/mirclem)\
**Post date:** [March 28, 2019, 8:37pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/1 "2019-03-28T20:37:16Z")

</div>

Hi,

I noticed there is a massive work in progress on the markups widget which affects some modules using markups interaction. As the markup position is now previewed when hovering a slice, “PointAddedEvent” is triggered when entering the view and “PointModifiedEvent” is permanently triggered when hovering the view.

I wanted to use “PointStartInteractionEvent” but it is listed as deprecated in @lassoan recent commit. I had a quick look at the newly created vtkMRMLInteractionEventData.h and the way it is done in the [SlicerMarkupsWidget](https://github.com/Slicer/Slicer/blob/e9e96fc3f285b8c9a45869ce0a98b799f81ec807/Modules/Loadable/Markups/VTKWidgets/vtkSlicerMarkupsWidget.cxx#L493) but I can’t retranslate it into a python scripted module.

Thank you.

---

<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 28, 2019, 11:19pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/2 "2019-03-28T23:19:48Z")

</div>

The API is still being worked on. What would you like to do?

---

<div class="post-metadata">

**Author:** ![mirclem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mirclem/32/3356_2.png) [@mirclem](https://discourse.slicer.org/u/mirclem)\
**Post date:** [March 29, 2019, 2:35pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/4 "2019-03-29T14:35:12Z")

</div>

I would like to get a callback function only when a marker is placed and dragged in a view. Basically, the behavior that an observer of “PointModifiedEvent” did before the update and preventing this callback to be called during the preview phase.  
I want to use that when you move a markup in one view, so the other views follow. (Double left-click current action)

Also, in this [commit](https://github.com/Slicer/Slicer/commit/6289b33445e6a0ee412476f3fb82cbe360238f0d#diff-e7034a6ddfa3156097919e3390b88a61) you mention that markups should now “Slide on surface”, but can’t find what changed compared to previous versions. Most of the modules I have been working on were using this:

```
@vtk.calldata_type(vtk.VTK_INT)
def onPointModifiedEvent(self, caller, event, call_data):
    caller.RemoveObserver(tag["PointModifiedEventTag"])

    markupCoord = [0,0,0]
    caller.GetNthFiducialPosition(call_data, markupCoord)
    pointLocator = vtk.vtkPointLocator()
    pointLocator.SetDataSet(self.myModel.GetPolyData())
    pointLocator.AutomaticOn()
    pointLocator.BuildLocator()
    indexClosestPoint = pointLocator.FindClosestPoint(markupCoord)
    self.myModel.GetPolyData().GetPoints().GetPoint(indexClosestPoint, markupCoord)
    caller.SetNthFiducialPositionFromArray(call_data, markupCoord)

    PointModifiedEventTag = obj.AddObserver(obj.PointModifiedEvent, self.onPointModifiedEvent)
```

---

<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 29, 2019, 3:29pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/5 "2019-03-29T15:29:34Z")

</div>

You can check the status of a point (undefined/previewed/placed) to distinguish between previewed and already placed state. Let me know if this information is enough.

Jumping slice views while moving a markup seems to be a common need, so we may just add a flag to the markups display widget to enable/disable this.

Also note that now you can click on a markup to jump to it in all slice views. This, combined with the ability to see out-of-plane markup points (enable _projection_ in display settings), tracking the point on other slices might not be as important as before.

> [@mirclem](#):
>
> you mention that markups should now “Slide on surface”,

Yes, all markups slide on visible surface now. There is a single picker that is shared between all widgets to improve performance.

---

<div class="post-metadata">

**Author:** ![mirclem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mirclem/32/3356_2.png) [@mirclem](https://discourse.slicer.org/u/mirclem)\
**Post date:** [March 29, 2019, 3:53pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/6 "2019-03-29T15:53:56Z")

</div>

> [@lassoan](#):
>
> You can check the status of a point (undefined/previewed/placed) to distinguish between previewed and already placed state. Let me know if this information is enough.

Oh true, this is done by `GetNthControlPointPositionStatus()`.

> [@lassoan](#):
>
> Also note that now you can click on a markup to jump to it in all slice views. This, combined with the ability to see out-of-plane markup points (enable _projection_ in display settings), tracking the point on other slices might not be as important as before.

The _projection_ helps a lot to keep track of where the markup is, but when the markup slides on a round surface, it may shift to an adjacent slice and this might be confusing.

Anyway, thank you for the tips. I just downloaded today’s nightly version and many issues were solved compared to the one I had last week. I thought more maintenance would be needed for modules using markups but not anymore. I should not try to develop on WIP features.

---

<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 29, 2019, 5:27pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/7 "2019-03-29T17:27:44Z")

</div>

It is useful if you can test markups module and provide early feedback (what works, what you would need), because we can take those into account when refining the design or prioritizing development of new features.  
We’ll keep fixing and improving markups in the coming few months, so for production use, you might want to stick to the latest stable version of Slicer.

---

<div class="post-metadata">

**Author:** ![ljod](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ljod/32/652_2.png) [@ljod](https://discourse.slicer.org/u/ljod)\
**Post date:** [March 29, 2019, 5:33pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/8 "2019-03-29T17:33:14Z")

</div>

Hi Andras. This is a related question so I’m putting it here. We need to fix the SlicerDMRI usage of markups (interactive tractography) so SlicerDMRI can be available again in the nightlies. Specifically, what now replaces these events and calls (below), or where can I best look for an example of something similar?  
Thanks!!

MarkupAddedEvent  
MarkupRemovedEvent  
GetNthMarkupSelected

---

<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 29, 2019, 5:37pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/9 "2019-03-29T17:37:59Z")

</div>

Slicer5 migration guide should answer these questions, but let me know if it’s not enough.

[https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MigrationGuide/Slicer#Markups](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/MigrationGuide/Slicer#Markups)

---

<div class="post-metadata">

**Author:** ![ljod](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ljod/32/652_2.png) [@ljod](https://discourse.slicer.org/u/ljod)\
**Post date:** [March 29, 2019, 7:06pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/10 "2019-03-29T19:06:01Z")

</div>

Thanks! I’ll check it out and let you know.

---

<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 29, 2019, 7:40pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/11 "2019-03-29T19:40:03Z")

</div>

Also let me know if there is any feature that you would like markups to have. Many things that were not feasible to implement with the old design are possible now.

---

<div class="post-metadata">

**Author:** ![mirclem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mirclem/32/3356_2.png) [@mirclem](https://discourse.slicer.org/u/mirclem)\
**Post date:** [May 23, 2019, 3:27pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/12 "2019-05-23T15:27:06Z")

</div>

From what I can see [here](https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Markups/MRML/vtkMRMLMarkupsDisplayNode.cxx#L377), “Markups.MovingInSliceView” and “Markups.MovingMarkupIndex” are only accessible when hovering the views (preview mode), why not when the point is placed and moved afterward?

---

<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:** [May 23, 2019, 4:08pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/13 "2019-05-23T16:08:54Z")

</div>

“MovingInSliceView” means that the point is moving. Anyway, it is kept for a while for backward compatibility only, since there is now much richer API (that does not rely on custom MRML node attributes) for getting much more information.

Event data is now vtkMRMLInteractionEventData for many events (that contains information of what part of the markup was manipulated in which view node), you can also query active component type and index from the display node, and “active” state is stored in the display node (so you can choose to highlight hovered-over markups in only selected views).

Let me know if you find any inconveniences with the new API. We can make improvements as needed.

---

<div class="post-metadata">

**Author:** ![mirclem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mirclem/32/3356_2.png) [@mirclem](https://discourse.slicer.org/u/mirclem)\
**Post date:** [May 23, 2019, 5:18pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/14 "2019-05-23T17:18:57Z")

</div>

> [@lassoan](#):
>
> Event data is now vtkMRMLInteractionEventData for many events (that contains information of what part of the markup was manipulated in which view node), you can also query active component type and index from the display node, and “active” state is stored in the display node (so you can choose to highlight hovered-over markups in only selected views).

I understand which information vtkMRMLInteractionEventData can carry and this is very helpful. When you say event data is vtkMRMLInteractionEventData, do you mean the third argument of the callback function should be that type?  
Would I change the callback definition to (following)?

```
@vtk.calldata_type(vtk.VTK_OBJECT)
def onMarkupModified(self, caller, event, calldata):

```

And get current view like this:

```
if isinstance(calldata, slicer.vtkMRMLInteractionEventData):
    calldata.GetViewNode()

```

---

<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:** [May 23, 2019, 5:30pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/15 "2019-05-23T17:30:43Z")

</div>

Yes, all above is correct.

---

<div class="post-metadata">

**Author:** ![mirclem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mirclem/32/3356_2.png) [@mirclem](https://discourse.slicer.org/u/mirclem)\
**Post date:** [May 23, 2019, 5:37pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/16 "2019-05-23T17:37:56Z")

</div>

So far, Slicer crash without error message in the console using `@vtk.calldata_type(vtk.VTK_OBJECT)` when the mouse hovers a Slice View.

---

<div class="post-metadata">

**Author:** ![mirclem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mirclem/32/3356_2.png) [@mirclem](https://discourse.slicer.org/u/mirclem)\
**Post date:** [May 23, 2019, 7:09pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/17 "2019-05-23T19:09:03Z")

</div>

And from Visual Studio Debugger, it says:  
 ![error](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/a/1a8ddbbb689785e005455062c55c459e3d69e475.png)

---

<div class="post-metadata">

**Author:** ![mirclem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mirclem/32/3356_2.png) [@mirclem](https://discourse.slicer.org/u/mirclem)\
**Post date:** [May 28, 2019, 5:32pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/18 "2019-05-28T17:32:27Z")

</div>

Anyone tried to replicate this bug? Should I submit it?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [May 28, 2019, 9:19pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/19 "2019-05-28T21:19:52Z")

</div>

Hi @mirclem - Yes, if you have a simple way to reproduce this behavior definitely file an issue and put the link to it here. It would be great to have the snippet of python code to paste in the interactor and any steps needed that lead to the crash.

---

<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:** [May 28, 2019, 10:24pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/20 "2019-05-28T22:24:55Z")

</div>

I was able to reproduce the problem but did not have time to investigate much. The problem must likely is that the event data is not derived from vtkObject but from vtkObjectBase. I don’t know if there is a VTK calldata hint for vtkObjectBase, it would need to be investigated or asked on the VTK forum.

---

<div class="post-metadata">

**Author:** ![mirclem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mirclem/32/3356_2.png) [@mirclem](https://discourse.slicer.org/u/mirclem)\
**Post date:** [May 29, 2019, 2:39pm UTC](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332/21 "2019-05-29T14:39:50Z")

</div>

Hi, Ok I will do that then. Here is the code I used to get to this error:

```
@vtk.calldata_type(vtk.VTK_OBJECT)
def onMarkupChanged(caller,event,call_data):
	print(event)

markupsFiducialNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLMarkupsFiducialNode')
markupsFiducialNode.AddObserver(slicer.vtkMRMLMarkupsFiducialNode.PointModifiedEvent, onMarkupChanged)

selectionNode = slicer.mrmlScene.GetNodeByID("vtkMRMLSelectionNodeSingleton")
selectionNode.SetReferenceActivePlaceNodeID(markupsFiducialNode.GetID())
interactionNode = slicer.mrmlScene.GetNodeByID("vtkMRMLInteractionNodeSingleton")
interactionNode.SetCurrentInteractionMode(1)
interactionNode.SetPlaceModePersistence(0)

```

Then, just mouse over any views.

[Next page](https://discourse.slicer.org/t/handle-markups-events-from-an-external-module/6332.md?page=2)
