0

I try to make transitions between the animations of a character but I can't quite understand...

To start any of my character without animation in position T

Then I import a walking animation and add it to my character

To begin with, my character starts walking but his arms remain in T... why?

Here is my code

    import PlaygroundSupport
    import SwiftUI
    import SceneKit

    let scene = SCNScene(named: "Agnes.scn")!
    let model = scene.rootNode
    let speed:TimeInterval = 5
    var run:SCNAnimationPlayer?
    var dance:SCNAnimationPlayer?
    var idle:SCNAnimationPlayer?
    var anim:SCNAnimationPlayer?

    struct CharacterView: UIViewRepresentable {

func makeUIView(context: Context) -> SCNView {
    let scnView = SCNView(frame: .zero)
    scnView.scene = scene

scnView.autoenablesDefaultLighting = true
    scnView.allowsCameraControl = true
    return scnView
}
    
    func updateUIView(_ view: SCNView, context: Context) {
    }
}

struct ContentView: View {
    @State private var isRunning = false
    @State private var isSalsa = false
    @State private var isIdle = false

    var body: some View {
        VStack {
            CharacterView()
                .frame(height: 300)
            
            HStack {
                Button(action: {
                    anim?.stop(withBlendOutDuration: speed)
                    run?.play()
                    anim = run
                }) {
                    Text("Running")
                        .padding()
                        .background(Color.blue)
                        .foregroundColor(.white)
                        .cornerRadius(10)
                }
                
                Button(action: {
                    anim?.stop(withBlendOutDuration: speed)
                    dance?.play()
                    anim = dance
                }) {
                    Text("Salsa")
                        .padding()
                        .background(Color.green)
                        .foregroundColor(.white)
                        .cornerRadius(10)
                }
                
                Button(action: {
                    anim?.stop(withBlendOutDuration: speed)
                    idle?.play()
                    anim = idle
                }) {
                    Text("Idle")
                        .padding()
                        .background(Color.orange)
                        .foregroundColor(.white)
                        .cornerRadius(10)
                }
            }
            .padding(.top, 20)
        }
        .onAppear {
            // Mettez ici le code pour charger les animations du personnage
            run = Page_Contents.animation(name: "Running.usdz")
            dance = Page_Contents.animation(name: "Dancing.usdz")
            idle = Page_Contents.animation(name: "Idle.usdz")
            
        }
    }
}

func animation(name:String) -> SCNAnimationPlayer
{
    var animation:SCNAnimationPlayer? = nil
    
    let fichier = SCNScene( named: name )!
    fichier.rootNode.enumerateChildNodes { (child, stop) in
        if !child.animationKeys.isEmpty {
            animation = child.animationPlayer(forKey: child.animationKeys[0])
        }
    }
    
    let cle:String = name
    model.addAnimationPlayer(animation!, forKey:cle)
    animation?.animation.isRemovedOnCompletion = false
    animation?.animation.blendInDuration  = speed
    animation?.animation.blendOutDuration = speed
    animation?.animation.isRemovedOnCompletion = false
    
    animation?.stop()
    
    return animation!
}

PlaygroundPage.current.setLiveView(ContentView())

And my Playground

Thanks !

  • Seems as you would like to animate a mixamo character. You can find probably some answers here: https://stackoverflow.com/questions/75090481/how-to-get-a-ordinary-mixamo-character-animation-working-in-scenekit/75093081#75093081 – ZAY Jul 03 '23 at 08:16
  • I saw this post but it doesn't answer my problem I think :( – jeremyjoron Jul 03 '23 at 18:23
  • Well, just as an idea. I worked a lot with mixamo generated and original mixamo characters. And indeed, some of them were kind of buggy. I never observed the behaviour as you describe, but i.ex I ran into an issue, where the characters geometry was totally deformed by the mixamo animation output. another one, had an issie with the texture coordinates. this does not happen very often. I suggest you, try to download an original mixamo character, and try, if it behaves the same, as your model. – ZAY Jul 03 '23 at 20:48
  • I tried with a mixamo character directly and I have the same problem... – jeremyjoron Jul 04 '23 at 05:08

0 Answers0