Blender を Python モジュールとしてコンパイルしました。
ant@asus:~$ ls -l /home/ant/.local/lib/python3.6/site-packages/bpy/
drwxrwxr-x 3 ant ant 4096 Jun 6 14:44 2.79
-rwxrwxr-x 1 ant ant 64221200 Jun 6 14:44 bpy.so
パッケージフォルダーからpythonを実行すると、インポートは正常に機能します:
ant@asus:~/.local/lib/python3.6/site-packages/bpy/$ python3
>>> import bpy
Color management: using fallback mode for management
>>> dir(bpy)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__',
'__loader__', '__name__', '__package__', '__path__', '__spec__',
'app', 'context', 'data', 'ops', 'path', 'props', 'types', 'utils']
しかし、通常のインポートでは空のモジュールが得られます:
ant@asus:~$ python3
>>> import bpy
>>> dir(bpy)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__']
ファイルを追加しようとしました
__init__.py
異なるコンテンツ:
import bpy
ant@asus:~$ python3
>>> dir(bpy)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__', 'bpy']
>>> dir(bpy.bpy)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__', 'bpy']
import os,ctypes
basedir = os.path.abspath(os.path.dirname(__file__))
libpath = os.path.join(basedir, 'bpy.so')
dll = ctypes.CDLL(libpath)
print(dll)
ant@asus:~$ python3
>>> import bpy
<CDLL '/home/ant/.local/lib/python3.6/site-packages/bpy/bpy.so', handle d06750 at 0x7ff30917deb8>
>>> dir(bpy)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__path__', '__spec__', 'basedir',
'ctypes', 'dll', 'libpath', 'os']
>>> dir(bpy.dll)
['_FuncPtr', '__class__', '__delattr__', '__dict__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattr__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__le__', '__lt__', '__module__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__',
'_func_flags_', '_func_restype_', '_handle', '_name']
このバリアントはほとんど機能しているように見えますが、ブレンダーから多くのエラーが発生します:
from . import bpy
ant@asus:~$ python3
>>> import bpy
Color management: using fallback mode for management
RNA_string_set: OperatorProperties.data_path not found.
RNA_string_set: OperatorProperties.value not found.
...
PYTHONPATH や sys.path などを変更しようとする試みもたくさんありました。
最後に、straceを使用して解決策を見つけました-名前を変更するだけです
bpy.so -> __init__.so
これは機能しますが、あまりにもハックに見えます。では、正しい方法で行うにはどうすればよいのでしょうか。