# Combine STLs and export as NRRD

**URL:** <https://discourse.slicer.org/t/combine-stls-and-export-as-nrrd/16812>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [March 28, 2021, 6:16pm UTC](https://discourse.slicer.org/t/combine-stls-and-export-as-nrrd/16812 "2021-03-28T18:16:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sulaimanvesal](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sulaimanvesal/32/10463_2.png) [@sulaimanvesal](https://discourse.slicer.org/u/sulaimanvesal)\
**Post date:** [March 28, 2021, 6:16pm UTC](https://discourse.slicer.org/t/combine-stls-and-export-as-nrrd/16812/1 "2021-03-28T18:16:42Z")

</div>

Hi @lassoan,

I am able to export .stl file to a single binary label map (nrrd file) very well.

However, I have two .stl files (gland and tumor) that I need to combine them first and then export them as NRRD file.

I wrote the code based on the help from the [segmentation examples in script repo](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Export_labelmap_node_from_segmentation_node) and my understandings. However, I get this error and I think I am missing something.

I would appreciate, if you help me out with this.

```
import SampleData
import numpy as np
import SimpleITK as sitk
import sitkUtils

outputPath ="/Volumes/Mac_Partition/output_dir/"
# Input nodes
volumeNode = slicer.util.loadVolume('/Volumes/Mac_Partition/files/MRI_0001.nii.gz')
prostateNode = slicer.util.loadSegmentation('/Volumes/Mac_Partition/files/STLs/GlandSurface.STL')

# Write segmentation to labelmap volume node with a geometry that matches the volume node
labelmapVolumeNode1 = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(prostateNode, labelmapVolumeNode1, volumeNode)

targetNode = slicer.util.loadSegmentation('/Volumes/Mac_Partition/files/STLs/Target.STL')
# Write segmentation to labelmap volume node with a geometry that matches the volume node
labelmapVolumeNode2 = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(targetNode, labelmapVolumeNode2, volumeNode)

# 'a' and 'b' are numpy arrays,
a = slicer.util.arrayFromVolume(labelmapVolumeNode1)
b = slicer.util.arrayFromVolume(labelmapVolumeNode2)
# combined using any numpy array operations to produce the result array 'c'
c = b-a

segmentationNode = slicer.modules.volumes.logic().CloneVolume(volumeNode, "Difference")
slicer.util.updateVolumeFromArray(segmentationNode, c)
setSliceViewerLayers(background=segmentationNode)

# Write segmentation to labelmap volume node with a geometry that matches the volume node
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
print(segmentationNode)
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, volumeNode)

# Masking
voxels = slicer.util.arrayFromVolume(volumeNode)
mask = slicer.util.arrayFromVolume(labelmapVolumeNode)
maskedVoxels = np.copy(voxels) # we don't want to modify the original volume
maskedVoxels[mask==0] = 0

# Write masked volume to volume node and save it to disk.
maskedVolumeNode = slicer.modules.volumes.logic().CloneVolume(volumeNode, "Masked")
slicer.util.updateVolumeFromArray(maskedVolumeNode, maskedVoxels)
slicer.util.setSliceViewerLayers(maskedVolumeNode)
filepath = outputPath + "/" + volumeNode.GetName()+"-label.nrrd"
slicer.util.saveNode(labelmapVolumeNode, filepath)

```

This is the error:  
---------------------------------------------------------------------------  
TypeError Traceback (most recent call last)  
In [19]:  
Line 32:

```
TypeError: ExportVisibleSegmentsToLabelmapNode argument 1: method requires a vtkMRMLSegmentationNode, a vtkMRMLScalarVolumeNode was provided.
---------------------------------------------------------------------------
```

---

<div class="post-metadata">

**Author:** ![sulaimanvesal](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sulaimanvesal/32/10463_2.png) [@sulaimanvesal](https://discourse.slicer.org/u/sulaimanvesal)\
**Post date:** [March 28, 2021, 11:40pm UTC](https://discourse.slicer.org/t/combine-stls-and-export-as-nrrd/16812/2 "2021-03-28T23:40:51Z")

</div>

Never mind, I figured out the solution and corrected the code. Now I am able to combine STLs and export them as NRRD.

---

<div class="post-metadata">

**Author:** ![SabrinaVerga](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@SabrinaVerga](https://discourse.slicer.org/u/SabrinaVerga)\
**Post date:** [December 9, 2021, 10:31am UTC](https://discourse.slicer.org/t/combine-stls-and-export-as-nrrd/16812/3 "2021-12-09T10:31:39Z")

</div>

Hi @sulaimanvesal, I am trying to perform the same task as the one you shared in this post. I am still getting the same errors as yours, but I saw you solved the problem.  
Do you mind share your code with the solution ?  
thanks in advande for helping,  
Sabrina

---

<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 11, 2021, 3:31pm UTC](https://discourse.slicer.org/t/combine-stls-and-export-as-nrrd/16812/4 "2021-12-11T15:31:13Z")

</div>

The code above is incorrect. It sets a volume node in the variable called `segmentationNode`. Then this volume node is used as an input in `ExportVisibleSegmentsToLabelmapNode`, which expects a segmentation node as input. I would recommend to complete the [Scripting and module development tutorial](https://www.slicer.org/wiki/Documentation/Nightly/Training#PerkLab.27s_Slicer_bootcamp_training_materials), study the [segmentation section in the documentation](https://slicer.readthedocs.io/en/latest/user_guide/image_segmentation.html) and examples in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html), which all work correctly. Then, once you have a good understanding of what’s happening, you can have a look at the script above and make it work.
