SceneKit loads node with animation from a separate scn file

I have a view that dynamically creates an SCNView. This scene is empty, but when I click the button, I would like to add node from a separate scn file. This file contains animation, and I would like it to be animated in the main scene. The problem is that after adding an object to the scene, it does not revive. When I use this file as an SCNView scene, it works. isPlaying and loop are included. What else do I need to do to import such a node with animation? Sample code below:

override func viewDidLoad() {
    super.viewDidLoad()

    let scene = SCNScene()
    let sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
    sceneView.scene = scene
    sceneView.loops = true
    sceneView.isPlaying = true
    sceneView.autoenablesDefaultLighting = true
    view.addSubview(sceneView)


    let subNodeScene = SCNScene(named: "Serah_Animated.scn")!
    let serah = subNodeScene.rootNode.childNode(withName: "main", recursively: false)!

    scene.rootNode.addChildNode(serah)


}
+4
source share
2 answers

All you need is to wait for the animation:

        [childNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {
        for(NSString *key in child.animationKeys) {               // for every animation key
            CAAnimation *animation = [child animationForKey:key]; // get the animation
            animation.usesSceneTimeBase = NO;                     // make it system time based
            animation.repeatCount = FLT_MAX;                      // make it repeat forever
            [child addAnimation:animation forKey:key];            // animations are copied upon addition, so we have to replace the previous animation
        }
    }];
+4
source

Serah_Animated.scn, CAAnimation. .

let animScene = SCNSceneSource(url:<<URL to your scene file", options:<<Scene Loading Options>>)
let animation:CAAnimation = animScene.entryWithIdentifier(<<animID>>, withClass:CAAnimation.self)

animID .scn, Xcode, .

SceneKit AnimationID from Xcode Scene Editor

node.

scene.rootNode.addAnimation(animation, forKey:<<animID>>)

, animID, node.

scene.rootNode.removeAnimation(forKey:<<animId>>)
+3

Source: https://habr.com/ru/post/1663681/


All Articles