Make use of the 2 new states in the CLI node status ("Cancelling" and "Completing"). This gives the ability to scripted python modules to run CLIs without having to wait on them, option to cancel the cli, and most importantly, to be notified when the CLI is completed AND the outputs are loaded back into the scene.
The example below automatically assigns a color node to the generated output:
486 def runMergeLabels(self, run):
487 if run:
488 cliNode = self.getCLINode(slicer.modules.changelabel)
489 parameters = self.mergeLabelsParameters()
490 self.get('MergeLabelsApplyPushButton').setChecked(True)
491 self.addObserver(cliNode, self.StatusModifiedEvent, self.onMergeLabelsCLIModified)
492 cliNode = slicer.cli.run(slicer.modules.changelabel, cliNode, parameters, wait_for_completion = False)
493 else:
494 cliNode = self.observer(self.StatusModifiedEvent, self.onMergeLabelsCLIModified)
495 self.get('MergeLabelsApplyPushButton').setEnabled(False)
496 cliNode.Cancel()
497
498 def onMergeLabelsCLIModified(self, cliNode, event):
499 if cliNode.GetStatusString() == 'Completed':
500 # apply label map
501 newNode = self.get('MergeLabelsOutputNodeComboBox').currentNode()
502 colorNode = self.get('LabelmapColorNodeComboBox').currentNode()
503 if newNode != None and colorNode != None:
504 newNode.GetDisplayNode().SetAndObserveColorNodeID(colorNode.GetID())
505
506 if not cliNode.IsBusy():
507 self.get('MergeLabelsApplyPushButton').setChecked(False)
508 self.get('MergeLabelsApplyPushButton').setEnabled(True)
509 print 'MergeLabels %s' % cliNode.GetStatusString()
510 self.removeObservers(self.onMergeLabelsCLIModified)
Migrated from https://app.assembla.com/spaces/slicerrt/tickets/281-change-self-test-to-use-cli-modules-without-the-wait-cycles/details