USDZ Animations with RealityKit

Has anyone been able to access multiple animations stored in a USDZ file through the RealityKit APIs?

Replies

I have the exact same question, I dowloaded the usdz models from Apple's Developer Website, the 'toy_biplane' & 'toyrobot_vintage'.
I used the following code to animate
Code Block
modelEntity.availableAnimations.forEach {
                    modelEntity.playAnimation($0.repeat())
                }

then I just printed all the available animations just to check
Code Block
print("DEBUG: animations-> \(modelEntity.availableAnimations)")

it returned an empty array


Hello Aditya7,

If you use Entity.loadModel, this will return a ModelEntity, it will not load the animations that may be in the usdz file. Instead you should use Entity.load, which will return an Entity, and will load the animations that may be in the usdz file.

Additionally, you should note that calling playAnimation before the Entity isActive in the scene will result in the resulting AnimationPlaybackController immediately setting to an isComplete status, so the animation won't play when the Entity is added to the scene.

The following snippet demonstrates how you can handle this situation:

Code Block
// Load the usdz as an Entity
        entity = try! Entity.load(contentsOf: url)
        // Add the entity to an anchor, add the anchor to the scene.  The anchor might not be anchored in the scene at this point.
        anchor.addChild(entity)
        arView.scene.anchors.append(anchor)
        // Subscribe to AnchoredStateChanged events, play the animations of the anchor's children once it is active in the scene.
        arView.scene.subscribe(to: SceneEvents.AnchoredStateChanged.self) { [self] (event) in
            if anchor.isActive {
                for entity in anchor.children {
                    for animation in entity.availableAnimations {
                        entity.playAnimation(animation.repeat())
                    }
                }
            }
        }.store(in: &cancellables) // Remember to store the cancellable!

  • this doesn't work. I tried downloading a .usdz file from the internet. it had a animation in it. I was able to see the animation in the quicklook (rightclick->quicklook) on the file. But when I tried doing it on a real device with Xcode using RealityKit and ARKit. its didn't seem to work. Is there any new guidance?

Add a Comment

this works fine. but the model and animations need to be structured in the origin modeling tool correctly. I use Blender for this. Use the NLE (nonliner editor) and make sure your objects and animations are named properly for access. then use anEnity = model.findEntity("{nameOfObject}"). then ask the anEntity?.availableAnimations for its animations. then run one or all of them.