Build USD and bring USD up to date in usdz tool
Tags: usd, usdpython, usdzconverter, opneusd
Published: 2024-03-01 10:25:02

We use the latest versions of OpenUSD and usdPython as of 2024/03/01.

# Introduction
- Apple's usdPython python version is stuck at 3.7.9, so build and use USD with the latest python version available for OpenUSD.
  - [USDZ tool](https://developer.apple.com/jp/augmented-reality/tools/)
  - [OpenUSD](https://github.com/PixarAnimationStudios/OpenUSD)


Available Python versions can be found [here](https://github.com/PixarAnimationStudios/OpenUSD/blob/release/VERSIONS.md).

The version as of 2024/03/01 was 3.9.13.


# Step 1 Preparation before build
- Since we are using 3.7.9 python, install the latest version with pyenv
- The latest was [3.12.2](https://www.python.org/downloads/)

```
pyenv install 3.12.2
```

```
pyenv local 3.12.2
```

In ``python``, we also want to be able to run ``python3``, so run the following
```
alias python="python3"
```

- In USD, the `usdView` command uses `pyside6`, so install it.
- You can also use `pyside2`.
```
python -m pip install pyside6
```

# Step 2 Build

- Peel off unnecessary things with option
(be patient as it takes time to build)
```
python USD/build_scripts/build_usd.py /path/to/my_usd_install_dir --no-examples --no-tests --no-docs --no-ptex --no-openvdb --no-draco --no --materialx --no-examples
```

## Dealing with build errors

- Install `pyOpenGL` to deal with the error.

```
PyOpenGL is not installed; if pip is installed, run "pip install PyOpenGL" to install it, then re-run this script.
If PyOpenGL is already installed, you may need to update your PYTHONPATH to indicate where PyOpenGL is located.
```

```
pip install PyOpenGL
```


## Step 3 Replace
- The U.S. dollar/ distribution is replaced.
After replacing, see [here](https://github.com/activeguild/USDPython/tree/main/USD).


## Step 4 Run usdzconvert

```
usdzconvert . /cube.glb ~/cube.usdz
```

I get an error.

```
Input file: /Users/xxxxxx/Documents/cube.glb
Traceback (most recent call last):
  File "/Users/xxxxxx/projects/USDPython/./usdzconvert/usdzconvert", line 860, in <module>
    errorValue = main()
  File "/Users/xxxxxx/projects/USDPython/./usdzconvert/usdzconvert", line 855, in main
    return tryProcess(sys.argv[1:])
  File "/Users/xxxxxx/projects/USDPython/./usdzconvert/usdzconvert", line 810, in tryProcess
    ret = process(argumentList)
  File "/Users/xxxxxx/projects/USDPython/./usdzconvert/usdzconvert", line 650, in process
    usdStage = usdStageWithGlTF_module.usdStageWithGlTF(srcPath, tmpPath, legacyModifier, openParameters)
  File "/Users/xxxxxx/projects/USDPython/usdzconvert/usdStageWithGlTF.py", line 1529, in usdStageWithGlTF
    return converter.makeUsdStage()
  File "/Users/xxxxxx/projects/USDPython/usdzconvert/usdStageWithGlTF.py", line 1512, in makeUsdStage
    self.processNodeChildren(self.gltf['scenes'][0]['nodes'], self.asset.getGeomPath(), None)
  File "/Users/xxxxxx/projects/USDPython/usdzconvert/usdStageWithGlTF.py", line 1399, in processNodeChildren
    self.processNode(nodeIdx, path, underSkeleton, indent)
  File "/Users/xxxxxx/projects/USDPython/usdzconvert/usdStageWithGlTF.py", line 1366, in processNode
    usdGeom = self.processMesh(nodeIdx, newPath, underSkeleton)
  File "/Users/xxxxxx/projects/USDPython/usdzconvert/usdStageWithGlTF.py", line 1316, in processMesh
    usdGeom = self.processPrimitive(nodeIdx, gltfPrimitives[0], path, skinIdx, underSkeleton)
  File "/Users/xxxxxx/projects/USDPython/usdzconvert/usdStageWithGlTF.py", line 1142, in processPrimitive
    normalPrimvar = usdGeom.CreatePrimvar('normals', Sdf.ValueTypeNames.Normal3fArray, UsdGeom.Tokens.vertex)
AttributeError: 'Mesh' object has no attribute 'CreatePrimvar'
```


Check the issue and fix `usdzconvert/usdStageWithGlTF.py`.
https://github.com/PixarAnimationStudios/OpenUSD/issues/2628
https://github.com/activeguild/USDPython/commit/482afe50eba109c7d68b082edef003af4ec2e279

Confirmation that `usdzconverter` terminates normally with the above modification.