Batch script using ModelMaker

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:

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.

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

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.