6

I used an ios app to create a usdz file using arkit but now I want to convert the scan to a solid 3d model such as an stl or obj. is there an ios or mac application that allows for this conversion. I cannot find any app that can open them other than xcode and preview but neither allow me to export to a 3d model.

Samson T
  • 67
  • 1
  • 5

3 Answers3

7

ModelIO can, at least in iOS.

import ModelIO

let usdz = URL(fileURLWithPath: "model.usdz")
let asset = MDLAsset(url: usdz)
let stl = URL(fileURLWithPath: "model.stl")
try asset.export(to: stl)

.usdz is just a zipped .usdc file with textures (from the offical documentation). If you want to export the file on a mac, change the file extension to .zip, unzip the file and use ModelIO to export a .stl file.

Open Xcode, create a new Playground, choose macOS as the platform. I have downloaded a test file from Apple's Quick Look Gallery, and unzipped an example to stratocoaster_usdz/ in my Downlaods directory. Then use the following code:

import ModelIO

// Get the path to the Downloads directory in your home folder
let directory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
let usdc = URL(fileURLWithPath: "stratocaster_usdz/Stratocaster.usdc", relativeTo: directory)

// Load the usdc file that was packed in the usdz file
let asset = MDLAsset(url: usdc)

do {
    let target = URL(fileURLWithPath: "stratocaster.stl", relativeTo: directory)
    // Export to .stl
    try asset.export(to: target)
} catch {
    error
}
jlsiewert
  • 3,494
  • 18
  • 41
6

Thanks! Exactly what I was looking for. I created a Mac OS script to automate this in Terminal. I also did something similar in an Automator Quick Action to run from Finder with a RightClick on the file, but couldn't figure out a way to attach that here.

#!/usr/bin/env osascript -l JavaScript

// Save to a file, e.g. usdz2stl.js
// chmod +x usdz2stl.js
// usdz2stl.js /path/to/model.usdz

ObjC.import('ModelIO')

function run(argv) {
  var inFile = argv[0];
  var outFile = inFile + ".stl";
  console.log("Converting " + inFile + " ...");
  var usdz = $.NSURL.alloc.initFileURLWithPath(inFile);
  var asset = $.MDLAsset.alloc.initWithURL(usdz);
  var stl = $.NSURL.alloc.initFileURLWithPath(outFile);
  asset.exportAssetToURL(stl);
  console.log("Conversion Complete: " + outFile);
}
wr0ngway
  • 198
  • 2
  • 5
  • i've tryd your script. unfortunatelly I get an error: ./usdz2stl.js:440:489: script error: Error on line 16: SyntaxError: Unexpected end of script (-2700). Got it: I forgot to copy the } at the end – Jonathan Apr 22 '21 at 12:44
4

Simply directly open .USDZ or USDC file in XCODE

If you have unzipped the .zip you will keep an .USDC file and a folder called "0" which contains all the textures, materials, alpha in .PNG.

Once opened .USDZ or .USDC in XCODE >> File >> Export

You can directly choose the main .OBJ .STL formats and many others

enter image description here

ButchMonkey
  • 1,873
  • 18
  • 30