swiftrealitykitusdz

Can't play USDZ animation with RealityKit


I've followed the answer in this SO question regarding playing the animation of a USDZ file with the following code:

@IBOutlet var arView: ARView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    arView = ARView(frame: self.view.frame)
    self.view.addSubview(arView)
    arView.cameraMode = .nonAR
    let newAnchor = AnchorEntity(world: .zero)
    let newEnt = try! Entity.load(named: "Vintage Toy Robot")
    newAnchor.addChild(newEnt)
    arView.scene.addAnchor(newAnchor)
    
    print(newEnt.availableAnimations)
    
    for anim in newEnt.availableAnimations {
        newEnt.playAnimation(anim.repeat(duration: .infinity), 
                             transitionDuration: 1.25, startsPaused: false)
    }
}

However, the animation does not play, the USDZ file is just static.


Solution

  • Download robot model with animation from AR Quick Look page. Your code is working. Do not use not-animated robot from Reality Composer library.

    let entity = try! Entity.load(named: "toy_robot_vintage.usdz")
    

    And delete these two lines of code (you've got 2 ARViews, it's blocking the animation):

    arView = ARView(frame: self.view.frame)
    self.view.addSubview(arView)