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

Time:01-18

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 ?

CodePudding user response:

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

        // this model was converted from FBX model in Reality Converter
        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) { }
}

CodePudding user response:

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.

  • Related