本文介绍了Blender Python`bpy``__init __.py`,显然是从不存在的模块'_bpy'导入的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Mac OS X与PyCharm,Python 3.5.1和Blender 2.77结合使用.

I'm using Mac OS X with PyCharm and Python 3.5.1 and Blender 2.77.

我正在将Blender与Python脚本一起使用.我了解为了运行使用Blender的Python脚本(即导入bpy),我需要使用blender -b -P /path/to/my_python_script.py从命令行运行它(尽管我真的不知道为什么).一切都很好,并且可以运行,但是我希望我可以从Python内部运行它,因为我将这些脚本与其他非Blender Python代码一起使用,并且我喜欢使用PyCharm进行调试并完成几乎所有事情.我知道我不能只运行PyCharm的Blender Python脚本,但我认为我还是会尝试的.我通过编辑首选项"中的项目结构"设置以确保PyCharm可以看到bpy模块,以包含bpy模块所在的父目录,该目录在我的机器上为/Applications/blender.app/Contents/Resources/2.77/scripts/modules.但是,当我尝试运行脚本时,它会显示ImportError: No module named '_bpy'.我遵循了错误的根源,而罪魁祸首是bpy模块中__init__.py文件中的一行(其在我的计算机上的位置为/Applications/blender.app/Contents/Resources/2.77/scripts/modules/bpy/__init__.py);该行是:

I'm using Blender with Python scripts. I understand that in order to run Python scripts that use Blender (i.e. that imports bpy), I need to run it from command line using blender -b -P /path/to/my_python_script.py (although I don't really know why). That's all fine and it works, but I wish I could just run it from inside Python, because I use these scripts with other non-Blender Python code and I like to use PyCharm to debug and to do pretty much everything. I understand that I can't just run the Blender Python script from PyCharm, but I thought I'd try anyway. I took care to ensure that PyCharm can see the bpy module by editing the "Project Structure" settings in "Preferences" to include the parent directory in which the bpy module lives, which on my machine is /Applications/blender.app/Contents/Resources/2.77/scripts/modules. However, when I try to run the script, it gives ImportError: No module named '_bpy'. I followed the source of the error and the culprit was a line in the __init__.py file in the bpy module (whose location on my machine is /Applications/blender.app/Contents/Resources/2.77/scripts/modules/bpy/__init__.py); the line is:

from _bpy import types, props, app, data, context

因此,我尝试在我的机器上搜索模块_bpy,但在任何地方都找不到它.因此,似乎是从不存在的模块中导入内容.但是,我知道我的脚本有效,因为我已经在Blender中成功运行了它.

So I tried to search for the module _bpy on my machine, and couldn't find it anywhere. So it seems to be importing things from a module that doesn't exist. However, I know that my script works because I've successfully run it in Blender.

所以我的问题是,神秘的_bpy模块正在发生什么巫术,我和PyCharm都找不到,但是Blender应用程序没有问题?我希望对这里可能发生的事情有一个大致的了解,因此欢迎有根据的猜测(当然还有准确的答案).

So my question is, what witchcraft is going on with the mysterious _bpy module, that neither I nor PyCharm can find, but that the Blender app doesn't have a problem with? I looking to gain a general understanding of what might be going on here, so educated guesses (as well as precise answers obviously) are welcome.

推荐答案

您是否偶然发现前一行 from _bpy import...表示# internal blender C module应该是赠品.

Did you happen to notice the line before from _bpy import... it says # internal blender C module which should be the giveaway.

进行这项工作的 witchcraft 是,Blender二进制文件包含_bpy作为二进制python模块,blender使此模块可在blender随附的python解释器中访问,它在初始化期间执行此操作python解释器的.如果未将普通的Blender二进制文件内置到Python模块中,则无法将其导入Blender外部的python解释器中.

The witchcraft that makes this work is that the blender binary includes _bpy as a binary python module, blender makes this module accessible within the python interpreter included with blender, it does this during the initialization of the python interpreter. The normal blender binary cannot be imported into a python interpreter outside of blender without being built into a Python module (see below).

要弄清楚如何做,您可以从 Python的c-api .您可能还想在 source/blender/python ,您将在其中找到用于构建Blender主要基于C的模块的C文件,例如bpy,bgl,bmesh,mathutils.

To figure out how to do it you can start with the Python docs on Python's c-api. You may also want to look through blender's source code within source/blender/python where you will find the C files used to build blender's main C based modules such as bpy, bgl, bmesh, mathutils.

另请参见此答案,其中包含指向将Blender构建为Python模块的信息的链接,因此可以将其导入(不带gui的代码)转换为外部Python解释器.如果您在 blender.stackexchange.com 中搜索pycharm,您将找到有关在pycharm中使用bpy的几个答案,并且eclipse,包括使Blender作为外部解释器运行以进行调试的方式.

Also see this answer which has a link to info on building blender as a Python module so it can be imported (without the gui) into an external Python interpreter. If you search for pycharm at blender.stackexchange.com you will find several answers about using bpy in pycharm and eclipse, including ways to have blender running as an external interpreter for debugging.

这篇关于Blender Python`bpy``__init __.py`,显然是从不存在的模块'_bpy'导入的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:02