Difference between revisions of "Documentation/Labs/Slicer5MigrationGuide"

From Slicer Wiki
Jump to: navigation, search
(Created page with "This page lists non-backward compatible changes in Slicer API between 4.8 and 5.0 version. === slicer.util.getNode now raises exception if no node is found === slicer.util.g...")
 
 
Line 1: Line 1:
−
This page lists non-backward compatible changes in Slicer API between 4.8 and 5.0 version.
+
#REDIRECT [[Documentation/Nightly/Developers/Tutorials/MigrationGuide/Slicer]]
−
 
 
−
=== slicer.util.getNode now raises exception if no node is found ===
 
−
 
 
−
slicer.util.getNode() now to raises a MRMLNodeNotFoundException exception (instead of simply returning None), when the node is not found.
 
−
This makes code debugging easier, and in general more consistent with Python conventions.
 
−
 
 
−
How to update existing code:
 
−
 
 
−
It is advisable to only use slicer.util.getNode in tests, or interactively in the Python console, as its behavior is somewhat unpredictable (it may either found a node by name or ID, and result of wildcard search is even less deterministic). In all other cases, it is recommended to use the Scene's methods
 
−
 
 
−
==== Old code ====
 
−
 
 
−
<pre>
 
−
n = slicer.util.getNode(nodeNameOrID)
 
−
</pre>
 
−
 
 
−
==== New code ====
 
−
 
 
−
If node is to be found by name:
 
−
<pre>
 
−
  n = slicer.mrmlScene.GetFirstNodeByName(nodeName)
 
−
</pre>
 
−
 
 
−
If node is to be found by ID:
 
−
<pre>
 
−
  n = slicer.mrmlScene.GetNodeByID(nodeID)
 
−
</pre>
 
−
 
 
−
The following is only recommended in tests, not in module code (as recommended alternatives above are faster and more predictable):
 
−
 
 
−
<pre>
 
−
try:
 
−
  n = slicer.util.getNode(nodeNameOrID)
 
−
except slicer.util.MRMLNodeNotFoundException:
 
−
  n = None
 
−
</pre>
 
−
 
 
−
More information: https://github.com/Slicer/Slicer/commit/b63484af1b1b413f35396f8f7efb73e870448bd4
 

Latest revision as of 14:06, 19 January 2018

Home < Documentation < Labs < Slicer5MigrationGuide