From 02f1c9c9b90028f68be795bbd9a953adbe3a8403 Mon Sep 17 00:00:00 2001 From: sebastianhempel Date: Fri, 19 Sep 2008 17:52:58 +0000 Subject: [PATCH] bones git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@153 67173fc5-114c-0410-ac8e-9d2fd5bffc1f --- port/PyAssimp/pyassimp/pyassimp.py | 123 ++++++++++++++++++++++++++++- port/PyAssimp/pyassimp/structs.py | 28 +++---- 2 files changed, 133 insertions(+), 18 deletions(-) diff --git a/port/PyAssimp/pyassimp/pyassimp.py b/port/PyAssimp/pyassimp/pyassimp.py index cd40efa42..dd401f4a6 100644 --- a/port/PyAssimp/pyassimp/pyassimp.py +++ b/port/PyAssimp/pyassimp/pyassimp.py @@ -13,10 +13,6 @@ import helper from errors import AssimpError -#get the assimp path -LIBRARY = os.path.join(os.path.dirname(__file__), "assimp.so") - - class AssimpLib(object): """ @@ -48,6 +44,101 @@ class AssimpBase(object): return [] +class Matrix(AssimpBase): + """ + Assimp 4x4-matrix + """ + def __init__(self, matrix): + """ + Copies matrix data to this structure. + + matrix - raw matrix data + """ + m = matrix + + self.data = [ + [m.a1, m.a2, m.a3, m.a4], + [m.b1, m.b2, m.b3, m.b4], + [m.c1, m.c2, m.c3, m.c4], + [m.d1, m.d2, m.d3, m.d4], + ] + + + def __getitem__(self, index): + """ + Returns an item out of the matrix data. Use (row, column) to access + data directly or an natural number n to access the n-th row. + + index - matrix index + + result element or row + """ + try: + #tuple as index? + x, y = index + return data[x][y] + except TypeError: + #index as index + return data[index] + + + def __setitem__(self, index, value): + """ + Sets an item of the matrix data. Use (row, column) to access + data directly or an natural number n to access the n-th row. + + index - matrix index + value - new value + """ + try: + #tuple as index? + x, y = index + data[x][y] = value + except TypeError: + #index as index + data[index] = value + + +class VertexWeight(AssimpBase): + """ + Weight for vertices. + """ + + def __init__(self, weight): + """ + Copies vertex weights to this structure. + + weight - new weight + """ + #corresponding vertex id + self.vertex = weight.mVertexId + + #my weight + self.weight = weight.mWeight + + +class Bone(AssimpBase): + """ + Single bone of a mesh. A bone has a name by which it can be found + in the frame hierarchy and by which it can be addressed by animations. + """ + + def __init__(self, bone): + """ + Converts an ASSIMP-bone to a PyAssimp-bone. + """ + #the name is easy + self.name = str(bone.mName) + + #matrix that transforms from mesh space to bone space in bind pose + self.matrix = Matrix(bone.mOffsetMatrix) + + #and of course the weights! + self._load_array(bone.mWeights, + bone.mNumWeights, + VertexWeight) + + class Scene(AssimpBase): """ The root structure of the imported data. @@ -175,6 +266,30 @@ class Mesh(AssimpBase): #faces self.faces = self._load_faces(mesh) + + #bones + self.bones = self._load_bones(mesh) + + + def _load_bones(self, mesh): + """ + Loads bones of this mesh. + + mesh - mesh-data + + result bones + """ + count = mesh.mNumBones + + if count==0: + #no bones + return [] + + #read bones + bones = mesh.mBones.contents + self._load_array(bones, + count, + Bone) def _load_faces(self, mesh): diff --git a/port/PyAssimp/pyassimp/structs.py b/port/PyAssimp/pyassimp/structs.py index 64d4f5902..b49bdc2ab 100644 --- a/port/PyAssimp/pyassimp/structs.py +++ b/port/PyAssimp/pyassimp/structs.py @@ -18,10 +18,10 @@ class STRING(Structure): _fields_ = [ #length of the string - ("length", c_uint), + ("length", c_uint), #OK #string data - ("data", c_char*MAXLEN) + ("data", c_char*MAXLEN) #OK ] @@ -33,10 +33,10 @@ class MATRIX4X4(Structure): _fields_ = [ #all the fields - ("a1", c_float), ("a2", c_float), ("a3", c_float), ("a4", c_float), - ("b1", c_float), ("b2", c_float), ("b3", c_float), ("b4", c_float), - ("c1", c_float), ("c2", c_float), ("c3", c_float), ("c4", c_float), - ("d1", c_float), ("d2", c_float), ("d3", c_float), ("d4", c_float), + ("a1", c_float), ("a2", c_float), ("a3", c_float), ("a4", c_float), #OK + ("b1", c_float), ("b2", c_float), ("b3", c_float), ("b4", c_float), #OK + ("c1", c_float), ("c2", c_float), ("c3", c_float), ("c4", c_float), #OK + ("d1", c_float), ("d2", c_float), ("d3", c_float), ("d4", c_float), #OK ] @@ -126,11 +126,11 @@ class VERTEXWEIGHT(Structure): _fields_ = [ #Index of the vertex which is influenced by the bone. - ("mVertexId", c_uint), + ("mVertexId", c_uint), #OK #The strength of the influence in the range (0...1). #The influence from all bones at one vertex amounts to 1. - ("mWeight", c_float) + ("mWeight", c_float) #OK ] @@ -143,16 +143,16 @@ class BONE(Structure): _fields_ = [ #The name of the bone. - ("mName", STRING), + ("mName", STRING), #OK #The number of vertices affected by this bone - ("mNumWeights", c_uint), + ("mNumWeights", c_uint), #OK #The vertices affected by this bone - ("mWeights", POINTER(VERTEXWEIGHT)), + ("mWeights", POINTER(VERTEXWEIGHT)), #OK #Matrix that transforms from mesh space to bone space in bind pose - ("mOffsetMatrix", MATRIX4X4) + ("mOffsetMatrix", MATRIX4X4) #OK ] @@ -243,12 +243,12 @@ class MESH(Structure): #The number of bones this mesh contains. #Can be 0, in which case the mBones array is NULL. - ("mNumBones", c_uint), + ("mNumBones", c_uint), #OK #The bones of this mesh. #A bone consists of a name by which it can be found in the #frame hierarchy and a set of vertex weights. - ("mBones", POINTER(POINTER(BONE))), + ("mBones", POINTER(POINTER(BONE))), #OK #The material used by this mesh. #A mesh does use only a single material. If an imported model uses