Building blender on ubuntu so we can use with python as a module

Hi everyone, i have followed the instructions on building on

  1. Building Blender/Other/BlenderAsPyModule - Blender Developer Wiki
  2. Building Blender/Linux/Ubuntu - Blender Developer Wiki

i get to the end but i think am not doing somethings right about the Cmake process because i do not understand when to do it or if not to do it,

i have followed the Easy (automated) way: see [building blender]docs for instructions, but run make bpy .

please i would be gladd if anyone an guide me in building this so when type import bpy in python it works,

also i have problem understanding the setting path

You may want to copy into the module to the systems Python path, e.g.:

/usr/lib/python3.7/site-packages

thank you.

Besides installing it, there is nothing you have to do with CMake when doing the easy build.

Your second link points to https://wiki.blender.org/wiki/Building_Blender/Linux/Ubuntu#Automatic_Dependency_Installation which is under Advanced Setup. The easy build steps are above this. Since you linked to the Advanced Setup, are you sure you did the Easy Build?

That sentence has to be missing a word, I cannot make sense of it either.

Alright i followed the easy step, am asking do i have to do the copy to path also?,

it says blender installed succesfully but when i go into python and type import bpy i ge no module named bpy, how to i complete this step so that i can have bpy as a python module.

Are the python used for building bpy and the one you run to type import bpy the same ?
Also, in output of make bpy, you’ll see the location where the bpy.so was installed into.

Thank you very much for your response, you are getting to the point,

this part is exacly where a confused and i dont understand, currently am on google cloud/aws ubuntu instance, i did not install python it came installed with python3 so i just start the process of the blender installation , yes i saw the bpy.so, do i have to move it or do something with it?

your response is highly appreciated, i have been trying to work on this for 2 weeks, because we couldnt use bpy in our server without calling the subprocess and we want to elimnate this.

make bpy loads build_files/cmake/config/bpy_module.cmake which sets WITH_INSTALL_PORTABLE to off. that means bpy.so would be installed in the site packages of the python used during build process. (it’s printed in the cmake output). If it was on, bpy.so would remain in the build folder and you would have to do

import sys
sys.path.append("path to folder where bpy.so is present")

before importing bpy.

Check the contents of site-packages, write permissions of the same. If it’s there, importing it using the same python binary wouldn’t be a problem. To ease all this, one can also run ctest -VV in the build folder. that’ll try to import bpy and see if it succeeds. (assuming master branch is being built)

Here is the beginning of the make bpy process

– Configuring done
– Generating done
– Build files have been written to: /home/swissbobo/blender-git/build_linux_bpy

Building Blender …
make -s -C “/home/swissbobo/blender-git/build_linux_bpy” -j 8 install

and here is the end of the make bpy

edit build configuration with: /home/swissbobo/blender-git/build_linux_bpy/CMakeCache.txt run make again to rebuild
.
Blender successfully built, run from: /home/swissbobo/blender-git/build_linux_bpy/bin/blender
now before i go to the make install part,

do i need to do anything again? or just follow the instruction that says

Linux

System Wide Install

You may want to copy into the module to the systems Python path, e.g.:

/usr/lib/python3.7/site-packages

For a system wide installation:

WITH_INSTALL_PORTABLE=OFF

Note, PYTHON_SITE_PACKAGES will be used as the target path, but this is auto detected, nevertheless, you may want to modify.

Once these options are set, from the BPY build directory (build_linux_bpy/ by default) run:

make install.

i will wait for response before running this process thank you very much.

here i ran from the folder below but i get this errors

/blender-git/build_linux_bpy/bin$ python3
Python 3.9.5 (default, May 11 2021, 08:20:37) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bpy
Color management: using fallback mode for management
Color management: Error could not find role data role.
Color management: scene view "Filmic" not found, setting default "Standard".
blf_load_font_default: 'fonts' data path not found for 'droidsans.ttf', will not be able to display text
blf_load_font_default: 'fonts' data path not found for 'bmonofont-i18n.ttf', will not be able to display text
blf_load_font_default: 'fonts' data path not found for 'bmonofont-i18n.ttf', will not be able to display text
/run/user/1001/gvfs/ non-existent directory
bpy: couldn't find 'scripts/modules', blender probably won't start.
Freestyle: couldn't find 'scripts/freestyle/modules', Freestyle won't work properly.
ModuleNotFoundError: No module named 'bpy_types'
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): source/blender/python/intern/bpy_rna.c:7378 pyrna_srna_ExternalType: fa

and of line of error is

    @           0x542a05  PyImport_ImportModuleLevelObject
    @           0x6028bd  PyImport_ImportModuleLevel
    @     0x7f1d836867f0  bpy_import_test
Aborted (core dumped)

I wasn’t sure if installation is done by make bpy or not. If that step is pending, yes it should be done.

cmake --build /home/swissbobo/blender-git/build_linux_bpy --target install
or in build folder make install -j 4.

The error in your next comment indicates that the Resources folder either doesn’t exist or doesn’t have the fonts, scripts etc. The directory structure in bpy should be the same as Blender app and its fonts folder.

yes i did make bpy as i couldnt wait ,

then in the build folder i did make install,

before make install i did this WITH_INSTALL_PORTABLE=OFF

now the only time am able to get close to importing bpy which ends up with python crashing or quitting is inside

