# How to export with python a vertex's position before bone weights are applied

I have modified the obj export script for all my needs so far but I am having trouble with the Math in regards to how I get a vertex's position before you apply multiple bone matrix's (with their weights).

I can get it working with 1 bone matrix (weight of 1) but not with multiple bones (max 3) with a total weight of 1 (eg 3 bones with 0.33333 weight each).

I have tried many different combos but unable to find a working order.

Can you please point me to a source of how to export vertex's before bones and their weights are applied or what I have done wrong/need to do? (so the vertex shader does this during runtime)

Below is the code I am using for relevant parts if I have missed something you need let me know and I will add :D

Bone Matrix exporting:

oArmature = OBJECT.find_armature()
oBones = oArmature.pose.bones
for oBone in oBones:
BoneMatrix = EXPORT_GLOBAL_MATRIX * oArmature.matrix_world *  oBone.bone.matrix_local
oBoneName2Matrix[oBone.name] = BoneMatrix


Vertex Exporting:

#in the vertex loop
oVMatrix = mathutils.Matrix()
oVMatrix.zero()
for oWeight in oVWeights:
oMatrix = oBoneName2Matrix[oWeight.sBoneName] #tried adding .inverted() as well
oVMatrix += oMatrix * oWeight.fWeight
export = v.co * oVMatrix #tried adding .inverted() as well
#export = oVMatrix * v.co #tried adding .inverted() as well


layout (location = 0) in vec3 Vertex_Position;
layout (location = 2) in uvec3 BoneID;
layout (location = 3) in vec3 Vertex_Weight;
uniform mat4 BoneMatrixs[10];

mat4 TotalBones = mat4(0);
for(int i = 0; i < 3; ++i)
TotalBones += (BoneMatrixs[BoneID[i]] * Vertex_Weight[i]);

vec4 VertexPos_after_bones = TotalBones * vec4(Vertex_Position, 1.0);


Update: i am wanting to transform the final vertex position in blender to its position before any of the 3 bone matrices are applied.

vertex shader pov: ((bonematrix[0] * boneweight[0]) + (bonematrix[1] * boneweight[1]) + (bonematrix[2] * boneweight[2])) * vertex_pos_before_bones

i need to know how to get from the final vertex pos in blender to vertex_pos_before_bones but i can't get the math to work.

• Do you mean the completely unaffected mesh, or with only one posebone applied to the vertices (the one with the highest weight)? – CoDEmanX Apr 9 '14 at 5:20
• I need the vertex's position before any bones matrix's (and weights) are applied, so if it was a max of 1 bone its the vertex's position in bone space I need. So maybe its called the same thing for multiple bones as well. for 1 bone i did: (EXPORT_GLOBAL_MATRIX * oArmature.matrix_world * oBone.matrix).inverted() * v.co – ZeePal Apr 9 '14 at 5:29
• The rest pose? Try oArmature.data.bones and use .matrix of the bones. – CoDEmanX Apr 9 '14 at 6:20
• doesnt that translate a vertex from its final pos to bones space of 1 bone? I need to go to the bone space of all 3 bones effecting the vertex. I am exporting this data so my wip game engine can draw the models but when i use more than 1 bone per vertex the multi bone vertexs all end up at the center and not in the correct shape – ZeePal Apr 9 '14 at 6:57
• Just updated the question with some more info. Hope it helps – ZeePal Apr 9 '14 at 7:21