View Issue Details

IDProjectCategoryView StatusLast Update
0002479Slicer4Core: Scripting (Wrapping, Python)public2018-03-02 11:07
Reportersunilk Assigned Tolassoan  
PrioritynormalSeverityfeatureReproducibilityalways
Status closedResolutionfixed 
Product VersionSlicer 4.1.1 
Target VersionFixed in VersionSlicer 4.6.0 
Summary0002479: Get storable nodes w/o invoking "Save" dialog.
Description

Per the following dev thread:

Hi Kumar,

Your observation is correct, sometimes storable nodes (e.g. model nodes) don't have a storage node until the Save Data dialog is opened.
This can happen when a node is generated programatically (e.g. created from a CLI execution).
For this reason, the Save Data dialog method "QFileInfo qSlicerSaveDataDialogPrivate::nodeFileInfo(vtkMRMLStorableNode* node)" ensures that a default storage node is created.
If you need to access this function from your code, maybe it should be moved into vtkMRMLScene as a utility method:

You would need to:

  • Create a mantis issue
  • Create vtkMRMLScene::CreateMissingStorageNodes( / some options here ? /); where you move some the generic content of nodeFileInfo
  • Create vtkMRMLScene::CreateStorageNode(vtkMRMLStorableNode storableNode /, some options here ?*/ );
  • Call vtkMRMLScene::CreateStorageNode(...) from qSlicerSaveDataDialogPrivate::nodeFileInfo()
  • Write a unit test in vtkMRMLSceneMissingStorageNodeTest.cxx

I'll be happy to help you and review your code.

Hth,
Julien.

On Thu, Aug 16, 2012 at 12:34 PM, Kumar, Sunil <kumars@mir.wustl.edu> wrote:
Hello,

I’m trying to inventory all newly created nodes using Python. Here’s a simple workflow:

1) Open a scene built from a .raw stack
2) Create a model via a label map.
3) Do a print of the scene’s nodes that have file names. Code below. The model file names (.vtk and .nrrd) are not outputted.
nodes = slicer.app.mrmlScene().GetNodes()
for i in range(0,nodes.GetNumberOfItems()):
node = nodes.GetItemAsObject(i)
try:
fn = node.GetFileName()
if fn: print (node.GetFileName())
except:
pass
4) Open the “Save” dialog and cancel out of it (no saving).
5) Repeat step 3 and the model file names are there.

Is there a command in Python that can refresh the nodes without me having to open the “Save“ dialog?

Thanks in advance,
Sunil

Tagshelp-wanted

Activities

user349

2012-09-06 14:04

  ~0006000

Found a workaround after today's tcon.
Can cancel this request. Thanks.

jcfr

jcfr

2012-09-06 14:11

administrator   ~0006001

Would be great if you could post the workaround so that it is documented. Thanks

user349

2012-09-07 07:27

  ~0006006

See below. Thanks.

nodeList = slicer.app.mrmlScene().GetNodes()
for i in range(0,nodeList.GetNumberOfItems()):
node = nodeList.GetItemAsObject(i)
#

Isolates all nodes that are "storable" but not necessarily stored into a file yet.

This includes newly created nodes. When nodes are stored into a file they become "storage" nodes.

#
if node.IsA("vtkMRMLStorableNode"):
storableNode = slicer.vtkMRMLStorableNode.SafeDownCast(node)
if (storableNode and storableNode.GetSaveWithScene()):
storageNode = storableNode.GetStorageNode()
#

If there is no "storageNode" associated with the "storableNode,"

     # then create one and retrieve the prescribed file information.
     # Afterwards set the the file name to the storage node.
     #
     if not storageNode:
       storageNode = storableNode.CreateDefaultStorageNode()
       if storageNode:
         storageFileName = str(storableNode.GetName()) + '.' + storageNode.GetDefaultWriteFileExtension()
         storageNode.SetFileName(storageFileName)
         print (&quot;Newly created storageNode: '%s'&quot;%(storageFileName))
jcfr

jcfr

2012-12-08 10:19

administrator   ~0007476

Hi Sunil,

Did you have any chance to work on this ?

Thanks,
Jc

user349

2012-12-08 10:23

  ~0007477

Last edited: 2012-12-08 13:16

Hi Jc,

Yes. The note right above your question solves the problem...at least for my purposes.

Is that what you are referring to?

Thanks,
Sunil

jcfr

jcfr

2012-12-08 10:26

administrator   ~0007478

Hi,

Was referring to the suggestion reported by Julien in the issue description:

You would need to:

  • Create a mantis issue
  • Create vtkMRMLScene::CreateMissingStorageNodes( / some options here ? /); where you move some the generic content of nodeFileInfo
  • Create vtkMRMLScene::CreateStorageNode(vtkMRMLStorableNode storableNode /, some options here ?*/ );
  • Call vtkMRMLScene::CreateStorageNode(...) from qSlicerSaveDataDialogPrivate::nodeFileInfo()
  • Write a unit test in vtkMRMLSceneMissingStorageNodeTest.cxx

user349

2012-12-08 13:15

  ~0007480

Ah, gotcha.

No, I haven't. The Python workaround was sufficient for my purposes.

Thanks,
Sunil

jcfr

jcfr

2012-12-09 07:04

administrator   ~0007483

Thanks for clarifying :)

lassoan

lassoan

2016-08-31 11:44

developer   ~0014078

Added methods to add missing storage node in rev25326.

To add a default storage node to the scene for a single node:
someStorableNode.AddDefaultStorageNode()

To create all missing storage nodes and save the entire scene:
ioManager = slicer.app.ioManager()
ioManager.saveNodes("SceneFile", { "fileName": "savedscene.mrml" })

Issue History

Date Modified Username Field Change
2012-09-06 08:31 user349 New Issue
2012-09-06 08:31 user349 Status new => assigned
2012-09-06 08:31 user349 Assigned To => jcfr
2012-09-06 14:04 user349 Note Added: 0006000
2012-09-06 14:11 jcfr Note Added: 0006001
2012-09-07 07:27 user349 Note Added: 0006006
2012-12-08 10:19 jcfr Note Added: 0007476
2012-12-08 10:23 user349 Note Added: 0007477
2012-12-08 10:26 jcfr Note Added: 0007478
2012-12-08 13:15 user349 Note Added: 0007480
2012-12-08 13:16 user349 Note Edited: 0007477
2012-12-09 07:04 jcfr Target Version => Slicer 4.3.0
2012-12-09 07:04 jcfr Note Added: 0007483
2013-03-06 18:02 jcfr Tag Attached: I-want-to-contribute
2013-09-02 20:13 jcfr Reporter user349 => sunilk
2013-09-02 20:14 jcfr Target Version Slicer 4.3.0 => Slicer 4.4.0
2014-05-12 23:20 jcfr Tag Renamed I-want-to-contribute => help-wanted
2014-05-13 08:39 jcfr Status assigned => acknowledged
2014-05-13 08:39 jcfr Target Version Slicer 4.4.0 =>
2016-08-31 11:44 lassoan Note Added: 0014078
2016-08-31 11:44 lassoan Status acknowledged => resolved
2016-08-31 11:44 lassoan Fixed in Version => Slicer 4.6.0
2016-08-31 11:44 lassoan Resolution open => fixed
2016-08-31 11:44 lassoan Assigned To jcfr => lassoan
2018-03-02 11:07 jcfr Status resolved => closed