Edit开发者_如何学Python (Original post below):
So I have come up with the following code. I can export the mesh, bone structure and animations. I can animate a simple skeleton. But for some reason if I animate more than one bone, something goes wrong and the arm will move in the wrong axis.
My cpp code is here: http://kyuu.co.uk/so/main.cpp
My python export code is here: http://kyuu.co.uk/so/test.py
Could someone please tell me what I am doing wrong? I think it might be something to do with the bone roll in blender. I have seen many posts about that.
Thanks.
(Original post:)
I have been working on this problem for a while now and still cannot figure out what I am missing, so I am hoping someone kind will help me :3
Right, I have something like the following code in my application:
class bone {
bone * child;
Eigen::Matrix4f local_bind_pose; // this i read from a file
Eigen::Matrix4f final_skinning_matrix; // i then transform each vertex by this matrix
// temporary variables
Eigen::Matrix4f inv_bind_pose;
Eigen::Matrix4f world_bind_pose;
}
I.e. a simple bone hierarchy.
I believe I can work out the inv_bind_pose
with:
world_bind_pose = bone.parent.world_bind_pose * local_bind_pose
inv_bind_pose = world_bind_pose.inverse()
I know that the bind_pose
must be relative to the parent bone.
I know that blender is z = up and I am using y = up.
But I cannot get this information exported from blender. I am using version 2.56.3.
Would the rotation part of the matrix be bone.matrix_local
? Would the translation part be bone.tail() - bone.head()
?
What about bone roll? It seems that does affect the result.
Some references:
http://www.gamedev.net/topic/571044-skeletal-animation-bonespace
http://blenderartists.org/forum/showthread.php?209221-calculate-bone-location-rotation-from-fcurve-animation-data
http://www.blender.org/development/release-logs/blender-240/how-armatures-work
http://code.google.com/p/gamekit/source/browse/trunk/Engine/Loaders/Blender/gkSkeletonLoader.cpp?spec=svn482&r=482
Thank-you so much!
We use blender bones extensively. I think you might find this snippet useful.
import gzip
import struct
import bpy
groups = [x.name for x in bpy.context.object.vertex_groups]
rig = bpy.context.object.parent
buf = bytearray()
buf.extend(struct.pack('ii', len(groups), 60))
for i in range(60):
bpy.context.scene.frame_set(i)
for name in groups:
base = rig.pose.bones[name].bone.matrix_local.inverted()
mat = rig.pose.bones[name].matrix @ base
x, y, z = mat.to_translation()
rw, rx, ry, rz = mat.to_quaternion()
buf.extend(struct.pack('3f4x4f', x, y, z, rx, ry, rz, rw))
open('output.rig.gz', 'wb').write(gzip.compress(buf))
This exports blender bones directly for the GPU.
Here we use it
This script can be run in Object Mode. I hope it helps.
精彩评论