How do I make Python aware of `bpy.pyd` (Windows) internals?

I have built Blender’s API as a Python module, and produced a Release bpy.pyd. Upon placing this in Lib\site-packages\bpy. I can now import bpy successfully. However, I am unable to load any of the modules associated with bpy, for instance from bpy.types import Action, will give me an error ModuleNotFoundError: No module named 'bpy.types'. Similarly, after doing a import bpy, running a script with only the line bpy.types.Action gives the error: AttributeError: module 'bpy' has no attribute 'types'.

If I start up IPython, import bpy, then type bpy. in the console and hit Tab for auto-completion, no auto-complete options show up.

If I am interpreting the Python docs correctly, there is nothing more for me to do once I make Python aware of the bpy.pyd. So, what’s going wrong? Why is Python not aware of the “detail” contained in bpy.pyd?

For the current and improved approach see my updated answer.

You would also need to copy the DLLs and the .\2.92 directory as shown in the Wiki, but exclude the python3.dll and python37.dll.

The exact steps are:

  1. In Blender’s git repository run make bpy
  2. In the build folder you will find the bpy.pyd, BlendThumb.dll, libfftw3-3.dll, libgmp-10.dll, libgmpxx.dll, tbb.dll, tbbmalloc.dll, tbbmalloc_proxy.dll. These need to be copied into the directory where your python.exe is located, e.g. C:\Program Files\Python37.
  3. In the build folder you will find the .\2.92 subdirectory that contains the scripts and data files. This directory needs to be copied into C:\Program Files\Python37 as well.

Hi @Robert , I am trying to follow your advice, but I’m not sure it’s still the way to do it. I build my bpy. My Python executable is located at:
C:\Users\user\AppData\Local\Programs\Python\Python310\python.exe

I added the .dll files & subdirectory that contains the scripts and data files to the same folder as the executable:

I have my bpy.pyd in a folder called bpy in site-packages

From my command prompt, if I import and print the bpy module, I get:
<module 'bpy' (<_frozen_importlib_external._NamespaceLoader object at 0x00000244A05CC5B0>)>

And just like the original poster, if I print print(bpy.types.Operator), I get the error:
AttributeError: module 'bpy' has no attribute 'types'

Could you please let me know if I’m following your directions correctly and how I might get the bpy module to work in Python? Thank you!

@KevinBurke could yout try again with a recent build from master? (if you weren’t doing this already)

The final result should now be a “bpy/” directory containing __init__.pyd on windows (making it a Python package). You should not have copy DLL’s about or rename files. Besides making sure the “bpy” directory is in Python’s search path.

For a full list of recent changes see: T100913: Improve support for building Blender as a Python Module.

2 Likes

That used to be the very hacky and unclean way of getting it to work.

I have tested this just now and the current steps are as Campbell said. Since it now builds a proper module you can either add it to your PYTHONPATH environment variable or to your sys.path prior to import.

  1. Build the Python module make bpy
  2. Open a terminal and run python to get the interpreter prompt
  3. import sys and then append both the path to <some prefix>\build_windows_Bpy_x64_vc16_Release\bin\Release and <some prefix>\build_windows_Bpy_x64_vc16_Release\bin\Releas\bpy with calls to sys.path.append(<put string var here>).
  4. Import the module with import bpy

These steps are not necessary when the paths are in PYTHONPATH since Python will already check there on its own (they are automatically getting added to sys.path).

  1. Add paths to PYTHONPATH
  2. Start a Python interpreter
  3. Use import bpy
1 Like