Home > Blockchain >  Available animations collection is empty, while using .loadModelAsync()
Available animations collection is empty, while using .loadModelAsync()

Time:02-12

I am successfully loading USDZ file in my RealityKit scene asynchronously by using LoadModelAsync. I am using toy_biplane USDZ file provided by apple which has default animations. If I load model asynchronousely availableAnimations array is always empty!

My codes:

func loadASingleModel(name: String, 
                completion: @escaping (_ model: ModelEntity?) -> Void) {
        
    var cancellable: AnyCancellable?
    cancellable = Entity.loadModelAsync(named: name)
        .sink(receiveCompletion: { handler in
            if case let .failure(error) = handler {
                print("Unable to load a model due to error \(error)")
            }
            cancellable?.cancel()
            completion(nil)

        }, receiveValue: { [self] (model: ModelEntity?) in
            if let model = model  {
                cancellable?.cancel()
                print("Congrats! Model is successfully loaded!")
                print(model.availableAnimations, 
                      //Here availableAnimations is empty
                      "Debug: Any Animation?")
                            completion(model)
            }
        })
    }
}

How to load USDZ file asynchronously with animations?

CodePudding user response:

Use ModelEntity.loadAsync. It loads full hierarchy including animation.

import RealityKit
import Combine

let anchor = AnchorEntity()
var cancellable: AnyCancellable? = nil


self.cancellable = ModelEntity.loadAsync(named: "biplane.usdz").sink(
        
    receiveCompletion: { completion in

        if case let .failure(error) = completion {
            print("Unable to load a model \(error)")
        }
        self.cancellable?.cancel()
    }, 

    receiveValue: { [self] planeModel in

        let animation = planeModel.availableAnimations[0]
            
        anchor.addChild(planeModel)
        anchor.position = [0, 0,-2]
        anchor.scale = [0.1, 0.1, 0.1]
        arView.scene.anchors.append(anchor)
                
        planeModel.playAnimation(animation.repeat(count: .max))
    }
)
  • Related