Using a Custom Python Module With Blender
I’ve reached the stage in my blender project that I need to interface blender with a bunch of pre-existing libraries that we have which are written in C++. This is a better way to work with our system than directly interfacing to the database as our C++ libraries provide a common API with other systems that are using the same database. I am also eventually going to be doing more advanced things with the blender model such as interacting with our simulator system so it’s worth knowing how to do this now.
Fortunately, python provides a way to make bindings to a C/C++ library. Also, blender is happy to import any python module that is compiled against the same version of python that blender has built-in. In my case this is blender 3.1.2 and I’ve already tested this using the py-postgresql module (see previous posts: Blender Project and Connecting to a Database From Blender)
Python has this helpful documentation to get you started on writing a C/C++ module. I followed these instructions and had a python module working without any trouble once I managed to link in all the dependant libraries and set the include paths. I tested this by trying out the module from the python3 console on the linux command line.
As predicted, running from blender was seamless building on the work I’d done previously to get the sys.path right. I imported my module in my blender script and ran my module function with no problems.
