Hello,
I’m new to creating modules and my programming skills are rather basic, so I encountered problems in loading segmentation from subdirectories, and in exporting and saving the mesh models.
The first issue looks like following: I want to load segmentations named ‘_repair.seg.nrrd’ from multiple subdirectories. What I got so far is:
TypeError: ExportAllSegmentsToModelHierarchy argument 1: method requires a VTK object.
I would be really grateful for your help, as I lack knowledge to fix this (probably really trivial) problem. I have a next piece of code that looks as below. However, I don’t even know if it works, because of the previous error I mentioned.
# Make sure surface mesh cells are consistently oriented
surfaceMesh = MasterModelNode.GetClosedSurfaceRepresentation(segmentID)
normals = vtk.vtkPolyDataNormals()
normals.AutoOrientNormalsOn()
normals.ConsistencyOn()
normals.SetInputData(surfaceMesh)
normals.Update()
surfaceMesh = normals.GetOutput()
# Save as PLY file
writer = vtk.vtkSTLWriter()
writer.SetInputData(surfaceMesh)
#### name should be: name of segmentation .ply
file_new = dir_path + "/" + filename + "/" + whichOP + "/CT/" + filename + segmentName + ".ply"
writer.SetFileName(file_new) # set file name
writer.Update()
# Clean up
segmentEditorWidget = None
slicer.mrmlScene.RemoveNode(file_complete)
slicer.mrmlScene.RemoveNode(segmentEditorNode)
Yes, that line is incorrect. If returnNode=True is specified then slicer.util.loadNodeFromFile returns (success, MasterModelNode) pair and not just MasterModelNode.
So I used slicer.util.loadSegmentation(filename, returnNode=False) as you suggested but the error remains - ‘method requires a VTK object’.
Also I need to export the mesh surface as .ply file.
Is it possible that I miss a line about a node, like getNode(‘Segmentation’)?
Best!
What error, where? What command do you type exactly?
If you need .ply export you cannot use the direct export feature but you need to use VTK writers. You can add a feature request to the Slicer issue tracker to implement .ply format export.
In cases like this you need to confirm line-by-line that you are getting the data you expect from the API, for example by adding print statements or executing the calls in the python console.
Here, you have the returnNode=False argument to the loadSegmentation call, but then try to use the MasterModelNode as a node, when it’s a boolean.