Save Scenes in the same file

Hi to everyone, me and my team are trying to save the scene in the same directory where we have saved the scene before.

    def saveScene(self):    
        """
        Saves the current MRML scene to the specified directory.
    
        This method performs the following actions:
        - Displays a message indicating that the scene is being saved.
        - Saves the MRML scene to the directory specified by `self.saveResults_path` under the '2_Slicer' subdirectory.

        """
        slicer.app.processEvents()
        scenePath = os.path.join(self.saveResults_path, '2_Slicer')
        # Verify if the path exist
        if os.path.exists(scenePath):
            # Remove the path
            shutil.rmtree(scenePath)
            # Create the path
            os.makedirs(scenePath)
        else:
            print(f'El directorio {scenePath} no existe.')
        # Save the scene:
        slicer.mrmlScene.SaveScene(scenePath)     

The problem is that slicer.mrmlScene.SaveScene() needs empty directories to save properly the data. In case the directory is not empty, the slicer.mrmlScene.SaveScene() apparently removes the files but don’t save anything.
To satisfy this requirement we tried to remove the files before saving but it does not work…

Literally we are trying to emulate the slicer interface when it asks if we want to override the files and clicks in ‘Yes to all’. First:

Captura de pantalla 2024-10-08 134502

, to check all the nodes and data. And second:

Captura de pantalla 2024-10-08 134522

, to override all the files needed.

My current Slicer version is : 5.7.0 -2024-10-05
My OS version is: Windows 11

Thanks a lot :slight_smile:

Have you checked the slicer.mrmlScene.Commit function?

1 Like

Hi @cpinter, yes we tried it, but this function (as far as we know) only saves the scene, not all the data contained in the nodes ( for example, centerline curve JSON, volume nodes NRRD, segmentations…)

slicer.mrmlScene.Commit() needs any specific args to override and save all the data?

Hi all,

Indeed, we are facing problems with slicer.mrmlScene.Commit, slicer.mrmlScene.SaveSceneToSlicerDataBundleDirectory, and slicer.mrmlScene.SaveScene().

It would be very useful to replicate the behavior of the Ctrl+S shortcut with an overwrite function. Otherwise, we always need to create a new directory to save scenes, unnecessarily increasing the number of files and memory usage.

Does anyone have any ideas on how to achieve this?