Creating a Blender python module

Greetings.

This is my first post here and have just signed up.
Not sure if this is the right forum to ask this

I have been a Blender user for quite a few years and to be brief, I had recently decided to use blender for procedural content creation after I found the Unreal Engine editor crashed when deleting the procedural content I had created using C++. In the last month I have transferred and improved the method of procedural content creation by porting the code I had created from c++ in Unreal Engine 4 to a blender python script as an addon, and found no problems
with displaying and deleting the created content.

However, I am now wanting to go to the next stage, but as I suspected before learning to code in Python and using the blender python API, and the reason for initially starting in Unreal engine, blender python is not adequate for the task. I had to use a few hacks to get things to work as it was. So, I have decided to try and create, if possible, a blender python module which can be incorporated into blender using c++.

I have downloaded and built the latest blender 2.8 as of 2/5/2019, looked at a few examples of c, c++ code that look to be source code for blender python scripting, but cannot find or work out which blender python module they are related to so as to get an idea of how to create a blender python module.

So my query is this. Is there some documentation available on how to create a blender python module to be integrated with the existing blender python API, and importantly, if I need to refer to an existing blender python module, how to find that module c, c++ code so as to look at and get an idea of how to perform those module tasks in my own code. I have found some documentation on extending the python interpreter, but I’m not sure if or how this will
help in doing the same for extending blender python.

I would appreciate any help given and thanks in advance for any help given.

4 Likes

Hi, I understand you want to code in C/C++ but keep that code as an add-on for Blender, right?
May I ask what hacks you had to make to work it as it was? Do you want to go back to C/C++ for performance reasons as well?
I ask because if you already have a python module working, it’s probably easier to make it better than to go full C/C++.
Also, it’s important to know if you intend to distribute your C/C++ code, since you have to take into account compiling for different platforms.

If I’m understanding what you’re trying to do it’s fairly straightforward- there are already Blender addons that do this (notably UVPackmaster). The process is fairly simple when you distill it down to the basic functionality: Pack up your mesh data in python, send it to your external process, do your thing, then send it back. I’m assuming the sending and receiving part is what you want help with, given that the other parts are going to be unique to your project.

sending and receiving data fromanother process is actually fairly straightforward in python using subprocess:
https://docs.python.org/3/library/subprocess.html?highlight=subprocess#subprocess.Popen.communicate

while your addon is doing its thing, you will want a modal operator waiting for a response so you can unpack the data it receives. There’s a modal operator template that ships with Blender which is a good start for how you would set something like that up.

Side note- this probably goes without saying, but when you’re dealing with external processes, you have to handle all of the multiplatform stuff yourself because you’ve left the safety of Blender. Obviously you’ll have to compile your own binary for each OS you intend to support, but your python implementation will also need special checks for OS as well.

If I understand correctly, you want to extend Blender’s embedded Python interpreter with C/C++ code.
This might help:
https://docs.python.org/3/extending/extending.html

Thank you for you reply.

