Use existing slicer modules with python and cli

I followed this tutorial on how to use existing modules in slicer through cli library:

It sounds not bad (considering my understanding in cli is close to 0…)
Anyway, since I want to use registration module (such as BRAINSfit) I’d like to have the option to cancel the operation after running it. but the cli cancel command is not implemented yet in the file that I’ve found.

so my questions:

  1. Is using slicer.cli is a good way execute existing modules proggramatically? If not- what is?
  2. How can I cancel the running process with cli?


Here is part of the code- the apply btton, executing the add Scalar Volume module (but can’t stop in the middle):

   def onApplyButton(self):

        # allow cancel the process
        if self.InProgress:
            self.InProgress = False
            self.abortRequested = True
            raise ValueError("User requested cancel.")
            # self.cliNode.cancel() # not implemented
            self.applyutton.text = "Cancelling..."
            self.applyButton.enabled = False

        self.InProgress = True
        self.applyButton.text = "Cancel"

        fixedImage  = self.fixedSelector.currentNode()
        movingImage = self.moveImgSelector.currentNode()
        outputImage = self.outputSelector.currentNode()

        if self.registratoinMethod == 'addScalarVolumes':
          moduleVar = slicer.modules.addscalarvolumes
          parameters = {}
          parameters["inputVolume1"] = fixedImage.GetID()
          parameters["inputVolume2"] = movingImage.GetID()
          parameters["outputVolume"] = outputImage.GetID()
          parameters["interpolation order"] = 0

        # get the module as object
        self.cliNode =,None, parameters)
        print self.cliNode.cancel.

        #           self.outputSelector.currentNode())

        # make the output volume appear in all the slice views
  1. Yes

  2. Did you try calling self.cliNode.Cancel()? (that should probably be added to

Hi, thanks! followed your suggestion- I did, but it didn’t work.
begginner question- maybe there is a way to access the cancel of the module itself? (the cancel button exist in some modules)

Are you sure it didn’t work? In your example code you have cancel but the method is Cancel with a capital C. Calling Cancel should be the same as clicking the Cancel button on CLI interface widget.

Thanks!! This fixed the problem!!
I changed it to:

if self.InProgress:
self.InProgress = False
self.abortRequested = True
self.applyButton.text = “Cancelling…”
self.applyButton.enabled = False
now it cancel the progress.

Could you help me with another question?

  1. Is there a way to make something happen after the process is over? like change the name of the botton after it finishes.

  2. How can I get through the cli the log, that is ussualy apear in the consol window while running the algorithm with the original loadable module?

I couldn’t find an explanation here:

Great - glad that worked.

If you observe events from the cliNode you can tell what state the process is in and react accordingly. You can review this documentation with the printStatus example:

Also to get the log info you can call cliNode.GetOutputText() and cliNode.GetErrorText()

(I just updated the wiki with this info).

1 Like