# Batch conversion of .nrrd to models

**URL:** <https://discourse.slicer.org/t/batch-conversion-of-nrrd-to-models/16663>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [March 20, 2021, 11:53pm UTC](https://discourse.slicer.org/t/batch-conversion-of-nrrd-to-models/16663 "2021-03-20T23:53:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Selva](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Selva](https://discourse.slicer.org/u/Selva)\
**Post date:** [March 20, 2021, 11:53pm UTC](https://discourse.slicer.org/t/batch-conversion-of-nrrd-to-models/16663/1 "2021-03-20T23:53:11Z")

</div>

Hi,

I have a batch of segments-.nrrd files. (100s)  
I would like to load them (Add data- files) as .nrrd. (The default in add data dialog box is volume, so I manually change them to segmentations, one by one. (So they load as segmentations)

Then I have to convert them to models. Here also I either do it one by one in the data module by right click, convert to models or in segmentations module by exporting to models.

Is there an efficient way to do this, without going one by one.

1. Load as segmentations (When default is volume)
2. Export to models

Thanks

---

<div class="post-metadata">

**Author:** ![Andinet\_Enquobahrie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/andinet_enquobahrie/32/75_2.png) [@Andinet\_Enquobahrie](https://discourse.slicer.org/u/Andinet_Enquobahrie)\
**Post date:** [March 21, 2021, 12:33pm UTC](https://discourse.slicer.org/t/batch-conversion-of-nrrd-to-models/16663/2 "2021-03-21T12:33:41Z")

</div>

> [@Selva](#):
>
> 1. Load as segmentations (When default is volume)
> 2. Export to models

You can write a short python script to do the above

```auto
segmentationNode = slicer.util.loadSegmentation(YOUR_FILE_NAME) # load the data as a 

# Export segmentation to model
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
exportFolderItemId = shNode.CreateFolderItem(shNode.GetSceneItemID(), "Segments")
slicer.modules.segmentations.logic().ExportAllSegmentsToModels(segmentationNode, exportFolderItemId)

```

There are more examples you can take a look at in the [python Script Repository.](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository)

-Andinet

---

<div class="post-metadata">

**Author:** ![Selva](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Selva](https://discourse.slicer.org/u/Selva)\
**Post date:** [March 21, 2021, 1:08pm UTC](https://discourse.slicer.org/t/batch-conversion-of-nrrd-to-models/16663/3 "2021-03-21T13:08:05Z")

</div>

Thanks @Andinet_Enquobahrie

Tried , but does not load as a batch, does it?  
I ve got 100s of.nrrd files in one folder, wanting them to load/ change at once.

Thanks

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [March 21, 2021, 3:24pm UTC](https://discourse.slicer.org/t/batch-conversion-of-nrrd-to-models/16663/4 "2021-03-21T15:24:55Z")

</div>

If you combine the code provided by @Andinet_Enquobahrie with a [for loop that traverses all files in a directory](https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory) then you’ll end up with a script that does just what you want.

---

<div class="post-metadata">

**Author:** ![Selva](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Selva](https://discourse.slicer.org/u/Selva)\
**Post date:** [March 22, 2021, 3:27am UTC](https://discourse.slicer.org/t/batch-conversion-of-nrrd-to-models/16663/5 "2021-03-22T03:27:04Z")

</div>

Thank you very much, @cpinter and @Andinet_Enquobahrie

I tried learning that, doing a loop

Sorry I am a medical person, couldn’t get to doing a loop.

Thanks
