Using bpy (Blender as a python module) as an easily downloadable python package

Hi there!

I’m new to this form but I’ve been around the Blender Stack Exchange and have experience with add-on development.

I have successfully converted Blender 3.2 into a python module on Windows 10 and can import it into my Python 3.10 scripts and run bpy commands.

I’ve posted a more detailed version of my setup on the Blender stack exchange: scripting - How to convert Blender as a Python module into an easily downloadable package? - Blender Stack Exchange

My question is; How do I create a downloadable package like Python’s requests module? (it doesn’t have to be on pypi, just downloadable from a git repo)

My goal is to deploy this bpy module to a Windows server running docker with a Django Rest_Framework API. I want to be able to tell DockerFile: Download bpy3.2 at this GitHub link when deployed or possibly add it to a requirements.txt file. What I don’t understand is how the file structure of the folders above will affect the download and implementation into python310. Would it be easier to just use a Virtual Env and copy it to the container somehow?

Any help would be appreciated, thank you for your time!

1 Like

I believe there are other more python oriented forms that can handle the how. There might be a dev with experience how to set it up here.

The blender as py module isn’t cross platform when using inside docker you might want to compile it in Linux against the libs of the docker container you’re using. After that if your idea is to upload the binaries to a git repro would perhaps explode in size.

What I have done for blender101 is to build the python module inside a docker container and push the created docker image to a docker repro.

In the later you can just base your docker file from the pushed image. Using requirements.txt might be a lot of work and management issues need to be overcome.

You could

  • build bpy with portable option ON,
  • with a convenient installation location
  • zip up the whole installation folder
  • and upload it to github releases of any of your repos.

On the docker, download and extract it anywhere and change sys.path or PYTHONPATH or something alike to point to the extracted module.

Though on windows odd that 3.2 folder is in python310. On mac it goes in site-packages/Resources/3.2 IIRC.

You might want to see ⚙ D9230 Blender as a Python Module - Extension Module Packaging Script and github repos of Tyler.

2 Likes

Ah thank you @ankitm and @JeroenBakker! That’s some great insight, I’ve taken a look at Tyler’s repos and some of the stuff he has there is really useful.

From the above I think the best approach is to do what @ankitm said and zip the 3.2 folder, bpy.pyd, and .dll files on a repo, then download them on the deployed server. After they are downloaded it would just be a mater of installing the appropriate files into the python310 directory.

My initial thinking was to package everything into a virtualenv, however as @ankitm mentioned, the 3.2 folder needs to be installed directly to python310. For some reason it doesn’t work if its in the venv folder.

Thank you all for your help! I’m going to give this a try and update once it’s working :slight_smile:

@ankitm Just to clarify, do you mean zip the entire blender-git folder shown here:
image

Or just the build_windows_Bpy_x64_vc17_Release folder?

I’m afraid you’ll have to use cmake and not rely solely on make.bat for this case.

cmake -C./build_files/cmake/config/bpy_module.cmake -DWITH_INSTALL_PORTABLE=ON -DCMAKE_INSTALL_PREFIX=../install_folder  -S . -B ../build_folder

Adjust it for windows though…

set in the command above

set in the command above

zip install_folder.

This folder has build artifacts that are not needed at all for a functional bpy.so elsewhere.

@ankitm Oh interesting, thank you. I tried using the following command for windows:

cd C:\blender-git\blender
cmake -C.\build_files\cmake\config\bpy_module.cmake -DWITH_INSTALL_PORTABLE=ON -DCMAKE_INSTALL_PREFIX=..\install_folder  -S . -B ..\build_folder

And it did create the build_folder, however install_folder was never created:
Screenshot 2022-05-06 171256

Is this the correct folder to zip:

It doesn’t contain any .py or bpy related files so I’m confused. If it is the right folder, could you elaborate a bit more on what I should do after it’s extracted on the server; Should the 3.2 folder and .pyd/.dll files all be in the python310 path on the server as well? Then it’s just a matter of setting the environment variable pythonpath = C:\path\to\build_folder?

Just to recap the steps I’ve done so far:

  1. Git clone into Blender using git clone https://github.com/blender/blender.git
  2. Build blender dependencies using cd blender then make update
  3. Run make bpy
  4. Run:
cd C:\blender-git\blender
cmake -C.\build_files\cmake\config\bpy_module.cmake -DWITH_INSTALL_PORTABLE=ON -DCMAKE_INSTALL_PREFIX=..\install_folder  -S . -B ..\build_folder

Should my next steps be the following?
5. Upload 3.2, bpy.pyd and *.dll from C:\blender-git\build_windows_Bpy_x64_vc17_Release\bin\Release to the Windows server. (3.2 in python310, bpy.pyd and *.dll to site-packages)
6. Upload build_folder to the Windows server. Then set the pythonpath environment variable to C:\path\to\build_folder.
7. Profit :wink:

Just thought I’d lay out my plan incase anyone else finds this thread in the future. Thank you for all your help and I apologies for all the questions. I really appreciate your time!

you also have to build and install after cmake created the folder.
Step 4 also contains:

cmake --build ../build_folder --target install

Also step 3 can be removed.

Step 5 would be removed also.

step 6 should be to upload install_folder

Ok awesome, I think I got it to somewhat work. I’m making progress I guess :sweat_smile:. Pycharm is now recognizing the install_folder as a library root after I set PYTHONPATH = C:\install_folder:
Screenshot 2022-05-07 101710
So that’s good I think.

However it still doesn’t recognize bpy as a module with import bpy:

Screenshot 2022-05-07 101950

I also noticed that there was no bpy.pyd file in the install_folder, could that be an issue?

1 Like

bpy support on windows is not a priority so it seems install is not working correctly with a different install location.

you can probably find bpy.pyd in build folder and copy it manually to install_folder.

for modulenotfound error, I’m not familiar with windows imports… so up to you to fix it.

After which it’s probably a matter of putting 3.2 folder relative to bpy.pyd such that import succeeds.
Those relative locations should be the same as non-portable build you showed in scripting - How to convert Blender as a Python module into an easily downloadable package? - Blender Stack Exchange