-
Hi, I would like to bind class methods to C++ using nb::callable. For example the method Class Test:
def __init__(self):
pass
def test_method(self, cpp_func_param):
cpp_func_param() On the C++ side, I have the following: class Vk_PyFunc : public Vk_Func {
public:
Vk_PyFunc(nb::object obj, nb::callable func)
:
_func(func),
_obj(obj),
Vk_Func(sizeof(Vk_PyFunc) + sizeof(Vk_Func))
{}
void operator()(std::function<void()> repeat) override {
_call_F(repeat);
}
private:
nb::callable _func;
nb::object _obj;
auto _call_F(std::function<void()> repeat) -> void
{
nb::gil_scoped_acquire acquire;
try {
_func(_obj, nb::cpp_function(repeat));
} catch (const nb::python_error &e) {
Vk_Logger::Error(typeid(this), "[Python exception]: {0}", e.what());
}
}
}; The important bit is _func(_obj, nb::cpp_function(repeat)); The issue I'm having is, that it crashes with Nanobind. It used to work with Pybind. I can resolve the issue if I don't put the Python methods inside of a class. But since I need access to the Does anyone know if it is possible to bind class methods to C++ and call them from there using Nanobind, and if so, how? I tried all combinations I could think of. A full-code Python example is in: https://github.com/Dude19735/Pyke/blob/main/test_py/test_vkviewer.py Thanks a lot for any help with this! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can take a look e.g. at how the |
Beta Was this translation helpful? Give feedback.
You can take a look e.g. at how the
std::function
bindings work, they do something similar. If you need help, then please either make a small reproducer or provide more information (e.g. a backtrace to know what part crashed would be useful).