SliceViews - Propagate volume selection is not working?

Dear slicer community,

I’ve been used Slicer from quite a while, and I am currently writing a new module which uses some “old” functions I wrote time ago to automate some interaction with 3D Slicer.

In particular, I need to set the sliceViews to a specific volume I just pushed into 3D slicer. To do so, I used the following functions:

def get_slice_view(color_string):
    lm = slicer.app.layoutManager()
    selected_slice = lm.sliceWidget(color_string)
    return selected_slice

and

def set_volume_to_slice_view(
    volume_node, slice_node, layer="background", propagate=False
):
    """
    Assign a volumeNode to a specific view.

    :param volume_node: volume to assign to a specific view
    :param slice_node:  select the slice_node. Could be either the slice_node or the string
                        ['Red', 'Yellow', 'Green']
    :param layer: string specifying the layer where to put the volumeNode
                ['background', 'foreground', 'labelmap']
    :param layer:
    :param propagate:

    :return: True (success) or False (Failure)
    """
    if isinstance(slice_node, str):
        slice_node = get_slice_view(slice_node)

    try:
        logic = slice_node.sliceLogic()
        composite_node = logic.GetSliceCompositeNode()
    except AttributeError:
        slice_node = get_slice_view(slice_node.GetName())
        logic = slice_node.sliceLogic()
        composite_node = logic.GetSliceCompositeNode()

    if layer == "background":
        composite_node.SetBackgroundVolumeID(volume_node.GetID())
        if propagate:
            slicer.app.applicationLogic().PropagateBackgroundVolumeSelection(0)
        return True
    elif layer == "foreground":
        composite_node.SetForegroundVolumeID(volume_node.GetID())
        if propagate:
            slicer.app.applicationLogic().PropagateForegroundVolumeSelection(0)
        return True
    elif layer == "labelmap":
        composite_node.SetLabelVolumeID(volume_node.GetID())
        if propagate:
            slicer.app.applicationLogic().PropagateLabelVolumeSelection(0)
        return True
    else:
        logging.info("*** Error assigning volume to slice View ***")
        return False

I tested this same code, and it is working without the Propagate*VolumeSelection() (meaning the propagate flag in my function set to True).

I am wondering if I am using it wrong, it is just my problem or it is a function bug.
As I said, with propagate=False the function is working (however, only a single sliceView is changing), and I replicate the failure directly in the 3D Slicer python interactor.

I am using Slicer 5.2.2, on Windows platform.

Thanks in advance for any help you could provide.
Great job with the platform by the way!
Best Regards,
Davide

You might want to try:

1 Like

Thank you very much @pieper, it did exactly what I needed.
I did look into the utils module from the embedded interpreter, and missed that one.

Thanks again.
Davide