tiled icon indicating copy to clipboard operation
tiled copied to clipboard

Modernize Python bindings

Open bjorn opened this issue 4 years ago • 3 comments

The current Python plugin relies on a large amount of generated code based on the somewhat hard to maintain and always incomplete tiledbinding.py and qtbinding.py.

I've recently learned about pybind11 (see also this blog post), which seems like a much nicer way of binding the classes to Python. The bindings are defined with similar verbosity, but directly in C++ which makes it easier to keep up-to-date.

In addition, pybind11 provides a more convenient way of interfacing with an embedded Python interpreter than using the Python API directly.

bjorn avatar Aug 28 '19 13:08 bjorn

Did You consider using boost::python?

Sample binding looks like this:

class_<CGame, bases<CGameObject>, boost::noncopyable, std::shared_ptr<CGame>>("CGame", no_init)
        .def("getMap", &CGame::getMap)
        .def("changeMap", &CGame::changeMap)
        .def("loadPlugin", &CGame::loadPlugin)
        .def("getGuiHandler", &CGame::getGuiHandler)
        .def("getObjectHandler", &CGame::getObjectHandler)
        .def("createObject", &CGame::createObject<CGameObject>)
        .def("getGui", &CGame::getGui);

and python:

@register(context)
class ChangeMap(CEvent):
    def onEnter(self, event):
        self.getMap().getGame().changeMap("map2")

lisu188 avatar Sep 05 '19 19:09 lisu188

@lisu188 I didn't try either yet, but the README.md from pybind11 has a lot to say about Boost.Python, and it makes pybind11 sound rather more attractive.

bjorn avatar Sep 05 '19 20:09 bjorn

In the context of this issue, also try using binder for even more automatic binding generation.

bjorn avatar Jan 11 '22 08:01 bjorn