From a4f2aab6c3d779895c8180dcf20328b92a610480 Mon Sep 17 00:00:00 2001 From: sebastianhempel Date: Tue, 23 Sep 2008 13:11:30 +0000 Subject: [PATCH] textures git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@162 67173fc5-114c-0410-ac8e-9d2fd5bffc1f --- port/PyAssimp/pyassimp/pyassimp.py | 93 +++++++++++++++++++++++++----- port/PyAssimp/pyassimp/structs.py | 12 ++-- port/PyAssimp/sample.py | 24 +++++--- 3 files changed, 100 insertions(+), 29 deletions(-) diff --git a/port/PyAssimp/pyassimp/pyassimp.py b/port/PyAssimp/pyassimp/pyassimp.py index e255083d4..6389bc876 100644 --- a/port/PyAssimp/pyassimp/pyassimp.py +++ b/port/PyAssimp/pyassimp/pyassimp.py @@ -27,7 +27,8 @@ class AssimpBase(object): Base class for all Assimp-classes. """ - def _load_array(self, data, count, cons): + @staticmethod + def _load_array(data, count, cons): """ Loads a whole array out of data, and constructs a new object. If data is NULL, an empty list will be returned. @@ -42,6 +43,19 @@ class AssimpBase(object): return [cons(data[i]) for i in range(count)] else: return [] + + + @staticmethod + def make_loader(function): + """ + Creates a loader function for "_load_array". + + function - function to be applied to the content of an element + """ + def loader(x): + return function(x.contents) + + return loader class Material(object): @@ -179,11 +193,53 @@ class Bone(AssimpBase): self.matrix = Matrix(bone.mOffsetMatrix) #and of course the weights! - self._load_array(bone.mWeights, + Bone._load_array(bone.mWeights, bone.mNumWeights, VertexWeight) +class Texture(AssimpBase): + """ + Texture included in the model. + """ + + def __init__(self, texture): + """ + Convertes the raw data to a texture. + + texture - raw data + """ + #dimensions + self.width = texture.mWidth + self.height = texture.mHeight + + #format hint + self.hint = texture.achFormatHint + + #load data + self.data = self._load_data(texture) + + + def _load_data(self, texture): + """ + Loads the texture data. + + texture - the texture + + result texture data in (red, green, blue, alpha) + """ + if self.height == 0: + #compressed data + size = self.width + else: + size = self.width * self.height + + #load! + return Texture._load_array(texture.pcData, + size, + lambda x: (x.r, x.g, x.b, x.a)) + + class Scene(AssimpBase): """ The root structure of the imported data. @@ -214,14 +270,19 @@ class Scene(AssimpBase): self.flags = model.flags #load mesh-data - self.meshes = self._load_array(model.mMeshes, - model.mNumMeshes, - lambda x: Mesh(x.contents)) + self.meshes = Scene._load_array(model.mMeshes, + model.mNumMeshes, + Scene.make_loader(Mesh)) #load materials - self.materials = self._load_array(model.mMaterials, - model.mNumMaterials, - lambda x: Material(x.contents)) + self.materials = Scene._load_array(model.mMaterials, + model.mNumMaterials, + Scene.make_loader(Material)) + + #load textures + self.textures = Scene._load_array(model.mTextures, + model.mNumTextures, + Scene.make_loader(Texture)) def list_flags(self): @@ -283,22 +344,22 @@ class Mesh(AssimpBase): mesh - raw mesh-data """ #load vertices - self.vertices = self._load_array(mesh.mVertices, + self.vertices = Mesh._load_array(mesh.mVertices, mesh.mNumVertices, helper.vec2tuple) #load normals - self.normals = self._load_array(mesh.mNormals, + self.normals = Mesh._load_array(mesh.mNormals, mesh.mNumVertices, helper.vec2tuple) #load tangents - self.tangents = self._load_array(mesh.mTangents, + self.tangents = Mesh._load_array(mesh.mTangents, mesh.mNumVertices, helper.vec2tuple) #load bitangents - self.bitangents = self._load_array(mesh.mBitangents, + self.bitangents = Mesh._load_array(mesh.mBitangents, mesh.mNumVertices, helper.vec2tuple) @@ -337,9 +398,9 @@ class Mesh(AssimpBase): #read bones bones = mesh.mBones.contents - self._load_array(bones, - count, - Bone) + return Mesh._load_array(bones, + count, + Bone) def _load_faces(self, mesh): @@ -376,7 +437,7 @@ class Mesh(AssimpBase): result = [] for i in range(structs.MESH.AI_MAX_NUMBER_OF_TEXTURECOORDS): - result.append(self._load_array(mesh.mTextureCoords[i], + result.append(Mesh._load_array(mesh.mTextureCoords[i], mesh.mNumVertices, helper.vec2tuple)) diff --git a/port/PyAssimp/pyassimp/structs.py b/port/PyAssimp/pyassimp/structs.py index d72ebda20..55722cf07 100644 --- a/port/PyAssimp/pyassimp/structs.py +++ b/port/PyAssimp/pyassimp/structs.py @@ -445,12 +445,12 @@ class TEXTURE(Structure): #If mHeight is zero the texture is compressed in a format #like JPEG. In this case mWidth specifies the size of the #memory area pcData is pointing to, in bytes. - ("mWidth", c_uint), + ("mWidth", c_uint), #OK #Height of the texture, in pixels #If this value is zero, pcData points to an compressed texture #in an unknown format (e.g. JPEG). - ("mHeight", c_uint), + ("mHeight", c_uint), #OK #A hint from the loader to make it easier for applications #to determine the type of embedded compressed textures. @@ -459,7 +459,7 @@ class TEXTURE(Structure): #information about the texture file format used OR the #file extension of the format without a leading dot. #E.g. 'dds\0', 'pcx\0'. All characters are lower-case. - ("achFormatHint", c_char*4), + ("achFormatHint", c_char*4), #OK #Data of the texture. #Points to an array of mWidth * mHeight aiTexel's. @@ -468,7 +468,7 @@ class TEXTURE(Structure): #as possible. If mHeight = 0 this is a pointer to a memory #buffer of size mWidth containing the compressed texture #data. Good luck, have fun! - ("pcData", POINTER(TEXEL)) + ("pcData", POINTER(TEXEL)) #OK ] @@ -513,11 +513,11 @@ class SCENE(Structure): ("mAnimations", POINTER(POINTER(ANIMATION))), #The number of textures embedded into the file - ("mNumTextures", c_uint), + ("mNumTextures", c_uint), #OK #The array of embedded textures. #Not many file formats embedd their textures into the file. #An example is Quake's MDL format (which is also used by #some GameStudio(TM) versions) - ("mTextures", POINTER(POINTER(TEXTURE))) + ("mTextures", POINTER(POINTER(TEXTURE))) #OK ] \ No newline at end of file diff --git a/port/PyAssimp/sample.py b/port/PyAssimp/sample.py index bf7a67972..0a366b56d 100644 --- a/port/PyAssimp/sample.py +++ b/port/PyAssimp/sample.py @@ -11,7 +11,7 @@ import os #get a model out of assimp's test-data MODEL = os.path.join(os.path.dirname(__file__), "..", "..", - "test", "3DSFiles", "test1.3ds") + "test", "MDLFiles", "MDL3 (3DGS A4)", "minigun.MDL") def main(): scene = pyassimp.load(MODEL) @@ -25,12 +25,7 @@ def main(): print " flags:", ", ".join(scene.list_flags()) print " meshes:", len(scene.meshes) print " materials:", len(scene.materials) - print - - for index, material in enumerate(scene.materials): - print "MATERIAL", index+1 - for key, value in material.properties.iteritems(): - print " %s: %s" % (key, value) + print " textures:", len(scene.textures) print print "MESHES:" @@ -49,6 +44,21 @@ def main(): print " bones:", len(mesh.bones), "first:", mesh.bones[:3] print + print "MATERIALS:" + for index, material in enumerate(scene.materials): + print " MATERIAL", index+1 + for key, value in material.properties.iteritems(): + print " %s: %s" % (key, value) + print + + print "TEXTURES:" + for index, texture in enumerate(scene.textures): + print " TEXTURE", index+1 + print " width:", texture.width + print " height:", texture.height + print " hint:", texture.hint + print " data (size):", len(texture.data) + if __name__ == "__main__": main() \ No newline at end of file