When using clipAction on a cloned animation fbx on a cloned character model, I get the following error:
Uncaught (in promise) TypeError: root.skeleton.getBoneByName is not a function
at Function.findNode (three.js:31518)
at new PropertyBinding (three.js:31447)
at Function.create (three.js:31456)
at AnimationMixer._bindAction (three.js:32670)
at AnimationMixer.clipAction (three.js:33007)
After console.log the characterModel, there seemed to be no issues in the object, it looked like a normal Group, so what is causing this error?
Here is the clone method brought from here: Clone an already-loaded FBX model in three.js and be able to animate it. Adapted from: https://gist.github.com/cdata/f2d7a6ccdec071839bc1954c32595e87 ยท GitHub
const cloneFbx = (fbx) => {
const clone = fbx.clone(true)
clone.animations = fbx.animations
clone.skeleton = { bones: [] }
const skinnedMeshes = {}
fbx.traverse(node => {
if (node.isSkinnedMesh) {
skinnedMeshes[node.name] = node
}
})
const cloneBones = {}
const cloneSkinnedMeshes = {}
clone.traverse(node => {
if (node.isBone) {
cloneBones[node.name] = node
}
if (node.isSkinnedMesh) {
cloneSkinnedMeshes[node.name] = node
}
})
for (let name in skinnedMeshes) {
const skinnedMesh = skinnedMeshes[name]
const skeleton = skinnedMesh.skeleton
const cloneSkinnedMesh = cloneSkinnedMeshes[name]
const orderedCloneBones = []
for (let i=0; i<skeleton.bones.length; i++) {
const cloneBone = cloneBones[skeleton.bones[i].name]
orderedCloneBones.push(cloneBone)
}
cloneSkinnedMesh.bind(
new THREE.Skeleton(orderedCloneBones, skeleton.boneInverses),
cloneSkinnedMesh.matrixWorld)
// For animation to work correctly:
clone.skeleton.bones.push(cloneSkinnedMesh)
clone.skeleton.bones.push(...orderedCloneBones)
}
return clone
}
and the code causing the error
const characterModel = cloneFbx(characterModelRaw);
const deathAnimation = cloneFbx(animationFBXs["death.fbx"]);
const deathAnimationMixer = new THREE.AnimationMixer(characterModel);
const death = deathAnimationMixer.clipAction(deathAnimation.animations[0]);
animationFBXs
is an object mapping the name of the animation to the imported FBX file of the animation. And characterModelRaw
is an imported character model.