Created
December 12, 2018 22:33
-
-
Save elaughli/43a619fbdc294e56c60fab376f37a352 to your computer and use it in GitHub Desktop.
function to convert a saved obj file to USDZ in objective-c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]); | |
} |
Anyone ever get this to work?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.