# Is it possible to create/clone a node keeping the same location in the hierarchy of the father node?

**URL:** https://discourse.slicer.org/t/is-it-possible-to-create-clone-a-node-keeping-the-same-location-in-the-hierarchy-of-the-father-node/5540
**Category:** Support
**Tags:** subject-hierarchy
**Created:** [January 28, 2019, 1:56pm UTC](https://discourse.slicer.org/t/is-it-possible-to-create-clone-a-node-keeping-the-same-location-in-the-hierarchy-of-the-father-node/5540 "2019-01-28T13:56:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)
#### Post date: [January 28, 2019, 1:56pm UTC](https://discourse.slicer.org/t/is-it-possible-to-create-clone-a-node-keeping-the-same-location-in-the-hierarchy-of-the-father-node/5540/1 "2019-01-28T13:56:51Z")

</div>

Basically I have a big subject hierarchy (several folders), and inside each I have nodes (volumes, segmentations, transformations). I want to create a table for instance and place it in the same location of the generating data, or clone one of the volumes, make some calculation and save as a new volume in the same hierarchy.

When I do this

```python
    volumeNode = slicer.vtkSlicerVolumesLogic().CloneVolume(slicer.mrmlScene, fatherNode, nodename, True)
    util.updateVolumeFromArray(volumeNode, new_arr)
    volumeNode.Modified()

```

the created volume is outside of the hierarchy so I have to move it manually. Is there a way to tell the new volume to be placed where the father is located??

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [January 29, 2019, 1:16am UTC](https://discourse.slicer.org/t/is-it-possible-to-create-clone-a-node-keeping-the-same-location-in-the-hierarchy-of-the-father-node/5540/2 "2019-01-29T01:16:28Z")

</div>

This feature is already available in Data module. You can convert file folder hierarchy to subject hierarchy by clicking on the empty area of the subject hierarchy tree:

For example, you load a number of images from a folder structure (folder names a, b, c, d, e):

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/a/1a610a7d7f6055e21d5a704977efdb9d09531c6a.png)

results in:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/9/8979ee404fa0822539fca25a24caf01f02e00d0f.png)

---

<div class="post-metadata">

### Author: ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)
#### Post date: [January 29, 2019, 9:40am UTC](https://discourse.slicer.org/t/is-it-possible-to-create-clone-a-node-keeping-the-same-location-in-the-hierarchy-of-the-father-node/5540/3 "2019-01-29T09:40:44Z")

</div>

I have already a hierarchy:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/3/63065456cad1e7366b47b5c09966d867d160a30c.png)

what I need is to coy the object to perform some operations and keep bpoth the new and the old volumes, I am doing like this

```python
    shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
    itemID = shNode.GetItemByDataNode(node)
    newname = name.replace('CTCT', 'DENS')
    clonedItemID = slicer.modules.subjecthierarchy.logic().CloneSubjectHierarchyItem(shNode, itemID, newname)
    vox_arr = np.array(slicer.util.array(newname)).copy()
    vox_arr = np.where(vox_arr<0, 0.0009990791851848257 * vox_arr + 1, 0.0005116346986394071 * vox_arr + 1)
    clonedNode = shNode.GetItemDataNode(clonedItemID)
    slicer.util.updateVolumeFromArray(clonedNode, vox_arr)
    clonedNode.Modified()

```

But then when I visualize both images, THEY ARE BOTH CHANGED!!

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/7/f7cd365e9e2d463453ea5b0ac8493ad2eb595a45.png)

I need to preserve the hierarchy path, but also to create a new volume and modify it without changing the original. This code preserves the hierarchy, but modifies both volumes:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/5/755045aa642e0b4eb475619cfbe9cd105c321287.png)

the code in the original post creates a new volume you can modify safely without modifying the original but it does not preserve the hierarchy.

So how to do this?

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [January 29, 2019, 10:15am UTC](https://discourse.slicer.org/t/is-it-possible-to-create-clone-a-node-keeping-the-same-location-in-the-hierarchy-of-the-father-node/5540/4 "2019-01-29T10:15:34Z")

</div>

Cloning a volume using `CloneSubjectHierarchyItem` creates a shallow copy of the image data. You can either use Volume module’s [CloneVolume](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Clone_a_volume) method or modify your code above to clone the image data by adding `clonedNode.SetAndObserveImageData(vtk.vtkImageData())` before `slicer.util.updateVolumeFromArray(clonedNode, vox_arr)`.

---

<div class="post-metadata">

### Author: ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)
#### Post date: [January 29, 2019, 10:43am UTC](https://discourse.slicer.org/t/is-it-possible-to-create-clone-a-node-keeping-the-same-location-in-the-hierarchy-of-the-father-node/5540/5 "2019-01-29T10:43:18Z")

</div>

> [@lassoan](#):
>
> clonedNode.SetAndObserveImageData(vtk.vtkImageData())

yes, that worked!  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/a/8a2c7bb3cfb5c7552f91871dcd5b2626d6bda5d9.png)

not trivial line BTW! Thanks

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [January 30, 2019, 1:17am UTC](https://discourse.slicer.org/t/is-it-possible-to-create-clone-a-node-keeping-the-same-location-in-the-hierarchy-of-the-father-node/5540/6 "2019-01-30T01:17:14Z")

</div>

> [@Alex\_Vergara](#):
>
> not trivial line BTW

Yes, that’s why we added the [clone volume example](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Clone_a_volume) in the script repository.
