Skip to content

Instantly share code, notes, and snippets.

@xunkai55
Last active March 16, 2022 00:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xunkai55/6331fdd3446257cf5b4a98f202727245 to your computer and use it in GitHub Desktop.
Save xunkai55/6331fdd3446257cf5b4a98f202727245 to your computer and use it in GitHub Desktop.
Setup bpy (Blender as a python module) on Mac OS X
# Do not excute the script directly. It is just for demonstration.
# If you followed the documentation and got the errors below, please take a look at this guide.
# Color management: using fallback mode for management
# bpy: couldnt find 'scripts/modules', blender probably wont start.
# Freestyle: couldn't find 'scripts/freestyle/modules', Freestyle won't work properly.
# ImportError: No module named 'bpy_types'
# ImportError: No module named 'bpy_types'
# pyrna_srna_ExternalType: failed to find 'bpy_types' module
# ImportError: No module named 'bpy_types'
# ImportError: No module named 'bpy_types'
# pyrna_srna_ExternalType: failed to find 'bpy_types' module
# ImportError: No module named 'bpy_types'
# pyrna_srna_ExternalType: failed to find 'bpy_types' module
# F0102 21:06:58.742173 1895752448 utilities.cc:322] Check failed: !IsGoogleLoggingInitialized() You called InitGoogleLogging() twice!
# "
# Firstly, download the source and excute "make bpy" things.
cd $THE_FOLDER_YOU_BUILD/bin
# Now you should find bpy.so and 2.xx directory here.
# If you type python3 and `import bpy` here, you would get the error messages above.
# Don't worry.
mkdir ../Resources
mv 2.78 ../Resources/
# Change the version number if is out of date.
# Try the test script. It could be good now.
python3 -c "import bpy ; bpy.ops.render.render(write_still=True)"
# Of course, if you want to setup bpy in somewhere else,
# you have to do something just like `mkdir ../Resources; mv 2.78 ../Resources`.
@bednarikjan
Copy link

Thank you this helped! I would add that (at least for me) this only works if your current working directory is "$THE_FOLDER_YOU_BUILD/bin" before running pyhton3. If you want this to work even if you run python from different directory, just add "$THE_FOLDER_YOU_BUILD/bin" to your PYTHONPATH, e.g. "export PYTHONPATH=$THE_FOLDER_YOU_BUILD/bin:$PYTHONPATH".

@johnwquarles
Copy link

Thank you!

Finally was able to import bpy.

@odadoun
Copy link

odadoun commented Sep 8, 2017

Yes ! it works :-)
Thanks

@mohsen1363
Copy link

Hi, I am new on these topics. Can you please provide more information on how to do these? I highly appreciate it in advance.
I wanted to download the source for bpy but I could not find it. It seems that bpy is part of blender itself. So, I am a bit confused in how to download bpy and "make" it.

@kabukunz
Copy link

kabukunz commented Mar 1, 2018

This summarizes the Blender build process as the time of writing (Os X 10.12, Blender 2.79 master build, python 3.6.2)

Download and install Os X system python 3.6.2 from https://www.python.org/ftp/python/3.6.2/python-3.6.2-macosx10.6.pkg
You need command line cmake frow Brew so install Brew from: https://brew.sh/

This setup requires shell prompt (terminal).

On Os X, CMD-Space type "terminal" then enter.

check you have Os X system python in your path:

echo $PATH
/Library/Frameworks/Python.framework/Versions/3.6/bin:...

then install cmake:

brew install cmake. Test with:

cmake --version
cmake version 3.10.2

use git (provided with Os X) to download master Blender distro, located in git repo and svn prebuilt libraries:
https://developer.blender.org/
https://developer.blender.org/diffusion/

mkdir
cd

git clone git://git.blender.org/blender.git --recursive
svn export https://svn.blender.org/svnroot/bf-blender/trunk/lib/darwin

cd blender

remember to move your downloaded prebuilt libraries into the top dir where blender repo is with:

mkdir ../lib
mv ../darwin ../lib

start building process:

make bpy
cd ../build_darwin_bpy
make install
(building "normal" Blender installation is just a matter of typing "make" instead of "make bpy")

