Three.js : add a mesh inside another mesh while keeping position

Sometimes you have a mesh positionned into a scene (a knife that has been trown in my case), and you want to put that mesh into an other mesh (the knife has been planted into a crate). Its position and rotation won't be automatically updated in that process, so the object won't stay at the same place it was before.

That's where this little function comes in handy :

/**
 * Place a mesh inside another + adapt it's position & rotation
 * @param parentMesh = the mesh hosting the other mesh
 * @param childMesh  = the mesh we want to put in the parent
 */
function addMeshInsideAnother(parentMesh, childMesh) {
  // put the child mesh inside the parent mesh
  parentMesh.add(childMesh);
  
  // place the child mesh inside the parent mesh
  // relatively to where it was in the world
  var pos = childMesh.position.clone();
  childMesh.position.copy(parentMesh.worldToLocal(pos));
  
  // rotate the child mesh inside the parent mesh
  // relatively to it's rotation in the world
  var quat = new THREE.Quaternion();
  parentMesh.getWorldQuaternion(quat);
  childMesh.applyQuaternion(quat.inverse());
}