Parenting object to bone (getting correct position)

I’m trying to correctly position an object when parenting it to a bone but when I add it to a bone, it gets translated.
It imports in the right position when I just import it but don’t add to a bone. Do I need to do something to zero the object before I add it to the bone?

There is the SceneUtils helper to do this:

The code is small enough that you can just replicate it in your project or you can include it from the examples.

instead of bone.add(object) you would use: SceneUtils.attach(object, object.parent, bone);
This will apply the inverse transform of bone to object, remove the object from its parent and add it to your bone keeping its original 3d position.

If the object does not have a parent yet, use: SceneUtils.attach(object, scene, bone);

1 Like

Awesome! Extremely helpful. Thanks so much :smiley:

So this works really well if my model starts in the rest pose. If I’ve moved the model at all, the object im importing will come into the scene where it would should go if the model was still in the rest pose. How do I apply whatever delta there is between the rest pose and current pose?

Not sure if this will solve the problem, I’m not too familiar with how bones work specifically.

In the attach function, you may need to call:
parent.updateMatrixWorld();
in order to update the matrix of the bone to take into account for its new transformations

1 Like

This didn’t immediately fix the problem, but I suspect this is because I’m just not implementing it correctly or not implementing all steps. Can I call updateMatrixWorld on a bone? or should I call it on the model which has the attached armature and then it will recursively be applied to all children?

Hard to debug without a working example, but the code should look like the following:

rootBone.updateMatrixWorld(true);
SceneUtils.attach(object, scene, bone);

You can try out bone.updateMatrixWorld(); but I’m not sure that it will update with it’s parent transforms properly.