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

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

    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??

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):

results in:

1 Like

I have already a hierarchy:

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

    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

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:

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?

Cloning a volume using CloneSubjectHierarchyItem creates a shallow copy of the image data. You can either use Volume module’s CloneVolume method or modify your code above to clone the image data by adding clonedNode.SetAndObserveImageData(vtk.vtkImageData()) before slicer.util.updateVolumeFromArray(clonedNode, vox_arr).

yes, that worked!
image

not trivial line BTW! Thanks

Yes, that’s why we added the clone volume example in the script repository.