Add object to AR experience dynamically

I wrote a simple ARKit app that has a hardcoded "virtual" hat into the Experience.rcproject. The hat is a .usdz file.

I want to add the functionality for the users of the app to import their own .usdz hats, in that they are not pre-hardcoded in the app. There would be a button "Upload your hat", user would click and import the .usdz file, and the hat would end up being on the user's head.

From the code perspective, the Experience model is strongly typed, so not sure how that could work:

    func updateUIView(_ uiView: ARView, context: Context) {
        let arConfiguration = ARFaceTrackingConfiguration()
        uiView.session.run(arConfiguration,
                           options:[.resetTracking, .removeExistingAnchors])
        
        let arAnchor = try! Experience.loadHat() // I want this to happen dynamically depending on the imported file from the UI
        uiView.scene.anchors.append(arAnchor)
    }

Is adding a .usdz file dynamically to Experience somehow possible?

It would be like any file, wouldn’t it? I’d probably setup a dedicated folder for the app’s ICloud drive and pick it up from there. Plus that interface allows the user to select it from anywhere on their drive. Look up UIDocumentPickerViewController.

The above code example loads a Reality Composer experience, but in your case, you'd want to load a USDZ asset. Check out this article: Loading Entities from a File.

Add object to AR experience dynamically
 
 
Q