Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error:Bone weights do not match bones.

Discussion in 'Animation' started by zhutianlun810, Mar 23, 2021.

  1. zhutianlun810

    zhutianlun810

    Joined:
    Sep 17, 2017
    Posts:
    165
    Hello,

    I am trying to copy bones and boneweights from one skinnedMeshRenderer to another skinnedMeshRenderer. The meshes of them have same number of vertices and triangles. However, When I copy the boneweights. It gives me the error "Bone weights do not match bones." and "Bone index is not within the number of bones.". Before that, I've successfully copied the bones. They should have same bones.

    This is the code I have:
    Code (CSharp):
    1.     private void SetBoneWeight()
    2.     {
    3.         if (glove_1 != null && glove_2 != null)
    4.         {
    5.             SkinnedMeshRenderer[] gloves_1_Renderers = glove_1.GetComponentsInChildren<SkinnedMeshRenderer>();
    6.             SkinnedMeshRenderer[] gloves_2_Renderers = glove_2.GetComponentsInChildren<SkinnedMeshRenderer>();
    7.  
    8.             for (int i = 0; i < gloves_1_Renderers.Length; i++)
    9.             {
    10.  
    11.                 BoneWeight[] boneWeights = gloves_1_Renderers[i].sharedMesh.boneWeights;
    12.              
    13.                 BoneWeight[] result = new BoneWeight[boneWeights.Length];
    14.                 for (int j = 0; j < boneWeights.Length; j++)
    15.                 {
    16.                     result[j].boneIndex0 = boneWeights[j].boneIndex0;
    17.                     if (result[j].boneIndex0 < 0 || result[j].boneIndex0 >= gloves_2_Renderers[i].bones.Length)
    18.                     {
    19.                         Debug.Log(result[j].boneIndex0);
    20.                     }
    21.                     result[j].boneIndex1 = boneWeights[j].boneIndex1;
    22.                     if (result[j].boneIndex1 < 0 || result[j].boneIndex1 >= gloves_2_Renderers[i].bones.Length)
    23.                     {
    24.                         Debug.Log(result[j].boneIndex1);
    25.                     }
    26.                     result[j].boneIndex2 = boneWeights[j].boneIndex2;
    27.                     if (result[j].boneIndex2 < 0 || result[j].boneIndex2 >= gloves_2_Renderers[i].bones.Length)
    28.                     {
    29.                         Debug.Log(result[j].boneIndex2);
    30.                     }
    31.                     result[j].boneIndex3 = boneWeights[j].boneIndex3;
    32.                     if (result[j].boneIndex3 < 0 || result[j].boneIndex3 >= gloves_2_Renderers[i].bones.Length)
    33.                     {
    34.                         Debug.Log(result[j].boneIndex3);
    35.                     }
    36.                     result[j].weight0 = boneWeights[j].weight0;
    37.                     result[j].weight1 = boneWeights[j].weight1;
    38.                     result[j].weight2 = boneWeights[j].weight2;
    39.                     result[j].weight3 = boneWeights[j].weight3;
    40.                 }
    41.              
    42.                 gloves_2_Renderers[i].sharedMesh.boneWeights = result;
    43.             }
    44.         }
    45.     }
    There is no log printed. I believe all boneIndex is in the range.
    I have no idea about these errors. What do they mean?
     
    Last edited: Mar 23, 2021