# How do I use Python code to display 3D？

**URL:** https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401
**Category:** Development
**Tags:** segmentation, python, 3d-model
**Created:** [January 11, 2022, 4:56am UTC](https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401 "2022-01-11T04:56:12Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![pleuvoir](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pleuvoir](https://discourse.slicer.org/u/pleuvoir)
#### Post date: [January 11, 2022, 4:56am UTC](https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401/1 "2022-01-11T04:56:12Z")

</div>

**Software version:** 4.11.20210226 R29738/7A593C8  
**Problem description:** from the official web page, find the following code: segmentation. CreateClosedSurfaceRepresentation () 。  
I tried to realize the “Create\_” function of clicking the button next to “Closed surface” under the “Representations” function of “Segmentations” module in the software, but it kept prompting errors or did not show the effect.

---

<div class="post-metadata">

### Author: ![pleuvoir](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pleuvoir](https://discourse.slicer.org/u/pleuvoir)
#### Post date: [January 11, 2022, 5:33am UTC](https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401/2 "2022-01-11T05:33:26Z")

</div>

As shown in the previous post, I tried to change the statement in the diagram to:  
LabelmapVolumeNode = slicer. Util. GetNode (" label “)  
Seg = slicer. MrmlScene. AddNewNodeByClass (” vtkMRMLSegmentationNode ")  
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(labelmapVolumeNode, seg)  
seg.CreateClosedSurfaceRepresentation()  
To achieve the “create” button effect in software.  
But the runtime keeps saying: Label cannot be found.  
Can you offer some ideas or solutions? Thank you so much!!

---

<div class="post-metadata">

### Author: ![pleuvoir](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pleuvoir](https://discourse.slicer.org/u/pleuvoir)
#### Post date: [January 11, 2022, 6:05am UTC](https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401/3 "2022-01-11T06:05:13Z")

</div>

The indexth node where name or ID matches is used to obtain the indexth node where name or ID matches. Seems to be the reason for my error. However, the problem is still unresolved. If I just use getNode(“label”), I will be told there is no “label” node or something. Could you please tell me how I can implement python code to display a 3D image of the current 2D slice .

---

<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: [January 11, 2022, 7:42pm UTC](https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401/4 "2022-01-11T19:42:12Z")

</div>

The latest version of the documentation ([https://slicer.readthedocs.io/en/latest/](https://slicer.readthedocs.io/en/latest/)) is applicable to the latest Slicer Preview Release. Please use the latest Slicer Preview Release and let us know if you still have questions.

---

<div class="post-metadata">

### Author: ![pleuvoir](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pleuvoir](https://discourse.slicer.org/u/pleuvoir)
#### Post date: [January 12, 2022, 1:38am UTC](https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401/5 "2022-01-12T01:38:56Z")

</div>

all right,I can try it ,thank you very much

---

<div class="post-metadata">

### Author: ![pleuvoir](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pleuvoir](https://discourse.slicer.org/u/pleuvoir)
#### Post date: [January 12, 2022, 8:07am UTC](https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401/6 "2022-01-12T08:07:40Z")

</div>

Dear Mr. Lassoan, thank you for your reply. I do have a question that needs to be answered, as shown below:  
I followed your guidance to find Python code that I think can be used instead of GUI to display 3D operations. However, the copy fails to run in Python on the software interface. I changed getNode(“label”) to getNode(“Segment\_2001”) and It`s failed .  
then I changed getNode(“label”)to Silcer.uitl.getNode (“”) on the same line as the error .  
Thank you very much and look forward to your reply!!!

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/8/580aa76c7e605d58836285b582486b8253acacb6.png)

---

<div class="post-metadata">

### Author: ![pleuvoir](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pleuvoir](https://discourse.slicer.org/u/pleuvoir)
#### Post date: [January 12, 2022, 8:12am UTC](https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401/7 "2022-01-12T08:12:41Z")

</div>

And,This is an error after I changed getNode(“label”) to slicer.util.getNode(“label”) :  
Error:Traceback (most recent call last):  
File “”, line 1, in   
File “D:\software\PycharmProjects\For Xv of Tiantan\For Xv of Tiantan\ColorMapper\Mapper.py”, line 83, in execute  
labelmapVolumeNode = slicer.util.getNode(“label”)  
File “D:\software\Slicer 4.11.20210226\bin\Python\slicer\util.py”, line 1312, in getNode  
raise MRMLNodeNotFoundException(“could not find nodes in the scene by name or id ‘%s’” % (pattern if (isinstance(pattern, str)) else “”))  
slicer.util.MRMLNodeNotFoundException: could not find nodes in the scene by name or id ‘label’

---

<div class="post-metadata">

### Author: ![pleuvoir](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pleuvoir](https://discourse.slicer.org/u/pleuvoir)
#### Post date: [January 21, 2022, 1:44am UTC](https://discourse.slicer.org/t/how-do-i-use-python-code-to-display-3d/21401/8 "2022-01-21T01:44:13Z")

</div>

I’ve worked out this problem
