Documentation/Labs/Slicer5MigrationGuide

From Slicer Wiki
Revision as of 05:44, 19 January 2018 by Lasso (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Home < Documentation < Labs < Slicer5MigrationGuide

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

n = slicer.util.getNode(nodeNameOrID)

New code

If node is to be found by name:

  n = slicer.mrmlScene.GetFirstNodeByName(nodeName)

If node is to be found by ID:

  n = slicer.mrmlScene.GetNodeByID(nodeID)

The following is only recommended in tests, not in module code (as recommended alternatives above are faster and more predictable):

try:
  n = slicer.util.getNode(nodeNameOrID)
except slicer.util.MRMLNodeNotFoundException:
  n = None

More information: https://github.com/Slicer/Slicer/commit/b63484af1b1b413f35396f8f7efb73e870448bd4