Create a basic Python-Wrapping for MITK using SWIG
Open, HighPublic

Description

Add the option to use SWIG to create bindings for common scripting languages. The idea behind this project is to build a infastructure for a Python-wrapping of the most basic MITK features using SWIG. This structure might then later be used to provide more complex wrapping or the wrapping of additional features.

goetzm created this task.Thu, Jan 11, 4:31 PM
goetzm triaged this task as High priority.

It is now theoretically possible to load the created module:

  1. Open a shell in the output folder (for windows: ../MITK_build/lib/Release)
  2. Append the PATH-Variables so that the created linking objects (For windows *.dll's) are found if necessary. For windows, this can be done by calling one of the starting scripts that are created by default.
  3. Start python and import the new module.

Right now there are some errors:

  • The name of the module is "_PythonMITK" - i don't know how to get rid of the underscore. But this is a minor problem.
  • Importing the library fails with the message: "ImportError: dynamic module does not define module export function (PyInit__PythonMITK). This is the current point of work.

Solved both problems:

  • The created project must have the same name in cmake and the corresponding .i - file.
  • A .py-wrapper is created in the folder MITK-build/Wrapping/Python (for windows). Copy this wrapper file to the library folder (Where the pyd -object is)
  • Execture steps 1-3 from above. The module can then be loaded without the the underscore, for now it is PythonMITK.

Updated current implemented. Successfully wrapped mitk::IOUtil , but there are still some bugfixes, workarounds. The most limiting so far are:

  • Saving is not possible, because mitk::IOUtil::Load () returns a Smartpointer, while mitk::IOUtil::Save expects a standard pointer.
  • The Include-Path for mitkIOUtil.h is hard coded right now. It is necessary to add this include-path to the swig-file. I added it as a hard-coded command as a workaround. Better solutions might be possible / need to be evaluated.

It is now possible to load and save a BaseData-object. This required the following changes:

  • Wrapping the mitk::BaseData : In Order to be able to handle these types of Data
  • Helping SWIG dealing with Vectors: IOUtil::Load returns a vector of BaseData. Therefore swig had to know how to handle those vectors. The way to do it is to include std_vector.i in the i-file. Then all instanciations of std::vector<a> needs to be initialized using a %template construction. Did this so far for BaseData and the standard c types.
  • Wrap itk::SmartPointer: While IOUtil::Load returns SmartPointers, IOUtil::Save requires Basic Pointer. It is therefore necessary to be able to handle the Smartpointer-Structure directly from python. Luckely, Swig has the ability to wrap Smartpointers rather easy. I.e. it detects overloaded -> Operators (as for our smartpointers) and wraps the functions calls directly so that there is nearly no difference between Natural Pointers and Smartpointers within the scripting language. It further allows to access all SmartPointer-methods. For more information refer to the links at the end.
  • mitkIOUtil, mitkBaseData, and itkSmartPointer do contain some macros. As only parts of the C++ code are evaluated in order to create the Scripting interface, it is neccesary to redefine these macros (either fully or as "dummy" version).

http://swig.10945.n7.nabble.com/Wrapping-a-custom-smart-pointer-td14899.html
http://www.swig.org/Doc3.0/SWIGDocumentation.html#SWIGPlus_smart_pointers
http://quantlib.org/reposit/docs/swig/SWIGDocumentation.html