# Segmentation visibility by code

**URL:** https://discourse.slicer.org/t/segmentation-visibility-by-code/13826
**Category:** Support
**Tags:** segmentation, python
**Created:** [October 2, 2020, 7:58pm UTC](https://discourse.slicer.org/t/segmentation-visibility-by-code/13826 "2020-10-02T19:58:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![aldoSanchez](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@aldoSanchez](https://discourse.slicer.org/u/aldoSanchez)
#### Post date: [October 2, 2020, 7:58pm UTC](https://discourse.slicer.org/t/segmentation-visibility-by-code/13826/1 "2020-10-02T19:58:22Z")

</div>

Hi, guys.  
today I am trying to access the visibility of objects created in 3d models by code.  
What I’m trying to do is what’s in the image just show Left-Cerebral-Cortex and remove the visibility of all the others.  
thanks

 ![segmentation](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/0/00ffeeb87382ba795e2b09d8702fc7f38e38b692.png)

---

<div class="post-metadata">

### Author: ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)
#### Post date: [October 2, 2020, 8:04pm UTC](https://discourse.slicer.org/t/segmentation-visibility-by-code/13826/2 "2020-10-02T20:04:02Z")

</div>

Try something like this:

```auto
displayNode.SetAllSegmentsVisibility(False)
displayNode.SetSegmentVisibility(leftCerebralCortexSegmentID, True)

```

[SetAllSegmentsVisibility](https://apidocs.slicer.org/master/classvtkMRMLSegmentationDisplayNode.html#ac6f83b3e93af6d030690b7cc955abcdd) (bool visible)  
[SetSegmentVisibility](https://apidocs.slicer.org/master/classvtkMRMLSegmentationDisplayNode.html#aee65fdd850b42e1b1d1f7117bc082b92) (std::string segmentID, bool visible)

If you still want all of the the 2D representations to be visible, you can use these functions:  
[SetAllSegmentsVisibility3D](https://apidocs.slicer.org/master/classvtkMRMLSegmentationDisplayNode.html#a9ad8635b19e7168dc1a693cf9872bbbe) (bool visible, bool changeVisibleSegmentsOnly=false)  
[SetSegmentVisibility3D](https://apidocs.slicer.org/master/classvtkMRMLSegmentationDisplayNode.html#a14e8c2cd351be7cea8793283845c6526) (std::string segmentID, bool visible)

---

<div class="post-metadata">

### Author: ![aldoSanchez](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@aldoSanchez](https://discourse.slicer.org/u/aldoSanchez)
#### Post date: [October 3, 2020, 10:58pm UTC](https://discourse.slicer.org/t/segmentation-visibility-by-code/13826/3 "2020-10-03T22:58:19Z")

</div>

hi  
this works to hide all segmentation:

```auto
disp=slicer.util.getNode('Segmentation')
disp.SetDisplayVisibility(False)

```

but I can’t make it show only Left-Cerebral-Cortex .  
try this:

```auto
slicer.vtkMRMLSegmentationDisplayNode('Segmentation').SetAllSegmentsVisibility(False)

```

but I have the following error:

```auto
ValueError: could not extract hexadecimal address from argument string

```

---

<div class="post-metadata">

### Author: ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)
#### Post date: [October 5, 2020, 2:55pm UTC](https://discourse.slicer.org/t/segmentation-visibility-by-code/13826/4 "2020-10-05T14:55:49Z")

</div>

```auto
segmentationNode = slicer.util.getNode('Segmentation')
displayNode = segmentationNode.GetDisplayNode()
displayNode.SetAllSegmentsVisibility(False) # Hide all segments
displayNode.SetSegmentVisibility(leftCerebralCortexSegmentID, True) # Show specific segment

```

---

<div class="post-metadata">

### Author: ![aldoSanchez](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@aldoSanchez](https://discourse.slicer.org/u/aldoSanchez)
#### Post date: [October 5, 2020, 3:03pm UTC](https://discourse.slicer.org/t/segmentation-visibility-by-code/13826/5 "2020-10-05T15:03:02Z")

</div>

> [@Sunderlandkyl](#):
>
> `leftCerebralCortexSegmentID`

How can I see the ID of  
leftCerebralCortexSegmentID  
the label name is Left-Cerebral-Cortex

---

<div class="post-metadata">

### Author: ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)
#### Post date: [October 5, 2020, 3:05pm UTC](https://discourse.slicer.org/t/segmentation-visibility-by-code/13826/6 "2020-10-05T15:05:04Z")

</div>

You can use this:

```auto
segmentation = segmentationNode.GetSegmentation()
leftCerebralCortexSegmentID = segmentation.GetSegmentIdBySegmentName("Left-Cerebral-Cortex")

```

[GetSegmentIdBySegmentName](https://apidocs.slicer.org/master/classvtkSegmentation.html#adda91e085fa229cd6d88abc1aa268b12) (std::string name)

---

<div class="post-metadata">

### Author: ![aldoSanchez](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@aldoSanchez](https://discourse.slicer.org/u/aldoSanchez)
#### Post date: [October 5, 2020, 3:15pm UTC](https://discourse.slicer.org/t/segmentation-visibility-by-code/13826/7 "2020-10-05T15:15:20Z")

</div>

thank you so much it works perfect i leave the code here:

```python
segmentationNode=getNode('Segmentation')
segmentation = segmentationNode.GetSegmentation()
leftCerebralCortexSegmentID = segmentation.GetSegmentIdBySegmentName("Left-Cerebral-Cortex")
displayNode = segmentationNode.GetDisplayNode()
displayNode.SetAllSegmentsVisibility(False) # Hide all segments
displayNode.SetSegmentVisibility(leftCerebralCortexSegmentID, True) # Show specific segment

```
