# Delayed callback when SegmentationNode is added to the scene

**URL:** <https://discourse.slicer.org/t/delayed-callback-when-segmentationnode-is-added-to-the-scene/14674>\
**Category:** Development\
**Tags:** python\
**Created:** [November 18, 2020, 3:39pm UTC](https://discourse.slicer.org/t/delayed-callback-when-segmentationnode-is-added-to-the-scene/14674 "2020-11-18T15:39:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joachim](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@joachim](https://discourse.slicer.org/u/joachim)\
**Post date:** [November 18, 2020, 3:39pm UTC](https://discourse.slicer.org/t/delayed-callback-when-segmentationnode-is-added-to-the-scene/14674/1 "2020-11-18T15:39:23Z")

</div>

I’ve created a listener when _SegmentationNodes_ are added to my scene, something like this:

```auto
@vtk.calldata_type(vtk.VTK_OBJECT)
def onNodeAdded(caller, event, node):
    if node.IsA( "vtkMRMLSegmentationNode" ):
        # call `myFunction()` in current thread
        myFunction( node )

slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.NodeAddedEvent, onNodeAdded)

```

Actually, I’m performing some editor effects automatically whenever I add a _SegmentationNode_.

If I don’t use the callback (i.e. `slicer.mrmlScene.AddObserver()` not called) and do the steps manually: call `myFunction()` in the interpreter after the node was added, things works as expected. However, if I add the callback as an observer (above code block), something wrong happens so `myFunction()` fails. I guess this is because the _SegmentationNode_ has to be fully added in order for `myFunction()` to work.

How can I call `myFunction()` after the _SegmentationNode_ and its dependencies was completely added? A timed callback to `myFunction()` can be a solution (practically but not completely safe in theory).

---

<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:** [November 18, 2020, 10:09pm UTC](https://discourse.slicer.org/t/delayed-callback-when-segmentationnode-is-added-to-the-scene/14674/2 "2020-11-18T22:09:57Z")

</div>

Yes, probably you want to wait for the segmentation node to get a default display node, etc. You can add a delayed callback (which is called when the application becomes idle) like this:

```
qt.QTimer.singleShot(0, myCallback)
```
