Building USD for Python 3 on Windows
With the help of conda
Context
Pixar's USD 20.5 release officially brought Python 3 support, so I was excited to play around with it and some of the most recent python features.
The easiest way I know of getting USD is from the NVIDIA Developer USD Pre-built Libraries and Tools section as it provides pre-built downloads for different operating systems.
However, if you have a different python version than the one available (e.g. I want to try Python-3.8 and the Windows offered one is Python-3.6), it might not work:
(py38base) C:\>set PATH=%PATH%;B:\write\builds\usd-20.05-win64_py36_release\bin;B:\write\builds\usd-20.05-win64_py36_release\lib
(py38base) C:\>set PYTHONPATH=%PYTHONPATH%;B:\write\builds\usd-20.05-win64_py36_release\lib\python
(py38base) C:\>python -c "from pxr import Usd;print(Usd)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "B:\write\builds\usd-20.05-win64_py36_release\lib\python\pxr\Usd\__init__.py", line 24, in <module>
from . import _usd
ImportError: DLL load failed while importing _usd: The system cannot find the file specified.
So I wanted to see if I could get USD to build on a conda environment.
Disclaimer
This is not an official recipe nor a tutorial on how to use conda or USD, it is a document on how I got USD to build with conda mainly as an experiment. For more thoroughly tested build scripts and recipes, you might want to take a look at the Docker and Saturn projects referenced on usd-resources.
Building USD
For testing software versions, I use the package and environment manager conda as it isolates every install and environment modification, plus it fits my minimal python development setup.
Using usd-build-club's Building USD on Windows wiki as a foundation, these are my conda steps:
1. Create a new environment with the target python version
(base) C:\>conda create -n py38usd205build python=3.8
2. Jump into that environment
Everything following will occur inside that environment.
(base) C:\>conda activate py38usd205build
3. Conda Install Jinja2 and CMake
(py38usd205build) C:\>conda install -c anaconda jinja2 cmake
...
The following NEW packages will be INSTALLED:
cmake anaconda/win-64::cmake-3.17.2-h33f27b4_0
jinja2 anaconda/noarch::jinja2-2.11.2-py_0
...
4. Pip Install PySide2 and PyOpenGL
(py38usd205build) C:\>python -m pip install PySide2 PyOpenGL
...
Successfully installed PyOpenGL-3.1.5 PySide2-5.15.0 shiboken2-5.15.0
5. Get the Developer Command Prompt for Visual Studio 2019
I had some issues with the conda-forge vs2017_win-64 package but found rdonnelly vs2019_win-64 to be a working one. You might need to install the Visual Studio 2019 C++ compiler as well.
(py38usd205build) C:\>conda install -c rdonnelly vs2019_win-64
...
The following NEW packages will be INSTALLED:
vs2019_win-64 rdonnelly/win-64::vs2019_win-64-19.25.28614-h4025799_0
vswhere pkgs/main/win-64::vswhere-2.7.1-h21ff451_0
...
Executing transaction: done
...
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools>CALL "VC\Auxiliary\Build\vcvars64.bat" -vcvars_ver=14.25 10.0.18362.0
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.5.4
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
6. Run the build_usd python script
(py38usd205build) C:\>python "B:\write\code\git\USD\build_scripts\build_usd.py" -v "B:\write\builds\py38usd205build"
Building with settings:
USD source directory B:\write\code\git\USD
USD install directory B:\write\builds\py38usd205build
...
Python support On
Python 3: On
...
Success! To use USD, please ensure that you have:
The following in your PYTHONPATH environment variable:
B:\write\builds\py38usd205build\lib\python
The following in your PATH environment variable:
B:\write\builds\py38usd205build\bin
B:\write\builds\py38usd205build\lib
Try it out
After setting the environment variables, the python snippet that previously failed should work now:
(py38usd205build) C:\>set PYTHONPATH=%PYTHONPATH%;B:\write\builds\py38usd205build\lib\python
(py38usd205build) C:\>set PATH=%PATH%;B:\write\builds\py38usd205build\bin;B:\write\builds\py38usd205build\lib
(py38usd205build) C:\>python -c "from pxr import Usd;print(Usd)"
<module 'pxr.Usd._usd' from 'B:\\write\\builds\\py38usd205build\\lib\\python\\pxr\\Usd\\_usd.pyd'>
Great! USD tools like usdview should work now (don't forget that Windows is still marked as experimental on the official USD repo).
(py38usd205build) C:\>usdview "B:\write\code\git\USD\extras\usd\tutorials\convertingLayerFormats\Sphere.usd"
Please feel free to comment any discoveries you have made!