this issues a warning:
ld: warning: directory not found for option '-L/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m'

to remove it open the file:
build_darwin_bpy/source/creator/CMakeFiles/blender.dir/link.txt
and change config-3.6m into config-3.6m-darwin

make install should now work. (I'm not sure if this step is needed.)

make install

cd bin
mkdir ../Resources
mv 2.79 ../Resources/
(note you don't need this if you build a non-bpy setup. make install should suffice)

try as shown above:

python3 -c "import bpy ; bpy.ops.render.render(write_still=True)"

now try to import into python:

python3.6
import bpy
dir(bpy)

['all', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'app', 'context', 'data', 'ops', 'path', 'props', 'types', 'utils']

more info explained here https://wiki.blender.org/index.php/Dev:Doc/Tools/Git

Thanks to the guys who found the fix

@TylerGubala
Copy link

I attempted this yet received the same error described.

I have a few more tests to do once I get back home; could someone describe what the final environment looks like?

would it be something like:

venv
  |_Resources
    |_2.79
      |_scripts
        |_modules

or:

venv
  |_Resources
    |_scripts
        |_modules

@YangZeyu95
Copy link

thanks,it works!

@gandarez
Copy link

gandarez commented Aug 27, 2019

@kabukunz I'm having the issue as described in this gist but even changing to ../config-3.7m-darwin in the middle and somehow make bpy changes back to config-3.7m and the error comes back. Any thoughts?

ld: warning: directory not found for option '-L/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m'

@sanketdiwale
Copy link

sanketdiwale commented Oct 15, 2019

@kabukunz Any ideas how to configure the make system to use non system python for bpy?
I have a number of python virtual environment that I manage through pyenv and I would like to use "$HOME/.pyenv/versions/3.7.2" instead of the "/Library/Frameworks/Python.framework/Versions/3.7/Python" that the make system looks for by default. I searched quite a bit, but couldn't find where this path is defined and how to change it?

I tried editing the build_files/config/bpy_module.cmake to update lines 10-11 to read as

set(WITH_INSTALL_PORTABLE    ON CACHE BOOL "" FORCE)
set(CMAKE_INSTALL_PREFIX "${HOME}/.pyenv/versions/3.7.2")

However, my compilation still fails with an error:

clang: error: no such file or directory: '/Library/Frameworks/Python.framework/Versions/3.7/Python'
make[2]: *** [bin/makesdna] Error 1
make[1]: *** [source/blender/makesdna/intern/CMakeFiles/makesdna.dir/all] Error 2
make: *** [all] Error 2

@kabukunz
Copy link

Same here. Two approach:

  • symlink pyenv in your home from Application/Library/Frameworks/Python
  • reinstall pyenv environment with Framework option (better, it did the above for you)

No easy usage otherwise. Default python for mac is in Frameworks, all apps looks for it there.

@Gaurav1302
Copy link

@kabukunz I'm having the issue as described in this gist but even changing to ../config-3.7m-darwin in the middle and somehow make bpy changes back to config-3.7m and the error comes back. Any thoughts?

ld: warning: directory not found for option '-L/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m'

Did anyone find a solution to this error?
@gandarez @kabukunz @sanketdiwale

@m-clare
Copy link

m-clare commented Sep 14, 2020

@Gaurav1302, I tried the solution here to no avail, and then I also tried renaming the folder to config-3.7m (removing the darwin suffix), and still ended up with the same errors:

clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [bin/bpy.so] Error 1
make[2]: *** [source/creator/CMakeFiles/blender.dir/all] Error 2
make[1]: *** [all] Error 2
make: *** [all] Error 2

@Gaurav1302
Copy link

Gaurav1302 commented Sep 14, 2020

@m-clare, I couldn't make this work on a Mac. So, I dual-booted my PC to Linux (Ubuntu) and it worked perfectly for me. It was much easier to use Blender over a Linux system. I suggest you switch to a Linux system instead of trying to figure out a way to make it work on a Mac. I assure you, it's much simpler.

@lakpa-tamang9
Copy link

@Gaurav1302, How did you install bpy in Ubuntu? I have Ubuntu 18.04 with python 3.7. I have tried several methods, still not able to do so correctly. Can you help me out here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment