I am trying to create a model in python. The model data, i.e. the raw vertices, vertex normals and triangles, will come from reading a non-standard file format in this extension. (Background: The extension will eventually be able to read data exported from an electroanatomic mapping system such as this one.)
I’ve found some guidance here and here, but I am stuck.
cubeVerts = [[1.0, 1.0, 1.0], [1.0, -1.0, 1.0], [-1.0, -1.0, 1.0], [-1.0, 1.0, 1.0], [1.0, 1.0, -1.0], [1.0, -1.0, -1.0], [-1.0, -1.0, -1.0], [-1.0, 1.0, -1.0]] cubeTris = [[0,1,2], [0,2,3], [0,1,5], [0,5,4], [0,4,7], [0,7,3], [3,2,6], [3,6,7], [1,5,7], [1,6,2], [4,5,6], [4,6,7]] modelNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode') modelPolyData = modelNode.GetPolyData() modelPolyData.SetVerts(cube)
But since the model created does not contain any points initially,
modelPolyData = modelNode.GetPolyData() return nothing and
modelPolyData.SetVerts fails with
AttributeError: 'NoneType' object has no attribute 'SetVerts'.
I considered using the approach in this example in the script repository and create a dummy sphere first which is then overwritten by the actual data, but here I ran into another problem:
vtkPolyData::SetVerts needs a vtkCellArray and I could not find any member function of this type that simply takes an array of point coordinates together with another array of connectivity information.
I even thought about simply writing out a .vtk file line by line from the python script and re-load it into Slicer, but I am sure there is a more elegant solution…
Any help would be appreciated.