# Batch script using ModelMaker

**URL:** <https://discourse.slicer.org/t/batch-script-using-modelmaker/9422>\
**Category:** Support\
**Created:** [December 7, 2019, 10:43am UTC](https://discourse.slicer.org/t/batch-script-using-modelmaker/9422 "2019-12-07T10:43:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ButuiHu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/butuihu/32/2386_2.png) [@ButuiHu](https://discourse.slicer.org/u/ButuiHu)\
**Post date:** [December 7, 2019, 10:43am UTC](https://discourse.slicer.org/t/batch-script-using-modelmaker/9422/1 "2019-12-07T10:43:48Z")

</div>

I have a lot of label map volume, and I would like to convert all to poly data vtk files. After googling, ITK-SNAP could help to export to vtk file, but could not run in batch script mode. 3D slicer’s ModelMaker could also do this, and it’s possible run a script. For now, what I have done:

```python
for seg_filename in seg_filenames:
    seg_nodename = seg_filename.split('/')[-1].split('.')[0]
    img_node = slicer.util.loadVolume(img_filename)
    label_node = slicer.util.loadLabelVolume(seg_filename)
    parameters = dict()
    parameters['InputVolume'] = label_node.GetID()
    result_model = slicer.vtkMRMLModelHierarchyNode()
    result_model.SetName(seg_nodename)
    slicer.mrmlScene.AddNode(result_model)
    parameters['ModelSceneFile'] = result_model.GetID()
    parameters['Name'] = seg_nodename
    modelmaker = slicer.modules.modelmaker
    slicer.cli.run(modelmaker, None, parameters)

```

I’m stuck at how to save the result. I can use `slicer.saveNode(node, filename, properties={})` to save a single node, but I don’t what to pass to `node`.  
It would be great how ModelMaker converts volume to model using VTK or ITK internally, then I could write python script using VTK or ITK directly.

---

<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 7, 2019, 12:24pm UTC](https://discourse.slicer.org/t/batch-script-using-modelmaker/9422/2 "2019-12-07T12:24:23Z")

</div>

Probably the simplest is to export using segmentation module’s export to files feature. Set `inputLabelmapFile` and `outputFolder` and run this code:

```python
segmentationNode = slicer.util.loadSegmentation(inputLabelmapFile)
slicer.modules.segmentations.logic().ExportSegmentsClosedSurfaceRepresentationToFiles(outputFolder, segmentationNode)

```

This code works on recent Slicer Preview Releases. On latest Stable Releases this would be a few lines longer: labelmap need to be loaded as volume node and that has to be imported to segmentation node.
