chir.set
(SET)
February 18, 2024, 9:16pm
1
Hi,
From my understanding, AddNewNodeByClass accepts a basename that would increment with a suffix on repeat calls. For example:
slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode", "M")
should yield model nodes with names M, M_1, M_2…
I could not observe this naming pattern.
I’m probably missing something here. Can someone point to the right usage for this expectation?
Thank you.
AddNewNodeByClass
accepts a name that is set to the node, but sets it exactly as specified. If you’d like to generate a unique name then utilize slicer.mrmlScene.GenerateUniqueName("…")
as part of the string you pass into AddNewNodeByClass
. If any documentation is incorrect or unclear, then I’m sure improvements to the API documentation would be welcome.
chir.set
(SET)
February 18, 2024, 9:52pm
3
vtkMRMLScene.h has this note:
/// \note Instead of calling SetName() after creating the node, prefer
/// passing \a nodeBaseName, indeed the method AddNode() ensures that the
/// final node name is unique in the scene by appending a suffix if needed.
It may mean that the unique name is handled by ‘AddNode’, itself called by ‘AddNewNodeByClass’.
Anyway, it’s easy to use ‘GenerateUniqueName’.
Thank you.
For future reference, there was an attempt made at the following that was ultimately abandoned.
main
← jamesobutler:add-new-node-basename-increment
opened 05:16PM - 20 May 25 UTC
As originally pointed out in https://discourse.slicer.org/t/addnewnodebyclass-ba… sename-does-not-increment/34386.
This logic setting the name in `AddNewNodeByClass` has seemingly been the same since it was introduced in https://github.com/Slicer/Slicer/commit/31551523b242a03c0f8f5fa17999f0eb7b7cd360. This PR has been made under the assumption that the input argument of "nodeBaseName" means that the intention is for the specified name to serve as a base and increment to provide a unique name.
https://github.com/Slicer/Slicer/blob/673ddca536801eedfdc3287dbc6ab110c494382c/Libs/MRML/Core/vtkMRMLScene.h#L211-L214
It appears that developers have worked around this issue of the name not incrementing by calling `GenerateUniqueName` manually.
https://github.com/Slicer/Slicer/blob/673ddca536801eedfdc3287dbc6ab110c494382c/Modules/Scripted/DICOMPlugins/DICOMScalarVolumePlugin.py#L460-L464
https://github.com/Slicer/Slicer/blob/673ddca536801eedfdc3287dbc6ab110c494382c/Modules/Scripted/Endoscopy/Endoscopy.py#L878-L881
However in some places it is unclear if the specified `nodeBaseName` has instead been used as a `nodeName` with expectation that it would not increment and be used exactly as specified. If this is the desire then we could instead change the `AddNewNodeByClass` method to name the input argument `nodeName` instead of `nodeBaseName`.
https://github.com/Slicer/Slicer/blob/673ddca536801eedfdc3287dbc6ab110c494382c/Docs/developer_guide/script_repository/markups.md?plain=1#L687-L689
https://github.com/Slicer/Slicer/blob/673ddca536801eedfdc3287dbc6ab110c494382c/Modules/Loadable/Markups/MRML/vtkMRMLMarkupsJsonStorageNode.cxx#L168-L177