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