15

The author here in point 17.20-17.50 mentions that you can access BPY with the standard Python interpreter in the future. It is already 1 year old so how can I access the BPY with the standard python console?

Trial 0: roundaround -solution not working with subprocess inside Blender

subprocess.call(['vim', 'test.py'])
# some editing of BPY -file with Vim (not working currently)
subprocess.call(['python', 'test.py'])  
# trying to execute the python -file (not working currently)

Trial 1: not working outside Blender

$ cat cubes.py 
import bpy

mylayers = [False]*20
mylayers[0] = True
add_cube = bpy.ops.mesh.primitive_cube_add
for index in range(0, 5):
    add_cube(location=(index*3, 0, 0), layers=mylayers)
$ python cubes.py 
Traceback (most recent call last):
  File "cubes.py", line 1, in <module>
    import bpy
ImportError: No module named bpy
hhh
  • 50,788
  • 62
  • 179
  • 282
  • What does the first one have to do with the second one? You are trying to edit `test.py` with vim from a subprocess call and it fails? – jdi Jun 10 '12 at 23:28
  • ...similar newbie q [here](http://avp.stackexchange.com/questions/4145/how-to-create-a-rolling-ball-with-python-in-blender) about using Blender-python, there trying to do an animated ball. – hhh Jun 11 '12 at 02:25
  • **UPDATE** There are now at least two different approaches: you use BPY outside Blender (experimental property) and you use python inside Blender (no solution candidate for this yet). – hhh Aug 19 '12 at 15:20

9 Answers9

15

Based on these instructions:

Obtain the blender source code:

cd ~/src # or what you prefer
git clone http://git.blender.org/blender.git

cd blender
git submodule update --init --recursive
git submodule foreach git checkout master
git submodule foreach git pull --rebase origin master

Take care of the dependencies, see e.g. here if necessary* and compile via the bpy target:

cd ~/src/blender
make bpy

(re)run the latter as root if errors like file INSTALL cannot set permissions on [...] occur

Your python 3 should now be able to import bpy.


* For Debian-ish systems run

sudo apt-get install subversion build-essential gettext \
 libxi-dev libsndfile1-dev \
 libpng12-dev libjpeg-dev libfftw3-dev \
 libopenexr-dev libopenjpeg-dev \
 libopenal-dev libalut-dev libvorbis-dev \
 libglu1-mesa-dev libsdl1.2-dev libfreetype6-dev \
 libtiff4-dev libavdevice-dev \
 libavformat-dev libavutil-dev libavcodec-dev libjack-dev \
 libswscale-dev libx264-dev libmp3lame-dev python3.2-dev \
 libspnav-dev libtheora-dev libjack-dev libglew1.6-dev
chutsu
  • 13,612
  • 19
  • 65
  • 86
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
  • Firing a pile of errors for me, more [here](http://pastie.org/4550483). After manually installing like you instructed, I found this [here](http://wiki.blender.org/index.php/Dev:2.5/Doc/Building_Blender/Linux/Ubuntu/CMake) but it has old pkgs -- the pastie has updated pkgs but still some err, haven't yet found out a solution to this `"Could NOT find PythonLibsUnix (missing: PYTHON_LIBRARY PYTHON_INCLUDE_DIR)"`. Chat -discussion [here](http://chat.stackoverflow.com/transcript/message/4994981#4994981). Anyway +1 for perhaps good solution candidate, alert experimental! – hhh Aug 19 '12 at 15:02
  • According to [this](http://blenderartists.org/forum/showthread.php?227153-How-to-build-Cycles&p=1924562&viewfull=1#post1924562), you need to install `python-devel`. Also make sure you're using Python >= 3.2 – Tobias Kienzler Aug 20 '12 at 14:30
  • yup, [your `apt-get`](http://pastie.org/4550483) lacks `python3.2-dev`, you need to install that as well (it's not a dependency of Python itself) – Tobias Kienzler Aug 20 '12 at 14:37
  • @hhh Did you try it again with `python3.2-dev` installed? – Tobias Kienzler Sep 28 '12 at 09:36
14

In case this is still relevant, you can run a script in the context of blender like this (the -b makes it headless, so you can run it on a render server without X11):

blender -b -P script.py

For more options see blender --help.

If you want to connect blender to an IPython console, so you can to interact with blender via python you can use this script which I just wrote: https://github.com/panzi/blender_ipython

Start a notebook:

./blender_ipython.py notebook

Start a Qt console:

./blender_ipython.py qtconsole
panzi
  • 7,517
  • 5
  • 42
  • 54
3

I use eclipse to develop in blender. I found a good starting point to be http://airplanes3d.net/pydev-000_e.xml

Loopo
  • 2,204
  • 2
  • 28
  • 45
  • 1
    This may be a solution to some users +1, a direct link to the Eclipse Blender [here](http://airplanes3d.net/downloads/pydev/pydev-blender-en.pdf). From page 40 onwards perhaps useful stuff to some, anyway not answering the question per se. – hhh Aug 19 '12 at 15:17
1

In the video link you posted during that time segment, there is no mention of running a stand-alone blender python script using the standard python interpreter. What you are seeing in the video is them pulling up the interactive console for the interpreter built into Blender.

Blender requires its own bundled python environment, and if you were going to try to run a script using a standard python interpreter, you would have to set up the environment to include all of the packages from the blender package. Though it seems its probably not even possible as I think Blender's python is modified.

The blender executable does seem to allow you to run a python script via:
/path/to/blender -P cubes.py

You can also start an interactive console from a bash shell via:
/path/to/blender --python-console

jdi
  • 90,542
  • 19
  • 167
  • 203
  • Could you give some demo with `/path/to/blender --python-console` (some history file with some simple example would be useful), I get just a gray area and I can see that the commands work and I can import bpy but I cannot see anything --- and I can see that it is clearly modified, `exit()` does not work... – hhh Jun 11 '12 at 02:23
  • @hhh: unfortunately I dont know the blender api. I was only able to give you insight on your specific question as to why you cannot run it in a standard python interp. You might just want to look up some samples on the internet to learn its API. I am sure there are a lot. – jdi Jun 11 '12 at 02:37
  • [You can actually compile Blender as a Python 3 module](http://stackoverflow.com/a/11102681/321973) – Tobias Kienzler Nov 15 '12 at 10:28
1

This article explains how to build blender as a python module.

http://wiki.blender.org/index.php/User%3aIdeasman42/BlenderAsPyModule

It doesn't appear that this technique will connect an outside python session to a regular blender process but rather run blender inside of the python process.

Ben L
  • 1,449
  • 1
  • 16
  • 32
0

I am new to programming but I found a simple workaround, I used the command line for terminal using os. My program looked somewhat like this.

import os
os.system("cd /home/")

(that is where my blender is)

and then I used the terminal command the same way I used cd.

https://docs.blender.org/manual/en/dev/render/workflows/command_line.html

Seb
  • 23
  • 9
0

Someone created a stub API generator. He even hosted the generated bpy at Github for Blender verison 2.78, 2.79, 2.80. It should be enough for you to write code in IDE e.g. PyCharm. I spotted some syntax errors in the generated code. You'll have to fix them. There is a document, too.

https://github.com/nutti/fake-bpy-module/tree/master/premade_modules/2.79

Here is a web site for the installation instruction if Nutti's doc is somewhat short.

https://b3d.interplanety.org/en/using-external-ide-pycharm-for-writing-blender-scripts/

To run the code you'd have to use Blender's Python as already pointed out by other answers.

blender -b -P script.py

Sorry, about the link only answer.

Kenji Noguchi
  • 1,752
  • 2
  • 17
  • 26
0

You can run any script wriiten in text editor of blender and debuging it in the python console of blender and here is how :

1.create your script file and put there your code .

  1. open the console python in the blender and type in it : bpy.data.texts[INDEX_OF_YOUR_FILE].as_module() .

    INDEX_OF_YOUR_FILE is the index of your file in the drop down menu of list of text in text editor .

Mhadhbi issam
  • 197
  • 3
  • 6
0

You can blender to run a python script with/without running in background from command line. Go to directory where blender is installed, open terminal/cmd over there and type following command -

blender -b -P path/to/your/script.py 

The flag -b tells blender to run in background. -P tells to run a python script using blender's python. If you want to open blender's GUI and run py script then run the following code:

blender -P path/to/your/script.py

While running as subprocess use the following code:

import subprocess
subprocess.run(['blender', '-b', '-P', 'path/to/your/script.py'])
MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22
YadneshD
  • 396
  • 2
  • 12