Anywhere to find a list of required inputs for Python?

Hello,

I am trying to write a Python script that can automate the export of a .stl model from a segmentation that is imported with the CT DICOM set.

I have pretty much everything by painfully looking through examples online, but I am stuck on how to do the final export.

I think this might be the code to use:

slicer.modules.models.logic().SaveModel()

But that code requires two arguments and I have no way of figuring out what they are supposed to be. This is an issue I have run into with many other commands. Is there some database I can access that says what all the arguments for a code are supposed to be?

All i get back in the traceback error is:

TypeError: SaveModel() takes exactly 2 arguments (0 given)

Which is next to useless as it doesnā€™t tell me what the arguments need to be.

You can use the help command to get documentation on any command. For example, enter this in the Python console:

help(slicer.modules.models.logic().SaveModel)

You can find full documentation of Slicerā€™s Python-wrapped C++ API at http://apidocs.slicer.org. Models module logic is documented here: http://apidocs.slicer.org/master/classvtkSlicerModelsLogic.html#ae7123e56a176f06e0eda54403f07f393

Thanks.

Iā€™m having difficulty finding the vtkMRMLModelNode for the model I want to save. Is there a way to list all available?

Also will the SaveModel() save as .stl format?

Itā€™s very confusing to me what stuff I have to use slicer.modules.models for and what stuff I have to use slicer.vtkMRMLModelNode() for

Get list of all model nodes in the scene: use slicer.util.getNodesByClass. For example:

models=slicer.util.getNodesByClass('vtkMRMLModelNode')
for model in models:   
  if not model.GetHideFromEditors():
    print(model.GetName())

Save model as STL:

slicer.util.saveNode(modelNode, "c:/tmp/test.stl")

Model node only stores the data, so youā€™ll only find methods in this class that are responsible getting/setting content. Any operation on the data is implemented in Models or other module logic classes or in utility functions.

Once again, Thanks so much for your help.

Sorry to be a bother, I hope this is the last I have to ask for help.
So I am trying to automate the step to export all segmentations as model that I can then save using the code you provided above.

So I have these segmentations. The Binary labelmap is currently master:

Then I have the following code to try to export them to models

>>>segmentations = slicer.util.getNodesByClass(ā€˜vtkMRMLSegmentationNodeā€™)
>>>modelHierarchyNode = slicer.vtkMRMLModelHierarchyNode()
>>>slicer.mrmlScene.AddNode(modelHierarchyNode)
>>>slicer.modules.segmentations.logic().ExportAllSegmentsToModelHierarchy(segmentations[0],slicer.util.getNodesByClass(ā€˜vtkMRMLModelHierarchyNodeā€™)[0])

After the final line, the console returns ā€œTrueā€ but no models appear, just this:

i am sure there is one step I am missingā€¦

Bump. Please see above. Iā€™ve tried trouble shooting and Iā€™m not sure where I am going wrong.

The ExportAllSegmentsToModelHierarchy() returns ā€œTrueā€, but no models appear under the ModelHierarchy node.

Thanks for the feedback. Youā€™ve discovered a bug in ExportAllSegmentsToModelHierarchy method that Iā€™ll fix in the nightly build today. Till then, or if you use the latest stable version, you can use ExportSegmentsToModelHierarchy method instead:

seg = getNode('Segmentation')
modelHierarchyNodeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelHierarchyNode')
segmentIds = vtk.vtkStringArray()
seg.GetSegmentation().GetSegmentIDs(segmentIds)
slicer.modules.segmentations.logic().ExportSegmentsToModelHierarchy(seg, segmentIds, modelHierarchyNodeNode)
1 Like