How can I access BPY in standard python console? BPY is the Blender-python -thing

不问归期 提交于 2019-12-03 06:00:46

问题


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

回答1:


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



回答2:


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



回答3:


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




回答4:


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.




回答5:


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




回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/10972637/how-can-i-access-bpy-in-standard-python-console-bpy-is-the-blender-python-thin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!