Create a vtkMRMLModelNode from a polyData

Operating system: macOS 10.146
Slicer version: 4.10.2

Hello,

I want to create a vtkMRMLModelNode and display it in slicer, i did it like this :

vtkNew<vtkMRMLModelNode> mesh;
mesh->SetAndObservePolyData(poly);
mesh->CreateDefaultDisplayNodes();
mesh->SetName(nameOfNode.c_str());
this->mrmlScene()->AddNode(mesh.GetPointer());

Blockquote vtkMRMLModelNode::CreateDefaultDisplayNodes failed: scene is invalid
error: [/RealTimeAblation/Slicer/Slicer-SuperBuild/Slicer-build/bin/Slicer.app/Contents/MacOS/./Slicer] exit abnormally - Report the problem.
09:49:38: /Slicer/Slicer-SuperBuild/Slicer-build/Slicer exited with code 1

Can you help me to solve this problem, please ?

Thanks for your help.

The warning is because you need to add the model node to scene before calling CreateDisplayNodes (because display nodes must be added to the same scene).

The crash is probably because scene is nullptr.

You can simplify things a bit, too:

vtkMRMLModelNode* mesh = this->mrmlScene()->AddNewNodeByClass("vtkMRMLModelNode", nameOfNode.c_str());
mesh->SetAndObservePolyData(poly);
mesh->CreateDefaultDisplayNodes();
2 Likes

Thank you so much, this solution works very well.