3

I made a simple cube model with animation in Blender. Exported it as a .fbx file with the option bake animation turned on.

With Reality Converter I converted the .fbx file to .usdz which I imported into my Xcode project. But I am not getting the animation back in my project (see below for the code).

import SwiftUI
import RealityKit

struct ContentView : View {
    var body: some View {
        ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}

struct ARViewContainer: UIViewRepresentable {
    
    let arView = ARView(frame: .zero)
    
    func makeUIView(context: Context) -> ARView {
        
        let anchorEntity = AnchorEntity()
        
        //get local usdz file which is in xcode project
        do {               
            let cubeModel = try ModelEntity.load(named: "cube")
            print(cubeModel)                
            print(cubeModel.availableAnimations) // here i get an empty array

            anchorEntity.addChild(cubeModel)               
            arView.scene.addAnchor(anchorEntity)
          }
          catch {
             print(error)
          }
        return arView   // the cube is visible on my ipad  
    }        
    func updateUIView(_ uiView: ARView, context: Context) { }
}

For what i understand is has to be possible to import with animations. Am i missing something ?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
saro
  • 705
  • 3
  • 13

2 Answers2

2

FBX model with character animation

Make sure you exported a model from Blender with enabled animation. To play animation in RealityKit use AnimationPlaybackController object. To test a character animation in RealityKit use this fbx model.

To convert .fbx model to .usdz use Reality Converter.

enter image description here

import SwiftUI
import RealityKit

struct ContentView : View {
    var body: some View {
        ARViewContainer()
            .ignoresSafeArea()
    }
}

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {

        let arView = ARView(frame: .zero)
        let model = try! Entity.loadModel(named: "walking.usdz")
        let anchor = AnchorEntity(world: [0,-1,-2])
        model.setParent(anchor)
        arView.scene.anchors.append(anchor)

        let animation: AnimationResource = model.availableAnimations[0]
        let controller = model.playAnimation(animation.repeat())
        controller.blendFactor = 0.5
        controller.speed = 1.7

        return arView
    }
    func updateUIView(_ view: ARView, context: Context) { }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • this is not working, no animations in the animationslist of the object with fbx format – saro Jan 18 '23 at 14:27
  • 1
    Yep, this model is working with my code, so with ModelEntity.load. Conclusion is that my own model from Blender is not right. So i have a cube and a plane under it, the cube is falling on the plane. Thats all, i select fbx and baked animation. I dont do anything with keyframes in blender, probably i have to dig in blender. – saro Jan 18 '23 at 17:12
  • If so, it seems it's really Blender's issue. – Andy Jazz Jan 18 '23 at 17:15
  • 1
    Yes, but the usd format in blender works also after converting it with realityconverter. – saro Jan 18 '23 at 18:04
0

I found the answer in blender, i did the export in USD format with animations on. Then convert the file in realityconverter to usdz format, now the model and the animations are in my xcode project.

saro
  • 705
  • 3
  • 13