4
$\begingroup$

(This is related to this post)

I would like to add a module to Blender's Python (Blender v2.76). I want to follow the solution outlined in the link above, but my Blender directory does not have a 2.7x subdirectory. Could this be because I am using 2.76, and the answer uses version 2.72?

The output of running ls in my blender directory is the following:

GPL-license.txt                 blender.app
GPL3-license.txt                blenderplayer.app
LICENSE-bfont.ttf.txt           copyright.txt
LICENSE-bmonofont-i18n.ttf.txt  game
LICENSE-droidsans.ttf.txt       jemalloc-license.txt
LICENSE-libiomp5.txt            readme.html
Python-license.txt              startScript.txt
blen files

Where blen files and game are my own subdirectories. From my understanding of the related answer, I would simply need to the Python module I need in the /blender/2.72/python/lib/python3.4/ directory?

What should I do if this directory is missing? This is strange, because I can script in Python, from within Blender, even though this directory is missing...

$\endgroup$
1
$\begingroup$

Python loads any module from the directories in sys.path. Try this in the interactive Python console in Blender:

import sys
print('\n'.join(sorted(sys.path)))

That'll give you a choice of paths. Alternatively, you can place your scripts next to your blend file, and in the blend file text editor itself have a script like this:

import bpy
import sys

p = bpy.path.abspath('//scripts')
if p not in sys.path:
    sys.path.append(p)

Make sure the script name ends in .py and the 'Register' checkbox is checked. After this runs you can import Python modules from the scripts subdirectory of whichever directory holds your blend file.

Also make sure that the directory names are actually valid Python names, so without spaces.

$\endgroup$
0
$\begingroup$

You seem to be on an unix system (DUH). maybe "2.7X" is in a nereby location (if blender's directory isn't directly /usr/share/blender and if there isn't any "blender_2.76" dir next to it.) maybe in "/usr/lib/blender"?

If not, you could see the "file" property seen in any addon's details

$\endgroup$
0
$\begingroup$

In a script, you can access modules installed on your system within Blender this way:

import sys
DIR = '/home/username/.local/lib/python3/dist-packages/MODULE/'
sys.path.append('DIR')
import MODULE

Just tell it what dir to use, and the module name.

$\endgroup$
0
$\begingroup$

Another way around this would be to compile Blender as a Python module. This way, you can easily do import bpy in your system's Python installation (Python 3.6 and above as of October 2018). What you have to do is to download Blender's source code from here by clicking on the drop-down menu and selecting Source Code. If you're on Windows, you should follow these instructions to compile Blender. Just make sure you set the CMake flag the Python module to enabled (shown below). If you're on a Unix-based operating system you should first execute /blenderSourceCode/build_files/build_environment/install_deps.sh to install the dependencies and then use CMake to generate the make files as follow:

cd blenderSourceCodeDir
mkdir build
cd build
cmake .. \
  -DCMAKE_INSTALL_PREFIX=/usr/lib/python3/dist-packages \
  -DWITH_PYTHON_MODULE=ON \
  -DPYTHON_SITE_PACKAGES=/usr/lib/python3/dist-packages \
make -j 4
make install

This way you can install any package through pip or other tools and import them alongside the bpy module as follow:

python3 -m pip install h5py
python3 -m pip install scipy

python3 -c "import bpy; import h5py; import scipy; print ('done')"
$\endgroup$
  • 1
    $\begingroup$ Official instructions on how to build blender can be found here: wiki.blender.org/wiki/Building_Blender Also I'd suggest cloning the repo instead of downloading the source code @Amir $\endgroup$ – p2or Oct 25 '18 at 7:17
  • $\begingroup$ @p2or I always clone the repo myself but I thought this might be easier for the average user :) $\endgroup$ – Amir Oct 25 '18 at 10:19

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.