How do I use Python code to display 3D?

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.

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!!

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 .

The latest version of the documentation (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.

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

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!!!

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’

I’ve worked out this problem

1 Like