Need Help: Compiling Blender as Python Module (for use in BGE)

Edit: I spoke too soon… see post 3 for current problems

Original Post:
I’m trying to compile Blender as a module using the “WITH_PYTHON_MODULE” CMake flag. I’m following the instructions here:
http://wiki.blender.org/index.php/User:Ideasman42/BlenderAsPyModule

Question:
Has anyone successfully compiled Blender as a Python module? I’m using current source, but I’ve tried the source from 2.62 official and 2.63a official as well. It appears that Campbell used the 2.61 source in that example, and I have not yet tried that.

What I’m doing:
I have disabled all of the conflicting flags, and successfully compiled bpy.so (an .so file is a .dll on Linux), but when I try to import in python3 using “import bpy” it fails.

Just to avoid the questions that will of course be asked:

There were no compile time errors.
I have included the 2.63 folder in the directory with bpy.so.
I am running python3 from the directory where bpy.so lives, and Python is properly recognizing it and attempting to load it.

The error:
It fails with the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: ./bpy.so: undefined symbol: BKE_sequencer_add_sound_strip

I understand this to mean that BKE_sequencer_add_sound_strip was not properly linked at compile time.

Am I just doing something wrong, or is this broken in current Blender?

I know that the “Compile Blender as Python Module” feature is considered experimental and even non-essential, but it would be incredibly useful for the Blender Game Engine, so I’d like to be able to get it working.

Update:

I successfully compiled bpy from the Blender 2.61 official source. I imagine the problem is that this feature is not currently being maintained, so it broken in current versions.

The blenderplayer probably won’t care which version of bpy is being called, so the Blender 2.61 bpy module should work in the current blenderplayer, as long as the 2.61 folder is included alongside it.

Suggestions:

Compiling as a module is more useful to the BGE community than to any other part of the Blender community. Therefore, I think the BGE community should do the following:

  1. Compile the Blender 2.61 bpy module for all operating systems. (I have compiled for Linux64, and can also compile Linux32 and Win32-mingw. If there is a desire to see this made available, someone else will need to compile Mac versions and Win64.)

  2. Find use case scenarios. If it’s useful to us, we ought to know why and be able to explain it. Uses I can think of: realtime mesh editing, and feeding binary data to LibLoad().

  3. Take an active role in development. If we are the ones who use it, we should find errors and report or fix them.

And yet another update on this if anyone is reading…

When bpy is imported by the blenderplayer, it causes a segmentation fault. bpy also causes a segfault when loaded from python3 in the terminal (on quit()).

I imagine the cause of this could be tracked down by examining the contents of the core dump…

Wow! This seems to be fantastic news! I actually never used bpy before, but I guess that it may allow object creation and editing! If that’s the case, this is possibly one of te most useful features for the BGE!
How can I help is there a way to test it? Is there a folder to download?

If you want a version that will automatically crash the blenderplayer when you import it, I can give you a Linux 64 or Linux 32 copy. If you want a version that imports properly in the blenderplayer, I don’t yet have that to offer.

Right now, what needs to be done to help is to debug the bpy module and find out why it’s crashing and if it can be fixed. That’s a rather complex task, and I’m not sure it’s something I’m up to.

Beyond that, we should find out why the bpy module won’t import properly when built from newer source code. Thus far, I’ve only been able to import it in python3 in the terminal when it is built from the 2.61 official source.

If anyone knows the proper way to compile a debug build of Blender, your advice would be appreciated. I have not delved into that as of yet.

Hey blendenzo, its really cool that you spend your time in figuring that out. I don’t use cmake so I’m not sure how to make an debug build, maybe there is a ‘make debug’ target defined in the generated makefile.

That would probably work if I used cmake-gui to generate an custom makefile. Using the bundled makefile, “make debug” tries to run “make install -j 2 -s” or something and that doesn’t work (Blender’s bundled makefile doesn’t define “install” as a valid make target). From looking at the CMakeLists.txt and examining the auto-generated settings in cmake-gui, it seems that “make debug” appends the “-g” flag to the make command and excludes the “-DNDEBUG” flag.

Edit: Okay… I generated a custom CMakeLists.txt with cmake-gui, and set “BUILD_TYPE” to “Debug” which calls the -g flag and doesn’t use the -O2 optimizations or the -DNDEBUG flag. Hopefully this will give me a useful core dump.