Developer Documentation

Python Interface

The Python interface enables OpenFlipper to export plugin functions to the integrated python interpreter of OpenFlipper. Only a few steps are required to export functions in a plugin.

1. Tell cmake, that your plugin supports the Python Interface. In your CMakeLists.txt add the Keyword PYTHONINTERFACE. E.g. :
   include (plugin)
   openflipper_plugin (PYTHONINTERFACE )
2. Add the corresponding includes and calls to your plugin header:
   - Add #include <OpenFlipper/BasePlugin/PythonInterface.hh> in your plugins header file
   - derive your plugin from the class PythonInterface
   - add Q_INTERFACES(PythonInterface) to your plugin class
3. Add a subdirectory called PythonInterface to your plugin directory
4. In this directory create a file called Python.cc with contents similar to the following Code:
   #include <pybind11/include/pybind11/pybind11.h>
   #include <pybind11/include/pybind11/embed.h>
   #include <YourPlugin.hh>
   #include <OpenFlipper/BasePlugin/PythonFunctions.hh>
   namespace py = pybind11;
   PYBIND11_EMBEDDED_MODULE(YourPlugin, m) { // yourPlugin will will be the module name in Python ( ... import yourPlugin )
   QObject* pluginPointer = getPluginPointer("Your"); // This will retrieve your plugin instance pointer. The name is the plugin name set via the name() function.
   // Export our plugin. Make sure that the c++ worlds core object is not deleted if
   // the python side gets deleted!!
   py::class_< YourPlugin,std::unique_ptr<YourPlugin, py::nodelete> > plugin(m, "Your"); // This will provide you with the existing instance of your plugin in Python.
   // On the c++ side we will just return the existing core instance
   // and prevent the system to recreate a new core as we need
   // to work on the existing one.
   plugin.def(py::init([&pluginPointer]() { return qobject_cast<YourPlugin*>(pluginPointer); })); // This will provide you with the existing instance of your plugin in Python.
   plugin.def("viewAll", static_cast<void (YourPlugin::*)()>(&YourPlugin::viewAll),"Change View in all viewers to view whole scene"); // Example export of an overloaded member function
   plugin.def("viewAll", static_cast<void (ViewControlPlugin::*)(int)>(&YourPlugin::viewAll),"Change View in given viewer to view whole scene",py::arg("Id of the viewer which should be switched") ); // Example export of an overloaded member function
   plugin.def("orthographicProjection", static_cast<void (ViewControlPlugin::*)()>(&ViewControlPlugin::orthographicProjection) ); // simple export example
   }