# How to use Python script to judge whether a node exists or not

**URL:** <https://discourse.slicer.org/t/how-to-use-python-script-to-judge-whether-a-node-exists-or-not/18241>\
**Category:** Development\
**Created:** [June 21, 2021, 7:10am UTC](https://discourse.slicer.org/t/how-to-use-python-script-to-judge-whether-a-node-exists-or-not/18241 "2021-06-21T07:10:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![qiqi5210](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/qiqi5210/32/15920_2.png) [@qiqi5210](https://discourse.slicer.org/u/qiqi5210)\
**Post date:** [June 21, 2021, 7:10am UTC](https://discourse.slicer.org/t/how-to-use-python-script-to-judge-whether-a-node-exists-or-not/18241/1 "2021-06-21T07:10:23Z")

</div>

segmentationNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentationNode”)  
After creating a node, you don’t want to create a new node. How to judge whether a node has been created

---

<div class="post-metadata">

**Author:** ![Juicy](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Juicy](https://discourse.slicer.org/u/Juicy)\
**Post date:** [June 21, 2021, 7:35am UTC](https://discourse.slicer.org/t/how-to-use-python-script-to-judge-whether-a-node-exists-or-not/18241/2 "2021-06-21T07:35:08Z")

</div>

I don’t know exactly what you want to do but here is a snippet of code I used to check whether there are any segmentation nodes in the scene and if so, cycle through them all and turn their visibility off so that they are hidden. You may be able to use some part of it or something similar for what you are doing.

```auto
  # Hide all Segments
  noOfSegmentationNodes = slicer.mrmlScene.GetNumberOfNodesByClass('vtkMRMLSegmentationNode')
  if noOfSegmentationNodes > 0:
    for i in range(0, noOfSegmentationNodes):
      currentSegment = slicer.mrmlScene.GetNthNodeByClass(i, 'vtkMRMLSegmentationNode')
      currentSegment.SetDisplayVisibility(0)

```

---

<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:** [June 21, 2021, 1:30pm UTC](https://discourse.slicer.org/t/how-to-use-python-script-to-judge-whether-a-node-exists-or-not/18241/3 "2021-06-21T13:30:13Z")

</div>

> [@qiqi5210](#):
>
> How to judge whether a node has been created

If you develop a module: You would normally store the selected node as a node reference in a parameter node. If you create a new module using Extension Wizard module then the generated module already works like this. You can also look at many other Python modules in Slicer core and extensions.

If you develop a script: store the node in some variable. If you create the node in a function then return that variable from the function.