only from here when i time python3 
and inside the pythong shell
>>> import bpy
>>> 
>>> import bpy
Color management: using fallback mode for management
Color management: Error could not find role data role.
Color management: scene view "Filmic" not found, setting default "Standard".
blf_load_font_default: 'fonts' data path not found for 'droidsans.ttf', will not be able to display text
blf_load_font_default: 'fonts' data path not found for 'bmonofont-i18n.ttf', will not be able to display text
blf_load_font_default: 'fonts' data path not found for 'bmonofont-i18n.ttf', will not be able to display text
/run/user/1001/gvfs/ non-existent directory
bpy: couldn't find 'scripts/modules', blender probably won't start.
Freestyle: couldn't find 'scripts/freestyle/modules', Freestyle won't work properly.
ModuleNotFoundError: No module named 'bpy_types'
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): source/blender/python/intern/bpy_rna.c:7378 pyrna_srna_ExternalType: failed to find 'bpy_types' module


cut some lines to line end

    @           0x52ca23  (unknown)
    @           0x5436fd  _PyObject_CallMethodIdObjArgs
    @           0x542a05  PyImport_ImportModuleLevelObject
    @           0x6028bd  PyImport_ImportModuleLevel
    @     0x7fe3ac1317f0  bpy_import_test
Aborted (core dumped)

here is the folder structure

swissbobo@instance-1:~/blender-git/build_linux_bpy/bin$ dir
bpy.so  datatoc  datatoc_icon  makesdna  makesrna  smaa_areatex
swissbobo@instance-1:~/blender-git/build_linux_bpy/bin$

Please ping me so that I get an email.

You’re mixing non-portable builds in python site packages and the bpy.so in the build folder. Need to pick one. Try running python -c "import bpy" in a folder other than the bin. I added a point in the troubleshooting section: Building Blender/Other/BlenderAsPyModule - Blender Developer Wiki

Continuing the discussion from Building blender on ubuntu so we can use with python as a module:

outside of the the bin folder anyway else i get

swissbobo@instance-1:~$ python3 -c "import bpy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'bpy'
swissbobo@instance-1:~/blender-git/blender$ python3 -c "import bpy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'bpy'

i have followed ur steps above, i really appreciate ur help and support

i can follow any steps u point me to step by step,

Below are all this steps i took

  1. Install Packages

Install essential packages with the package manager.

sudo apt update sudo
sudo apt install build-essential git subversion cmake libx11-dev libxxf86vm-dev libxcursor-dev libxi-dev libxrandr-dev libxinerama-dev libglew-dev

2.Download the latest source code from the git.blender.org.

mkdir ~/blender-git
cd ~/blender-git
git clone https://git.blender.org/blender.git

3.mkdir ~/blender-git/lib
cd ~/blender-git/lib
svn checkout https://svn.blender.org/svnroot/bf-blender/trunk/lib/linux_centos7_x86_64

4.cd ~/blender-git/blender
make update
make bpy

5 . For a system wide installation:

WITH_INSTALL_PORTABLE=OFF

then inside build_linux_bpy/

then ran make install

this is all i did.

There is one thing about building Blender as a Python module that I don’t like. Namely, doing an import bpy magically makes the other modules (e.g. mathutils) available for subsequent import. Normal Python libraries would, say, put all these modules in their own package. If this were called blender, then you could instead write more normal-looking code like

from blender import bpy
from blender import mathutils

and there would be no magical namespace shenanigans going on.

Quick update:
As per logs I asked for in a DM Pasteall/jQv5 (Python), SVN libdir python is selected by Blender and that’s where the installation of bpy happens. When python -c "import bpy" is run, it picks system Python which causes the error.

Discussed it with Campbell, and agreed upon using system Python for bpy instead of libdir one. I don’t have the platform where I can test this, would have to wait for a linux user to create a patch.

https://developer.blender.org/diffusion/B/browse/master/build_files/cmake/platform/platform_unix.cmake$105-114
https://developer.blender.org/D10372

reply thank you very much and yes i also found out and i could only import from this folder

blender-git/lib/linux_centos7_x86_64/python/lib/python3.9/site-packages

this is the only time am able to use blender.

i would test update out

The patch D10372 affects mac, where this issue is not there in the first place. That’s just an improvement.

I suggest using a post install script that copies the bpy.so and Resources folder from libdir site packages to system ones.

Greetings!

Could somebody please confirm that the instructions we are following -are- indeed supposed to work? I have just followed them as closely as I could, with the additional specification that where I specified “WITH_INSTALL_PORTABLE=OFF” was in CMakeCache.txt in the folder ~/blender-git/build_linux_bpy, which I used grep to determine was the only place it was specified either way. I specify this just before the last make install. I have confirmed that, like swissbobo, I do not get the error “No module named 'bpy” when I run in the directory he indicates. Not that that’s a good thing, I’d like to be able to run bpy from anywhere. In other directories I do. when I run it from -there-, I still get an error:

ImportError: /home/eriadar/blender-git/lib/linux_x86_64_glibc_228/python/lib/python3.10/site-    packages/bpy/lib/libboost_python310.so.1.80.0: undefined symbol: PyCMethod_New

I read somewhere that this might be generated by not following instructions in their proper order, so I re-started the whole process to be able to say I did it as properly as I could.

I have been told I can simply download bpy using pip https://pypi.org/project/bpy/ , but that has it’s own problems. I can maybe get it to work, but either way, the documentation should be updated -if- it needs to be…

From the comments of ankitm, it seems like the published procedure is -not- indeed expected to work, that additional steps are necessary to get files where they need to be.

…I just got a warning that some of the things I’m installing are going into directories that are not on PATH. This made me realize that I’m installing -not- as root. I don’t know if this could be the cause of my problems… I’m using sudo, so that’s somewhat equivalent…

ok, finally getting somewhere… (I also downloaded bpy from https://pypi.org/project/bpy/ , so whether my manual install or this more automatic one is the one getting used, I don’t know. What I -do- know is that for testing, I was using: python3 -c “import bpy” . -not- python3.10 -c “import bpy” . specifying the correct version of python, it seems to work. …now I have a whole new crop of problems… apparently I need to re-install numpy…