The most profound hack one was to use some python code I found on the net (https://www.geeksforgeeks.org/implementation-of-dynamic-array-in-python/) to create a dynamic array similar to the function of a c++ vector array. Another was to modify this code so I could effectively “clear” it of data.as only appending to it was given. Had to do this as what I thought was a local variable in a python class was was retaining values between separate calls calls to it.

The rest had to deal with having to repeat the same code with different variable names or slightly modified. Python classes and structures behave differently to c++ and so I had to adjust and what I thought were local variables were not really local.

I have also considered that the addon I have created cannot be improved. The reason I want to create a C/C++ python module is that the next stage involves recursive routines requiring pointers to parent and child objects stored in a dynamic vector array, and traversing through the links. in short, this content generator I have created in C++ loads an existing mesh object, copies and pastes and performs routines to place that object within a scene, rotate it and scale it. And yes, there are performance issues as well.It seems when performing every blender python action like copy/paste, location, rotation, scale etc is stored in blenders do/undo that can slow things down to a crawl.

So it is my view to create c++ python module is a lot easier in every way than trying to recreate this in python code. Python is a scripting language, not a programing language in my opinion and to have a python script of one line is a lot easier than one of what I see will can be balloon in a several hundred.

Oh and as a side note, I am experimenting at the moment. Thoughts about submitting my code for including in Blender is not entering my mind right now. That comes later. I am just seeing what I can do in creating content within Blender.

Here is a sample of what I have achieved in unreal engine.

Regards

Greetings.

To get and Idea of what I am doing and why I want to create a C++ bender python module, please look at my reply to Brachi. I am not not dealing with external processes at all, except creating and application to create a text file which the blender addon I have created reads to get the parameter data to perform its function. A very simple task to do. The user selects this text file through a Blender UI widget, presses a button to read that text file, process the parameter data to generate content, and needs to do nothing else.

Regards

Greetings Stiv and thanks.

I have already read this, and some extra stuff as well. It does not seem all that difficult to do.

My question was more to do with is the documentation of how to create a Blender python extension module, and if so where I can find it. Creating a Blender python extension may have differences to an ordinary Python module extension.

Regards

There is nothing special about blenders python, there is no documentation on how to make a “blender python extension” given it’s identical on how to make a regular python extension. The only thing you need to pay attention to is the version we are embedding which is currently at 3.7 for the nightlies and 3.5 for the official 2.79b

Thank you, that is what I needed to know.

I am only go to focus on Blender 2.8 ad beyond and where I have done my coding for. So it is 3.7 I will be using

All that is left is to find the source code that the blender python modules were created from such as bpy.object.XXXXX, bpy.context.XXXXX etc. so I can find out if/how to write c/c++ python module to call functions and perform tasks such as bpy.context.object, bpy.ops.object.delete and more. I have found what looks like the blender python c/c++ source code, within the /source/blender/python/ directory, but nothing specific of what bpy module each refers to if any. I need to spend more time to investigate.

Regards

It’s probably easier just to do those things in python, and just have the performance sensitive things in the C/C++ module.

Well I may pause my ambition here a while. I have looked at the source code more closely and found all, or most of the c code that the blender python extensions seems to originate from is in the source/blender/editors directory, and that the c functions are referenced using function pointers from within a data structure called wmOperatorType.

I think that the blender python extensions are defined within the source/blender/python directory where there is c code that looks to where the blender python extensions are defined. Something an expert python extension coder would understand, but I do not. And besides, this would probably mean that to create a blender python module using the blender c source code, one would have to modify the actual source code. Something I do not want to do

However, I have found on the net, a method to call a python function within a c python extension called PyEval_CallFunction. This looks to be the best solution. But no documentation or examples of how to use can be found.

But this is of no worry to me now. I am itching to pursue this later and concentrate on something else.

Thank you and Regards

Best option that I’ve found is make an actual DLL that Blender Python module call through CFFI library. You can easily install CFFI into Blender:

import bpy
from subprocess import call
pp=bpy.app.binary_path_python
call([pp,'-m','ensurepip'])
call([pp,'-m','pip','install','cffi']);

Zero hassle with different Python versions which can very quickly become a pain. Only thing you need to worry about is getting data through a C interface. Meaning you’re not just limited to C or C++, you can use any language you wish, like Rust.

Once the module is written, it should very easily work everywhere. If Blender only became with CFFI pre-installed, it would be even better.

With pickling from Blender to the module, you won’t get as fast data transfer. With CFFI, you can transfer Numpy arrays from Blender and back instantly. Meaning any data the Blender Python has access to, can get instant access from fast languages like C++ and Rust.

3 Likes

Interesting. Thank you for this information.

However I have decided to pursue my ideas by using Qt as I have been. I have decided that using Blender is not the way to go in creating procedural content for 3D applications like Blender to use and modify. Its python focused design and structure is just not suitable. Qt and other C++ librabries I have found since this original post offers a path that I can pursue. I consider this thread now closed and will not be pursuing this path in Blender.

Regards