Skip to content

Instantly share code, notes, and snippets.

@elaughli
Created December 12, 2018 22:33
Show Gist options
  • Save elaughli/43a619fbdc294e56c60fab376f37a352 to your computer and use it in GitHub Desktop.
Save elaughli/43a619fbdc294e56c60fab376f37a352 to your computer and use it in GitHub Desktop.
function to convert a saved obj file to USDZ in objective-c
// path to documents directory to save our usdc file
NSString* usdcPath = [documentsDirectory stringByAppendingPathComponent:@"usdcExample.usdc"];
NSURL *usdcUrl = [NSURL fileURLWithPath: usdcPath];
// path to documents directory to save our final usdz file
NSString* usdzPath = [documentsDirectory stringByAppendingPathComponent:@"usdzExample.usdz"];
NSURL *usdzUrl = [NSURL fileURLWithPath:usdzPath];
// load the .obj file at filePath as an MDLAsset
NSURL *url = [NSURL fileURLWithPath:filePath];
MDLAsset *asset = [[MDLAsset alloc]initWithURL:url];
// ensure the MDLAsset can write the desired extensions
// NOTE: including for example purposes.
// this is false, but ideally this would be true and would handle everything for us
if([MDLAsset canExportFileExtension:@"usdz"]){
NSLog(@"able to export as usdz");
} else {
NSLog(@"NOT able to export as usdz");
}
// usda is supported (USD ascii format)
if([MDLAsset canExportFileExtension:@"usda"]){
NSLog(@"able to export as usda");
}
// usdc is supported (USD binary format)
if([MDLAsset canExportFileExtension:@"usdc"]){
NSLog(@"able to export as usdc");
// save the usdc file
[asset exportAssetToURL:usdcUrl];
}
// rename the usdc to usdz because that's all it takes
NSError *renameErr;
NSFileManager *fm = [[NSFileManager alloc] init];
BOOL mvResult = [fm moveItemAtPath:usdcPath toPath:usdzPath error:& renameErr];
if(! mvResult){
NSLog(@"Error renaming usdz file: %@", [renameErr localizedDescription]);
}
@piemonte
Copy link

nice discovery! been needing to figure this out for a while. i made a swift version for your technique too.

📎 https://github.com/piemonte/obj2usdz/blob/master/Source/MDLAsset%2Busdz.swift

@sbuys
Copy link

sbuys commented Sep 26, 2019

👋 Curious if this still works for you. I've exported a USDC file successfully (untextured as you described), but renaming as USDZ doesn't work for me. Not sure if this is Catalina + latest XCode related, but the file shows up as empty in XCode / finder.

@elaughli
Copy link
Author

elaughli commented Sep 27, 2019

Hey thanks for the update. I've only tested this usdz creation code on iOS, and it was a bit of a hack (explainer blog post here).

Today I was able to export a mesh with this code on and iOS13 device and view it on Mojave with Preview and on iOS12 device with AR quicklook. However, I tried opening it with Xcode and Preview on a Catalina MBP and iOS13 device AR quicklook, and neither worked. I guess apple changed their implementation of USDZ readers, but I'm not sure exactly how. I'd see if it's possible to save files as USDZ natively now with ModelIO.

I'll play around with it over the weekend maybe and see what I can find.

UPDATE: I tested exporting natively with MDLAsset's canExportFileExtension, which returns false for a usdz url still and exportAssetToURL with a usdz url, which again returns false but provides no error. The developer forums are full of people struggling with usdz file creation, even some with the Xcode converter tools.

@icatSolutions
Copy link

Yeah. Tried almost all possible solutions available on the web but to no avail. Has anyone being successful to convert either an obj to usdz or even an .scn file to usdz?
I found that the structure of the .scn has to be of a certain format in order for Xcode to even export this to usdz but from within app it is impossible.
Any ideas? Or shall we just wait for WWDC 2020 ? lol

@elaughli
Copy link
Author

elaughli commented Mar 3, 2020

@icatSolutions I found another developer who was able to do a modified version of this method that basically fixes the padding either in the header or on the end of the intermediate .usdc file before renaming to .usdz. He hasn't yet published it, and I haven't dug into that myself just yet.

My suggestion if you want to play around with it is test exporting whatever it is you have as .usda and/or .usdc and verify that it works on a macbook or something that can view it on. Then if you dig through the .usda file and compare with the compressed .usdc file you should be able figure out where the padding on the end it's getting messed up for the 64 byte alignment requirement and can probably hack away at it until the simple rename to .usdz works with that particular file.

Hopefully there's a better way when WWDC rolls around 🤞

@icatSolutions
Copy link

I’ll wait for WWDC 2020. Not worth spending a minute on this...
Apple needs to give us the right tools if they want us to adopt new technologies.

@TheSeanLavery
Copy link

Anyone ever get this to work?

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