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]);
}
@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