Hello,
I am running a python script to slice an .stl file into a tif stack as per previous forum posts and the script repo.
I have been having issues with certain stl files causing slicer to crash.
Slicer will exit/crash when the script attempts to import the model to a segmentation node.
There are no exit/crash/error warnings and the log file contains nothing after the import line from the script.
The stl i am attempting to slice is just a 200 x 200 x 200 mm cube that has been created in autodesk inventor and converted to an stl, It has got a file size of just 3 kb.
Is there a limit to the model size or complexity of the stl file that slicer can handle?
I have allocated 100Gb of virtual memory as well as having 32Gb of physical memory.
I don’t understand why it is failing, any help would be much appreciated!
Thank you!
The script i am using is as follows:
import math
import numpy as np
import sys
np.set_printoptions(threshold=sys.maxsize)
inputModelFile = “C:\Users\User\Documents\Slicer\test area.stl”
outputDir = “C:\Users\User\Documents\Slicer\Tiff”
outputVolumeLabelValue = 255
outputVolumeSpacingMm = [0.0635, 0.0635, 0.1] #mm steps/resolution 400dpi =15.748dot/mm =0.0635
outputVolumeMarginMm = [0, 0, 0] #mm of buffer before/after and around
Read model
inputModel = slicer.util.loadModel(inputModelFile)
Determine output volume geometry and create a corresponding reference volume
bounds = np.zeros(6)
inputModel.GetBounds(bounds)
imageData = vtk.vtkImageData()
imageSize= [4724,4724,400]
imageOrigin = [ bounds[axis*2]-outputVolumeMarginMm[axis] for axis in range(3) ]
imageData.SetDimensions(imageSize)
imageData.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
imageData.GetPointData().GetScalars().Fill(0)
referenceVolumeNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLScalarVolumeNode”)
referenceVolumeNode.SetOrigin(imageOrigin)
referenceVolumeNode.SetSpacing(outputVolumeSpacingMm)
referenceVolumeNode.SetAndObserveImageData(imageData)
referenceVolumeNode.CreateDefaultDisplayNodes()
Convert model to labelmap ( 3d scalar volume node.)
seg = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLSegmentationNode’)
seg.SetReferenceImageGeometryParameterFromVolumeNode(referenceVolumeNode)
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(inputModel, seg)
seg.CreateBinaryLabelmapRepresentation()
outputLabelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLLabelMapVolumeNode’)
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(seg, outputLabelmapVolumeNode, referenceVolumeNode)
outputLabelmapVolumeArray = (slicer.util.arrayFromVolume(outputLabelmapVolumeNode) * outputVolumeLabelValue).astype(‘uint8’) #voxel array as numpy array (‘uint8’)
final=np.invert(outputLabelmapVolumeArray)
finaltest = np.where(final > 100, 0, 1)
Write labelmap volume to series of TIFF files
pip_install(“imageio”)
import imageio
for i in range(len(outputLabelmapVolumeArray)):
imageio.imwrite(f’{outputDir}/image_Page{i:03}_Clr1.tif’, final[i])
It crashes at the line:
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(inputModel, seg)