Almost all of the functionality of the C++ OMPL library is accessible through Python using more or less the same API. Some important differences will be described below. The Python bindings are generated with Py++, which relies on Boost.Python. The bindings are packaged in the ompl module. The main namespaces (ompl::base, ompl::control, ompl::geometric) are available as sub-modules. To quickly get an idea of what classes, functions, etc., are available within each submodule, type something like this at the Python prompt:
Although almost all C++ functionality is exposed to Python, there are some caveats to be aware off:
function_call(Constructor_call())), so it's advisable to create variables with the appropriate scope.
print fooin python. Similarly, a C++ call like
print foo.settings()in python.
Many of the python demo and test programs are direct ports of the corresponding C++ programs. If you compare these programs, the sometimes subtle differences will become more obvious. In the python programs you will notice that we can create python classes that derive from C++ classes and pass instances of such classes to C++ functions. Similarly, we can create python functions (such as state validity checkers or propagate functions) that can be called by C++ code.
Below is a simple annotated example. It is available in ompl/py-bindings/demos/RigidBodyPlanning.py.
OMPL relies heavily on boost::function objects for callback functions. To specify a Python function as a callback function, that function needs to be cast to the right function type. The simple example above already showed how to do this for a state validity checker function:
If you need to pass extra arguments to a function, you can use the Python
partial function like so:
The Python bindings are subdivided into modules, to reflect the main namespaces: ompl::base, ompl::control, and ompl::geometric. The code in the ompl/src/ompl/util directory is available in a submodule as well. Whenever you change the API to OMPL, you will need to update the Python bindings. Updating the bindings is a two-step process. First, the code for the modules needs to be generated. Second, the code needs to be compiled into binary Python modules.
The code for the Python bindings can be generated by typing “
make update_bindings.” This creates one header file per module, formed by concatenating all relevant header files for that module. This header file is then parsed by Py++ and the appropriate C++ code is generated. This code uses Boost.Python. Py++ is smart enough to create wrapper classes when necessary, register Python <-> C++ type conversions, and so on. If you only need to update the bindings for one module (say
base), you can type “
make update_base_bindings.” Any diagnostic information from Py++ is stored in a log file in the build directory for each module (
pyplusplus_base.log for the module
base and likewise for other modules). You can remove all generated code by typing “
make clean_bindings”. This is sometimes necessary if Py++ gets confused about which code needs to be regenerated. If this happens, all the generated code might still compile, but you can get odd crashes or errors at runtime.
For each module the relevant header files are listed in
ompl/py-bindings/headers_<modulename>.txt. The order in which the header files are listed is important. A header file should not be included by another header file listed above it. If you have created new header files, you should add the names of these files to the appropriate
To compile the Python modules type “
make py_ompl” (or simply “make”). If you only want to compile one python module (say
base), type “
make py_ompl_base.” The modules will appear as libraries in the lib subdirectory in the build directory, but they are also copied to
Every attempt has been to have CMake correctly identify dependencies and only compile code when necessary. If you want force CMake to regenerate the bindings from scratch, you can type “
make clean_bindings,” followed by “
make update_bindings” again. If, on the other hand, you want to (temporarily) disable the compilation of Python bindings, type:
cmake -D OMPL_BUILD_PYBINDINGS:BOOL=OFF .
in your build directory. You can re-enable them, by running this command again, but with OFF changed to ON. Changing these settings can also be done through the CMake GUI.