Calling C++ from Python

Hi Andras,
is it possible to call external c++ customised functions using slicer python.

(I moved this to a new topic – please start a new topic for new questions, rather than replying to the old one)

Yes. Most of Slicer core is implemented in C++, so you usually call C++ functions from Slicer’s Python interpreter. If you implement a C++ loadable module then you can call the methods implemented in them, too.

how to call any examples available

For example, MRML nodes are implemented in C++. You can call their methods as you would do it for any native Python class:

node = slicer.mrmlScene.GetFirstNodeByName("Red")
print(node.GetName())
1 Like

Hi Andras,
If I have an external implementation of an algortihm in c++ and want to call it from python. how to do that. the implementation is outside the slicer enviornment but is in c++

The easiest is to create a CLI module in C++ and call that from Python. First (and hardest) step is to build Slicer, then follow Slicer developer tutorials.

If you already have another executable that reads and writes Slicer compatible data like nifti files, and if you want to avoid having to build slicer from source, then another option is to make a wrapper so that the other program looks like a Slicer CLI.

Here’s an example:

Note that if the other program uses shared libraries or python packages you may need to manipulate the environment’s path variables to make sure the program resolves correctly.

1 Like

Hi Andras,
Can you please post the tutorials. I build the slicer previously. when I run slicer.exe it opens up slicer but it also show me text below: what does this mean? is it not successfully build.

Hi Andras,
Can you please refer to a good and easy to understand example for creating a CLI module in c++ and call from python.

It seems that the build partially failed. I would recommend to build latest master version of Slicer (it’ll become stable within a few weeks) and following all recommendations in build instructions. For example, build might have failed because the build folder name is too long. It has to be really short, up to a few characters - for example C:\S4 for source and C:\S4D / C:\S4R for debug/release mode binary works.

These should help:

Hi peiper,
I would need to know how to make a wrapper for externall c++ code. Could be please refer me to any good websites or examples.

Thank you

The typical way is to use something like SWIG, as done in ITK.

Or you could do something in python.

Or as Andras points out above, pretty much any C++ code in Slicer that extends VTK or Qt gets wrapped for python automatically.

Hope that helps.

is boost.python ok to use instead of swig or are there any limitations.

I believe anything that works for normal python would be okay. I’ve never tried boost.python but I suppose it would work. Just keep in mind that if you plan to redistribute your code there can be lots of issues making sure the libraries are all packaged and (on mac) fixed up correctly. The safest course is usually just to follow the exact pattern of something that is already maintained and known to work.

Is there a specific reason you would not like to implement a Slicer CLI module? You can run it in Slicer using GUI and from Python and C++, or you can even using without Slicer, just running it as a standalone executable from the command line.

Hi Andras,
I could not find good documentation for CLI modules. Although there are good documentations and presentation for scripting modules but I need some good documentation for working on CLI modules. I am a just a beginner.

Could you please provide me with helpful documentation?

Regards,
Saima Safdar

Would you like to implement CLI modules or run them from Python? Would you like to implement in CLI module in Python or in C++?

I have an external c++ project. I need an interface to interact through slicer python with that project without disturbing the c++ project code itself.

Any suggestions please? Sorry if I confuse you with what I want to explain. Previously you told to make c++ loadable modules and then call it though slicer python.

then you suggested to build CLI module in c++ and call it from python. I am confused with what to follow.

I will be using slicer extensions as well the segment mesher and skull stipper. Along with that I need an interface to call the external c++ project.

Implement a CLI module in C++ is probably the best way to go, as it is simpler, processing can run in the background, etc. I would only recommend implementing a loadable module instead, if you need to re-run the processing very frequently (many times per second) and so passing data to the CLI would become the bottleneck; or you need to pass many or very complex data structures that are not readily supported by the CLI interface.

1 Like