saro
saro

Reputation: 821

How can i import a model with animations from Blender into RealityKit?

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 ?

Upvotes: 5

Views: 1239

Answers (2)

Andy Jazz
Andy Jazz

Reputation: 58553

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) { }
}

Upvotes: 5

saro
saro

Reputation: 821

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.

Upvotes: 1

Related Questions