Calling Python from C++

There are several options for calling Python from C++ (one of them is shown in my comment above, using evalScript) and it is done at many places in Slicer. However, C++ modules provide low-level infrastructure, so it would not make sense for a C++ module to depend on Python.

If you want to implement some performance-critical processing in C++ but other features are in Python then I would recommend to:

  • create a hidden C++ loadable module that just implements performance-critical processing in its logic classes: these classes are all Python-wrapped and so can be used from either C++ of Python modules
  • create Python-scripted module that uses your C++ module’s logic and any Python packages (scipy, etc.)

Also note that VTK, ITK, and other libraries bundled with Slicer already provide many features of scipy, so you may remove the need to call Python from C++ by using those libraries instead.

1 Like