# Run Export To Files in the Segmentations Module from a script

**URL:** <https://discourse.slicer.org/t/run-export-to-files-in-the-segmentations-module-from-a-script/27235>\
**Category:** Development\
**Tags:** segmentation, python, module, extensions\
**Created:** [January 14, 2023, 1:12am UTC](https://discourse.slicer.org/t/run-export-to-files-in-the-segmentations-module-from-a-script/27235 "2023-01-14T01:12:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alistair\_McCutcheon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alistair_mccutcheon/32/13980_2.png) [@Alistair\_McCutcheon](https://discourse.slicer.org/u/Alistair_McCutcheon)\
**Post date:** [January 14, 2023, 1:12am UTC](https://discourse.slicer.org/t/run-export-to-files-in-the-segmentations-module-from-a-script/27235/1 "2023-01-14T01:12:12Z")

</div>

I’d like to run the Export To Files functionality in the Segmentations Module from a slicer module I am developing. When running this, I need to be able to pass in the destination folder and the file format (nii.gz).

Does anyone know how to do this please?

---

<div class="post-metadata">

**Author:** ![Alistair\_McCutcheon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alistair_mccutcheon/32/13980_2.png) [@Alistair\_McCutcheon](https://discourse.slicer.org/u/Alistair_McCutcheon)\
**Post date:** [January 14, 2023, 4:53am UTC](https://discourse.slicer.org/t/run-export-to-files-in-the-segmentations-module-from-a-script/27235/2 "2023-01-14T04:53:10Z")

</div>

This works (found here: [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#segmentations)):

```auto
outputPath = "c:/tmp"

def exportLabelmap():
  segmentationNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentationNode")
  referenceVolumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
  labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
  slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, referenceVolumeNode)
  filepath = outputPath + "/" + referenceVolumeNode.GetName() + "-label.nrrd"
  slicer.util.saveNode(labelmapVolumeNode, filepath)
  slicer.mrmlScene.RemoveNode(labelmapVolumeNode.GetDisplayNode().GetColorNode())
  slicer.mrmlScene.RemoveNode(labelmapVolumeNode)
  slicer.util.delayDisplay("Segmentation saved to " + filepath)

shortcut = qt.QShortcut(slicer.util.mainWindow())
shortcut.setKey(qt.QKeySequence("Ctrl+Shift+s"))
shortcut.connect( "activated()", exportLabelmap)

```

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [January 14, 2023, 4:27pm UTC](https://discourse.slicer.org/t/run-export-to-files-in-the-segmentations-module-from-a-script/27235/3 "2023-01-14T16:27:35Z")

</div>

For saving a scalar volume this should work, assuming you have a `yourScalarVolumeNode`:

```auto
tempDir = slicer.app.temporaryPath + "/Test/"
myStorageNode=yourScalarVolume.CreateDefaultStorageNode()
myStorageNode.SetFileName(tempDir+"test.nii.gz")
myStorageNode.WriteData(yourScalarVolume)

```
