I am currently trying to create a C++ library with pybind11. My .cpp-file is a simple test and looks like this:

#include <pybind11/pybind11.h>

namespace py = pybind11;

std::string test()
    return "Test successful!";

PYBIND11_MODULE(TreeGenModule, m)
    m.def("test", &test);

Opening a Python interactive console I can import my compiled module and use the test-function.

However when importing the module from within a Blender-addon I'm trying to write, I can import the module itself but the interpreter cannot find any of the defined functions.

Any ideas why this error occurs?


Here's what happens if I navigate to the folder of my module and import the module using an interactive python session:

Python 3.7.9 (default, Nov 20 2020, 02:21:39) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import TreeGenModule
>>> TreeGenModule.test()
'Test successful!'

However, if I try the same import from within my module and invoke TreeGenModule.test():

AttributeError: module 'TBO_Tree_Gen.cpp_bin.TreeGenModule' has no attribute 'test'


So after some digging I found out, that this was caused by importlib.reload.

Blenders Reload Scripts-command only reloads the addon if the main file (in my case __init__.py) has been changed. (Also discussed here)

Since my addon consists out of several .py-files and I wanted everything to be reloaded if I hit the command, I used importlib.reload to manually force reloading.

I also used this technique on my C++-module, which you're not supposed to do!

Reloading C++-modules with importlib is not supported and may result in errors, just like in my case.

After removing this reloading instruction my C++-module works just fine in Blender. The only downside is, that I'll have to restart Blender, whenever I change my C++-module and want to see these changes in effect.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.