1

How to use multiple animations for a USDZ 3D character in RealityKit?

Play an idle animation, when it walks, when it jumps, etc.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220

1 Answers1

1

Switch between Multiple Animations in RealityKit

In RealityKit 2023, you can switch between multiple animations like idle, dance or jump, but for that you need the same skeletal structure in each USDZ file. To implement it, you have to put all the animations from several animated USDZ models into AnimationResource collection, and then apply one animation at a time to a character with an identical skeleton. Blending works perfectly here.

You can test my code with Mixamo animations, where each character has an identical skeleton.

I downloaded several animated Mixamo models in .dae format two-three years ago, and after that made a DAE -> SCN -> USDZ conversion using a code published in this post.

enter image description here

import SwiftUI
import RealityKit

struct ContentView : View {

    @State var animationResource: [AnimationResource] = []
    @State var model1 = try! Entity.loadModel(named: "idle.usdz")
    @State var model2 = try! Entity.loadModel(named: "jump.usdz")
    
    var body : some View {
        ZStack {
            ARViewContainer(model: $model1)
                .ignoresSafeArea()
                .onAppear {
                    animationResource.append(model1.availableAnimations[0])
                    animationResource.append(model2.availableAnimations[0])
                }
            VStack {
                Spacer()
                HStack {
                    Spacer()
                    Button("Jump") {
                        model1.playAnimation(animationResource[1].repeat(),
                                             transitionDuration: 0.5)
                    }
                    Spacer()
                    Button("Neutral") {
                        model1.playAnimation(animationResource[0].repeat(),
                                             transitionDuration: 0.5)
                    }
                    Spacer()
                }
            }
        }
    }
}

struct ARViewContainer : UIViewRepresentable {

    @Binding var model: ModelEntity
    let arView = ARView(frame: .zero)
    let anchor = AnchorEntity()

    func makeUIView(context: Context) -> ARView {
        model.scale /= 120
        model.position.y = -0.7
        anchor.addChild(model)
        arView.scene.anchors.append(anchor)
        return arView
    }
    func updateUIView(_ view: ARView, context: Context) { }
}

#Preview {
    ContentView()
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    I don’t understand… It doesn't work for me… [This is my playground](https://www.icloud.com/iclouddrive/02ai5ftzm52T2dCEv0xtjizOw#RealityKit) – jeremyjoron Jul 03 '23 at 05:55
  • @jeremyjoron, your `Salsa.usdz` file is damaged or corrupted. Or its skeleton is different from `Idle.usdz` file. Try another models – I published a link to Mixamo. – Andy Jazz Jul 03 '23 at 07:57
  • I downloaded a new model directly from mixamo in fbx with 3 animations. I converted to usdz with Reality Converter and I have exactly the same problem. In addition, when I try the same manipulation with SceneKit the animations work well so I don't think it's a problem with the files… – jeremyjoron Jul 04 '23 at 05:06
  • According to messages in Xcode's console, we have a problem with a model – `Cannot find a BindPoint for any bind path: "", "", "Salsa_Dancing.Agnes.mixamorig_Hips", "Salsa_Dancing.Agnes.mixamorig_Hips.SkeletalPose.SkeletalPoses[0]"`. I agree that RealityKit is a more capricious SDK than SceneKit in animation sense. – Andy Jazz Jul 04 '23 at 08:10
  • Oh thank you ! I use Swift Playground on iPad i didn’t view this error. I will to try without mixamo… – jeremyjoron Jul 04 '23 at 12:14