0
votes

I am new to iOS and scenekit. I have an scn scene which contains a rigged model of a human body (skin and skeleton). And two other collada (.dae) files containing a gun and an animation to move hands. How can I attach the gun to the model's hand so that it will rotate with the model's hand while animating.

I have explored SCNSkinner and SCNIKConstraint but couldn't find a way to do it. Currently the gun object's position is static and doesn't move with the hands.

let model = SCNScene(named: "Model.scnassets/model.scn")
let gun = SCNScene(named: "Model.scnassets/gun.dae")
model?.rootNode?.addChildNode(gun)

//fromFile is a custom extension
let animation = SCNAnimation.fromFile(named: "test_with_gun", inDirectory: "Model.scnassets/Animation")
if let anim = animation {
            model.addAnimation(anim, forKey: anim.keyPath)
}
1

1 Answers

1
votes

model?.rootNode?.addChildNode(gun)

This will attach the gun to the model's object whereas I'm assuming that your animation is controlled through a Skinner/Armature. The transforms are set on the armature which in turns deforms/transforms the main object.

So, making the gun a child of the armature's root bone should work.

(Writing the answer in objective-c, hopefully that's ok for you)


    SCNNode * armatureRootBone = [model.rootNode childNodeWithName:@"amt_root" recursively:YES];

    [armatureRootBone addChildNode:gun];

Additionally, if the arm takes in additional rotation and movement, then the gun needs to be a child of arm's root/control bone.