# Cannot get display node of customized markups in c++

**URL:** <https://discourse.slicer.org/t/cannot-get-display-node-of-customized-markups-in-c/26810>\
**Category:** Development\
**Created:** [December 19, 2022, 2:37am UTC](https://discourse.slicer.org/t/cannot-get-display-node-of-customized-markups-in-c/26810 "2022-12-19T02:37:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nnzzll](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/nnzzll/32/16350_2.png) [@nnzzll](https://discourse.slicer.org/u/nnzzll)\
**Post date:** [December 19, 2022, 2:37am UTC](https://discourse.slicer.org/t/cannot-get-display-node-of-customized-markups-in-c/26810/1 "2022-12-19T02:37:56Z")

</div>

I customized a node called `vtkMRMLDeformableROINode` which is derived from `vtkMRMLMarkupsNode`.  
I tried to get its display node and set some properties.

The code below written in python works fine without any problem:

```auto
roiNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLDeformableROINode")
# setup Node ...
displayNode = roiNode.GetDisplayNode()
displayNode.SetDisplayableOnlyInView("vtkMRMLSliceNodeRed")

```

However, when I trying to do the same thing in C++:

```auto
auto displayNode = vtkMRMLMarkupsDisplayNode::SafeDownCast(roiNode->GetDisplayNode());
displayNode->SetDisplayableOnlyInView("vtkMRMLSliceNodeRed");

```

Segmentation fault occured and by printing out the backtrace I found out that `roiNode->GetDisplayNode()` returned a `nullptr`.

Why does the C++ code performed different with python code? How can I fix it?

---

<div class="post-metadata">

**Author:** ![nnzzll](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/nnzzll/32/16350_2.png) [@nnzzll](https://discourse.slicer.org/u/nnzzll)\
**Post date:** [December 19, 2022, 2:47am UTC](https://discourse.slicer.org/t/cannot-get-display-node-of-customized-markups-in-c/26810/2 "2022-12-19T02:47:56Z")

</div>

I figured out the reason.

In c++ version, I called `qSlicerApplication::application()->mrmlScene()->StartState(vtkMRMLScene::BatchProcessState)` before adding the new node. If I comment out this line, everything works just fine.

So can anyone gives an advice about how or when should I use the **State** feature?

---

<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:** [December 19, 2022, 5:03am UTC](https://discourse.slicer.org/t/cannot-get-display-node-of-customized-markups-in-c/26810/3 "2022-12-19T05:03:38Z")

</div>

After you create a new displayable node programmatically it is your responsibility to add display node as well. The easiest way for creating the display node and associate it with the markup node is to call `CreateDefaultDisplayNodes()` method.

> [@nnzzll](#):
>
> So can anyone gives an advice about how or when should I use the **State** feature?

Use it if you manipulate at least 10-20 nodes. Starting/ending batch processing state is quite costly (rebuilds scene models in all node selector GUI widgets, etc.), so it is only worth activating it if you save a lot of time by not ignoring scene updates during the batch process state.
