iosanimationswiftui3dscenekit

IOS Scene Kit not Animating asset from Blender


What I'm trying to do is show a 3D animation asset that I downloaded from Sketchfab and imported into Xcode.

Here's a video of how the asset looks in Xcode's 3D Editor:

enter image description here

Then I implemented it using SceneKit and .animationPlayer. Here’s the code:

struct SceneKitView: UIViewRepresentable {

func makeUIView(context: Context) -> SCNView {
    let view = SCNView()
    view.allowsCameraControl = false
    view.autoenablesDefaultLighting = true
    view.antialiasingMode = .multisampling2X
    view.backgroundColor = .clear
    
    if let scene = SCNScene(named: "Purple_Lightning_Rainstorm.scn") {
        
        playAnimationKeys(for: scene.rootNode)
        view.scene = scene
        
    } else {
        print("Failed to load the scene.")
    }
    
    
    return view
}

func updateUIView(_ uiView: SCNView, context: Context) {
    
}

func playAnimationKeys(for node: SCNNode) {
    let animationKeys = node.animationKeys
    for key in animationKeys {
        print("Node: \(node.name ?? "Unnamed"), Animation Key: \(key)")
        if let animationPlayer = node.animationPlayer(forKey: key) {
            animationPlayer.play()
        } else {
            print(" -> No Animation Player found for key \(key)")
        }
    }
    
    for child in node.childNodes {
        playAnimationKeys(for: child)
    }
}}

However, the animation does not play completely. It only shows once and does not finish, displaying only half of the animation. Here’s the result video:

enter image description here

Heres the code: https://github.com/godofsleepy/cloudproblem


Solution

  • Finally i found it, just add this code fix the problem

    view.scene?.isPaused = false

    and i already update the repo it should fix