I am trying to import a fbx-file with a skeleton. I got all the data and can import the mesh and bones correctly, this looks like this:


The problem occurs when I am trying to add weights and bindposes to the bones. The weights are correct, they deform the excepted vertices, but the mesh is deformed. See this picture:

The scales of the bones are all 1 and they are at the correct position. The hierarchy looks roughly like this:

TestObject
-------Hips
--------------L_Thigh
---------------------...
--------------Spine
---------------------...
--------------R_Thigh
---------------------...
-------MeshParent
--------------Mesh

And the code for setting the bindposes like this (simplified):

Transform[] boneTransforms = new Transform[numberOfBones];
Matrix4x4[] bindPoses = new Matrix4x4[numberOfBones];
for(int bi=0;bi<numberOfBones;bi++) {
	boneTransforms[bi] = getBoneWithIndex(bi).transform;
	bindPoses[bi] = boneTransforms[bi].worldToLocalMatrix* meshObject.transform.localToWorldMatrix;
	//Other stuff (setting boneWeights, ...)
}

In every example I found the bindPoses were computed like this, so I am not really sure what I am doing wrong … Are the bindposes the problem? How do you compute them correctly? Has it something to do with the hierarchy?

Ok, the reason was rather dumb:

I get a pointer to each object from my DLL and first generate a GameObject for each pointer, so that parenting can work. Then I import other data for the object, such as position, rotation, scale, name and the mesh-data. The problem was that I set the bindposes when importing the mesh and the parent-objects do not necessarily have their position/rotation/scale set at that time. For this reason the bindposes were not correct at the time the mesh was generated. I changed the order from:

GenerateGameObjectForEachObject();
GetTransformDataAndMeshForEachObject();

to

GenerateGameObjectForEachObject();
GetTransformDataForEachObject();
GetMeshForEachObject();

The code from my original post works as intended.