1

I initialize my Scene like this

// Load COLLADA Character
let myScene = SCNScene(named: "Characters.scnassets/Police/Police.dae")

// Recurse through all the child nodes in the Character and add to characterNode
for node in myScene!.rootNode.childNodes as [SCNNode]
{
    characterNode.addChildNode(node)
}

// Add characterNode to scene
self.rootNode.addChildNode(characterNode)

Is it possible to add an animation to characterNode from an external DAE? It is autorigged through Mixamo.

Fredrik
  • 3,027
  • 8
  • 33
  • 66
  • 1
    For this old question, the situation has now changed considerably. A full and complete answer is here: https://stackoverflow.com/a/75093081/294884 – Fattie Jan 18 '23 at 16:26
  • Does this answer your question? [How to get a ordinary Mixamo character animation working in SceneKit?](https://stackoverflow.com/questions/75090481/how-to-get-a-ordinary-mixamo-character-animation-working-in-scenekit) – Fattie Jan 18 '23 at 16:28

1 Answers1

2

Apple has an example in their Fox Scenekit app.

The following function loads an animation from your art.scnassets folder:

- (CAAnimation *)animationFromSceneNamed:(NSString *)path {
    SCNScene *scene = [SCNScene sceneNamed:path];
    __block CAAnimation *animation = nil;

    [scene.rootNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {
        if (child.animationKeys.count > 0) {
            animation = [child animationForKey:child.animationKeys[0]];
            *stop = YES;
        }
    }];

    return animation;
}

Which you can then add to your characterNode:

CAAnimation *animation = [self animationFromSceneNamed:@"art.scnassets/characterAnim.scn"];
[characterNode addAnimation:animation forKey:@"characterAnim"];

This should be the equivalent function in Swift, but I haven't had a chance to test it.

func animationFromSceneNamed(path: String) -> CAAnimation? {
    let scene  = SCNScene(named: path)
    var animation:CAAnimation?
    scene?.rootNode.enumerateChildNodes({ child, stop in
        if let animKey = child.animationKeys.first {
            animation = child.animation(forKey: animKey)
            stop.pointee = true
        }
    })
    return animation
}
James P
  • 4,786
  • 2
  • 35
  • 52
  • Thanks for the answer! I use Swift (which i never stated). So i found the corresponding function (animationWithSceneNamed) in [link](https://developer.apple.com/library/prerelease/content/samplecode/Fox/Listings/Swift_Common_SceneKitExtensions_swift.html#//apple_ref/doc/uid/TP40016154-Swift_Common_SceneKitExtensions_swift-DontLinkElementID_27) However, I get the error **Value of type 'SCNNode' has no member 'enumerateChildNodes'** when entering this code. – Fredrik Jul 27 '16 at 20:27
  • I think that sample code is quite old now, I've converted the objective-c function to Swift, see if that works for you. – James P Jul 27 '16 at 20:40
  • Thanks! Is there any way to add the animation to all keys? My object has "Body, Bottoms, Top" etc. Not sure exactly what "Key" means. – Fredrik Jul 27 '16 at 20:45
  • Okay, so it seems like the animation is working. However, the whole model/object is moving. The arms, legs etc as a whole. [Link to gif](https://gyazo.com/b4a7e571f45c4dbd7b31b8324ed952a7) – Fredrik Jul 27 '16 at 20:52
  • You could have the animation attached to the wrong node. I found it needed a lot of trial and error to get animations working correctly. Scenekit is very particular about the dae format and you may have to export it with different settings. This might help https://forums.developer.apple.com/thread/27744 – James P Jul 27 '16 at 20:57
  • Thanks for linking the thread. However, the armature/model gets a bit skewed when the "eye-popping" animation appears: [link to gif](https://gyazo.com/a98ee86187c5860050dff827a33ac3f5) I exported both the animations and the model from Blender using DAE. Then i ran the script provided in the other discussion/forum. Why is the officer so thin? And the feet + eyes are crazy! – Fredrik Jul 27 '16 at 21:33
  • It's hard to say, but I've had similar happen. In my case I think the animation file didn't quite match up to the skeleton, for some reason there were extra nodes that I had to go through and delete manually. It took a few tries to get it right. – James P Jul 27 '16 at 21:46
  • Marked your comment as the answer since it solved the question. And the new question is probably camera related. – Fredrik Jul 28 '16 at 09:53
  • I found this tutorial may address the problem with the animation doing funky things when exported from Blender to Scenekit. https://youtu.be/qnbrRCe62cM?t=9m27s – Ryan Pfister Aug 03 '17 at 20:47
  • Note that this excellent answer is now somewhat out of date. Full info .. https://stackoverflow.com/a/75093081/294884 – Fattie Jan 18 '23 at 16:28