Does any one know how to get pytorch working with Blender?

So I want to install pytorch as a third party library with blender’s packaged python which is 3.7.0 I was wondering if any of you guys uses Blender and managed it to get this to work? Can you please share your methods? Installing torch in Blender would fail and tell me I should install torch with miniconda instead. Can someone help a fellow out? I am pulling my hairs out right now.

Edited:

Here is what I did to make pytorch work with Blender. Hopefully, this will help someone in the future.
I use Blender 2.8 which supposedly comes with its own python 3 and pip. The current python 3 version is 3.7 for my blender 2.8. What I did was install pytorch directly into Blender. Here are the steps.

  1. Download and install Blender 2.8 beta

  2. Go to your PATH from windows 10 system environment variables panel and find your Blender 2.8 installation directory then add it to PATH. For me this is

    D:\Blender\blender-2.80-036ec5cae4f7-win64\blender-2.80.0-git.036ec5cae4f7-
    windows64\2.80\python\Scripts

  3. Open cmd and run it as administrator, I did it just to be safe. Then cd into

    D:\Blender…\2.80\python\Scripts\bin\

    This is where Blender’s default python is stored.

  4. Now run the follow lines in order:

    python -m ensurepip
    python -m pip install --upgrade pip setuptools wheel

  5. !IMPORTANT! Blender 2.8 now comes with a pre-installed Numpy and you need to go to Blender 2.8 site-packages dir and change the folder name. If you don’t do this after you install Pytorch it will fail. Pytorch will install another version on top of the pre existing Numpy causing version issues. The folder location of numpy and other packages is:

    D:\Blender…\2.80\python\lib\site-packages\

  6. Now you can go to the main Pytorch site and pick the correct pip3 installation instruction to run. Make sure the python version matches with Blender and not with other versions of python you have already installed. To make sure everything installed correctly you should open Blender 2.8 click on the scripting tab on top and try to import torch. If no error is shown you are now good to go.

2 Likes

As an alternative to manually navigating to rename Blender’s site-packages/numpy directory I found it can also be explicitly removed via pip.

As another reference; these were the commands I used to install pytorch on Windows:

python -m ensurepip
python -m pip install --user --upgrade pip setuptools wheel
python -m pip uninstall -y numpy
python -m pip install --user --upgrade https://download.pytorch.org/whl/cu90/torch-1.0.1-cp37-cp37m-win_amd64.whl
python -m pip install --user --upgrade torchvision

These were the commands I used to install pytorch on Linux:

./python3.7m -m ensurepip
./python3.7m -m pip install --user --upgrade pip setuptools wheel
./python3.7m -m pip uninstall -y numpy
./python3.7m -m pip install --user https://download.pytorch.org/whl/cpu/torch-1.0.1.post2-cp37-cp37m-linux_x86_64.whl
./python3.7m -m pip install --user torchvision

(In both cases making sure to run the python binary that’s distributed with Blender itself)

I checked the instructions here to confirm the appropriate pytorch wheel url for Linux/Windows and Python 3.7 (matching the version distributed with Blender).

It hardly matters but I chose to generally install everything outside of Blender’s site-packages via --user just to minimize my modifications to the based installation of Blender.

Only tested with Blender 2.8

I’ve created a dedicated library as part of a research for this purpose:

It creates N-instances of Blender, streaming via ZMQ directly into a PyTorch dataset. The related paper is here https://arxiv.org/abs/1907.01879. We used it massively generate Blender renderings for online training of a model. Additionally we created a control channel that allows the simulation to update to training needs.

Work was done for Blender 2.79, but we expect a major leap in performance when switching to 2.80 and EEVEE rendering engine. However, I haven’t looked into image streaming from EEVEE works yet.

Hope that helps.

1 Like

So here is an update:

We’ve just released a new version of blendtorch with Blender 2.83 that runs at 60FPS (640x480xRGBA).

Hope you find it useful.

1 Like