Skip to content

Instantly share code, notes, and snippets.

@daprice
Last active March 9, 2022 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daprice/7117b74c6d6b971823f8bbbaad7fae7e to your computer and use it in GitHub Desktop.
Save daprice/7117b74c6d6b971823f8bbbaad7fae7e to your computer and use it in GitHub Desktop.
Convert an STL intended for 3D printing (Z up, mm units) to a USDZ file compatible with Apple's AR Quick Look and ARKit

This script must be placed in the root folder of the USD tools library provided by Apple on their AR Quick Look Gallery page. Requires assimp to be installed.

Command format: ./stl_to_usdz.sh input_file.stl output_file.usdz

For now, this just inserts the necessary transformations at a hardcoded line number in the USD markup, so compatibility with future versions of usdzconvert is not guaranteed.

#!/bin/bash
BASEPATH=$(dirname "$0")
export PATH=$PATH:$BASEPATH/USD:$PATH:$BASEPATH/usdzconvert;
export PYTHONPATH=$PYTHONPATH:$BASEPATH/USD/lib/python
TEMPDIR=$(mktemp -d)
assimp export "$1" $TEMPDIR/model.obj
usdzconvert $TEMPDIR/model.obj $TEMPDIR/model_orig.usdz
usdcat $TEMPDIR/model_orig.usdz -o $TEMPDIR/model.usda
sed -i.bak "17i\\
custom double3 xformOp:scale = (0.1, 0.1, 0.1)\\
uniform token[] xformOpOrder = [\"xformOp:scale\"]
" $TEMPDIR/model.usda
sed -i.bak2 's/Y/Z/g' $TEMPDIR/model.usda
usdzip "$2" --arkitAsset $TEMPDIR/model.usda
@dezman
Copy link

dezman commented Mar 9, 2022

Getting this error here with this code:

Error in 'textFileFormatYyerror' at line 3162 in file pxr/usd/sdf/textFileFormat.yy : 'syntax error at 'custom' in </model_orig> on line 17 in file /var/folders/66/6tlfb7r143v805h598hz3pdm0000gq/T/tmp.v1HTtJJR/model.usda

It does export a usdz file, but the file doesn't load.

Helpful script tho :) usdz tools download here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment