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

inkplay: <p>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.</p>
<p><strong>Edited:</strong></p>
<p>Here is what I did to make pytorch work with Blender. Hopefully, this will help someone in the future.<br>
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.</p>
<ol>
<li>
<p>Download and install Blender 2.8 beta</p>
</li>
<li>
<p>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</p>
<p>D:\Blender\blender-2.80-036ec5cae4f7-win64\blender-2.80.0-git.036ec5cae4f7-<br>
windows64\2.80\python\Scripts</p>
</li>
<li>
<p>Open cmd and run it as administrator, I did it just to be safe. Then cd into</p>
<p>D:\Blender…\2.80\python\Scripts\bin\</p>
<p>This is where Blender’s default python is stored.</p>
</li>
<li>
<p>Now run the follow lines in order:</p>
<p>python -m ensurepip<br>
python -m pip install --upgrade pip setuptools wheel</p>
</li>
<li>
<p>!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:</p>
<p>D:\Blender…\2.80\python\lib\site-packages\</p>
</li>
<li>
<p>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.</p>
</li>
</ol>

rib: <p>As an alternative to manually navigating to rename Blender’s <code>site-packages/numpy</code> directory I found it can also be explicitly removed via <code>pip</code>.</p>
<p>As another reference; these were the commands I used to install pytorch on Windows:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>These were the commands I used to install pytorch on Linux:</p>
<pre><code class="lang-auto">./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
</code></pre>
<p>(In both cases making sure to run the python binary that’s distributed with Blender itself)</p>
<p>I checked the instructions <a href="https://pytorch.org/get-started/locally/" rel="nofollow noopener">here</a> to confirm the appropriate pytorch wheel url for Linux/Windows and Python 3.7 (matching the version distributed with Blender).</p>
<p>It hardly matters but I chose to generally install everything outside of Blender’s <code>site-packages</code> via <code>--user</code> just to minimize my modifications to the based installation of Blender.</p>
<p>Only tested with Blender 2.8</p>

Christoph_Heindl: <p>I’ve created a dedicated library as part of a research for this purpose:</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="32" height="32">
      <a href="https://github.com/cheind/pytorch-blender" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars1.githubusercontent.com/u/38565?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="400" height="400">

<h3><a href="https://github.com/cheind/pytorch-blender" target="_blank" rel="nofollow noopener">cheind/pytorch-blender</a></h3>

<p>Seamless integration of Blender into pytorch data sources - cheind/pytorch-blender</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>It creates N-instances of Blender, streaming via ZMQ directly into a PyTorch dataset. The related paper is here <a href="https://arxiv.org/abs/1907.01879" rel="nofollow noopener">https://arxiv.org/abs/1907.01879</a>. 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.</p>
<p>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.</p>
<p>Hope that helps.</p>

Christoph_Heindl: <p>So here is an update:</p>
<p>We’ve just released a new version of <strong>blendtorch</strong> with Blender 2.83 that runs at 60FPS (640x480xRGBA).</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/cheind/pytorch-blender" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars1.githubusercontent.com/u/38565?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/cheind/pytorch-blender" target="_blank" rel="nofollow noopener">cheind/pytorch-blender</a></h3>

<p>Seamless integration of Blender into PyTorch datasets. - cheind/pytorch-blender</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Hope you find it useful.</p>