# AddNewNodeByClass: basename does not increment

**URL:** <https://discourse.slicer.org/t/addnewnodebyclass-basename-does-not-increment/34386>\
**Category:** Feature requests\
**Created:** [February 18, 2024, 9:16pm UTC](https://discourse.slicer.org/t/addnewnodebyclass-basename-does-not-increment/34386 "2024-02-18T21:16:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [February 18, 2024, 9:16pm UTC](https://discourse.slicer.org/t/addnewnodebyclass-basename-does-not-increment/34386/1 "2024-02-18T21:16:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [February 18, 2024, 9:32pm UTC](https://discourse.slicer.org/t/addnewnodebyclass-basename-does-not-increment/34386/2 "2024-02-18T21:32:28Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [February 18, 2024, 9:52pm UTC](https://discourse.slicer.org/t/addnewnodebyclass-basename-does-not-increment/34386/3 "2024-02-18T21:52:50Z")

</div>

vtkMRMLScene.h has this note:

```auto
/// \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.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [May 27, 2025, 11:52am UTC](https://discourse.slicer.org/t/addnewnodebyclass-basename-does-not-increment/34386/4 "2025-05-27T11:52:01Z")

</div>

For future reference, there was an attempt made at the following that was ultimately abandoned.

> <https://github.com/Slicer/Slicer/pull/8434>
>
> 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
