VTK/Python Wrapping FAQ: Difference between revisions

From KitwarePublic
< VTK
Jump to navigationJump to search
Line 6: Line 6:
=== Python Versions ===
=== Python Versions ===


VTK is regularly tested with Python 2.7 and 3.4, and should work with 2.5, 2.6, 2.7, and 3.2+.
The VTK master branch is regularly tested with Python 2.7 and 3.4, and VTK 7 should work with 2.5, 2.6, 2.7, and 3.2+.  Versions of VTK prior to 7.0 will only work with Python 2.


=== Organization ===
=== Organization ===

Revision as of 16:18, 12 February 2016

VTK Python Wrapping System

Big Picture

All the automated wrapping code generation relies on the consistent coding style followed in the VTK header files and the use of the convenience macros. Basically, a target language specific parser parses the header file figures out suitable wrapper functions in the desired target language and spits out the wrapper code that is used to generate the wrappings.

Python Versions

The VTK master branch is regularly tested with Python 2.7 and 3.4, and VTK 7 should work with 2.5, 2.6, 2.7, and 3.2+. Versions of VTK prior to 7.0 will only work with Python 2.

Organization

  • First look in VTK/Wrapping. Go over the files there. Specifically look at vtkWrapPython.c which actually does the code generation for Python.
  • When VTK is built, an executable called vtkWrapPython is built from this.
  • vtkWrapPython generates the Python wrappers for each wrappable VTK header and this is built to generate the Python wrapper.
  • The wrapper code makes use of Wrapping/Python/vtkPythonUtil.h and several other C and C++ files in Wrapping/Python for various things. Also note that Wrapping/hints is a mechanism for helping the wrapper code with, err, hints to help the wrapping process along. To see what the individual codes represent see Wrapping/vtkParse.y and vtkParse.tab.c
  • The VTK module for the vtk Python package is all in Wrapping/Python/vtk.

Documentation

The VTK module for the vtk Python package is all in Wrapping/Python/vtk. Individual classes can be inspected with the python "help()" function, e.g. help(vtk.vtkObject).

C++ that won't be wrapped

Information about what kinds of VTK class methods can or cannot be wrapped is provided by the README_WRAP.txt file in the VTK/Wrapping/Python directory. Recent progress on the wrappers is described on the python wrapper enhancements page.

CMake's role

Big Picture

I think all CMake does is to build the parser and organize the building of the generated code, link it correctly (which can be a bloody pain) etc. I don't think it directly does anything regarding the wrapper generation process. Any useful CMake macros are best gleaned by looking at CMake files (at least thats how I used to do it).

CMake Macros

1. vtk_wrap_python3(${NAME}Python KitPython_SRCS "${WRAP_LIST}")

Will wrap the c++ files in WRAP_LIST into python, outputting a list of transformed files into KitPython_SRCS.

2. add_library(${NAME}PythonD ${KitPython_SRCS} ${Kit_PYTHON_EXTRA_SRCS})

Will take those outputs, and some optional additional ones, and create a library.

3. python_add_module(${NAME}Python ${NAME}PythonInit.cxx)

Will produce a python module that links in the library so that the wrapped code is more easily callable via python.

That is what ParaView's WRAP_PLUGIN_FOR_PYTHON macro does anyway.

Excluding files from being wrapped

You can tell the wrapper to ignore certain files by using this in your CMakeLists.txt:

 set_source_file_properties(
   vtkMyUnwrappableClass.cxx
   WRAP_EXCLUDE
 )

For files that you want to be wrapped in python, but that cannot be wrapped in Tcl or Java, you can mark them for "special wrapping":

 set_source_file_properties(
   vtkMyClassWrappableInPython.cxx
   WRAP_EXCLUDE
 )
 
 set_source_file_properties(
   vtkMyClassWrappableInPython.cxx
   PROPERTIES WRAP_SPECIAL 1
 )

Other Topics

Using VTK objects in code wrapped with Boost.Python

Example from and to python converters