# How to get a scene reliably and why is it null?

**URL:** <https://discourse.slicer.org/t/how-to-get-a-scene-reliably-and-why-is-it-null/42700>\
**Category:** Development\
**Created:** [April 26, 2025, 5:07pm UTC](https://discourse.slicer.org/t/how-to-get-a-scene-reliably-and-why-is-it-null/42700 "2025-04-26T17:07:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [April 26, 2025, 5:07pm UTC](https://discourse.slicer.org/t/how-to-get-a-scene-reliably-and-why-is-it-null/42700/1 "2025-04-26T17:07:54Z")

</div>

Hello,

I am stumbling on getting a scene node in most functions of a class while it is not null elsewhere in the same class ([1](https://github.com/chir-set/SlicerExtraMarkups/blob/2938b23ece62e3d8e58fb5afaf9ec1d118a6e453/Shape/MRML/vtkMRMLMarkupsShapeNode.cxx#L78), [2](https://github.com/chir-set/SlicerExtraMarkups/blob/2938b23ece62e3d8e58fb5afaf9ec1d118a6e453/Shape/MRML/vtkMRMLMarkupsShapeNode.cxx#L91), [3](https://github.com/chir-set/SlicerExtraMarkups/blob/2938b23ece62e3d8e58fb5afaf9ec1d118a6e453/Shape/MRML/vtkMRMLMarkupsShapeNode.cxx#L1228)).

In the constructor of the referenced class for example, it is always null, and in many functions also. The node is well [registered](https://github.com/chir-set/SlicerExtraMarkups/blob/2938b23ece62e3d8e58fb5afaf9ec1d118a6e453/Shape/Logic/vtkSlicerShapeLogic.cxx#L80).

Can a core developer provide a hint to resolve this?

Thank you.

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [April 26, 2025, 6:29pm UTC](https://discourse.slicer.org/t/how-to-get-a-scene-reliably-and-why-is-it-null/42700/2 "2025-04-26T18:29:27Z")

</div>

Precisions:

It 's on Linux and Slicer is built at de382bd89.

---

<div class="post-metadata">

**Author:** ![Mik](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mik](https://discourse.slicer.org/u/Mik)\
**Post date:** [April 27, 2025, 2:18pm UTC](https://discourse.slicer.org/t/how-to-get-a-scene-reliably-and-why-is-it-null/42700/3 "2025-04-27T14:18:29Z")

</div>

Have you tried this-\>Scene?  
Had your node already been added to the scene?

---

<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:** [April 27, 2025, 2:27pm UTC](https://discourse.slicer.org/t/how-to-get-a-scene-reliably-and-why-is-it-null/42700/4 "2025-04-27T14:27:35Z")

</div>

There’s a distinction between registering the node type so that the scene knows how to operate on it, and adding an instance of the node type to the scene. The node class needs to know what scene it is in so it can do things like creating the default display and storage nodes, adding them to the scene, and referencing their IDs.

Some node methods, like `UpdateNumberOfControlPoints` you referenced, really should include a check for a null scene pointer. Probably it’s not been added since that operation is unlikely to be called unless the node is already in use in a valid scene.

Does that address your question @chir.set ?

> <https://github.com/Slicer/Slicer/blob/main/Libs/MRML/Core/vtkMRMLScene.h#L131-L151>

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [April 27, 2025, 5:24pm UTC](https://discourse.slicer.org/t/how-to-get-a-scene-reliably-and-why-is-it-null/42700/5 "2025-04-27T17:24:39Z")

</div>

I have reworked parts of the project and things are more predictable now. Namely with [this commit](https://github.com/chir-set/SlicerExtraMarkups/commit/afefb69878fc5d580e455c72e4d8ef7d3187de0a) that does nothing special now in a constructor, apart from an elementary assignment . A major cause of trouble was a `cout` statement in the constructor (the `cout` seemed elementary too 😄 ). This seemed very disturbing to whatever underneath, even the markups raw pointer was changing randomly. Once removed, things calmed down.

Thank you for having considered.
