textures
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@162 67173fc5-114c-0410-ac8e-9d2fd5bffc1fpull/1/head
parent
64c431b5eb
commit
a4f2aab6c3
|
@ -27,7 +27,8 @@ class AssimpBase(object):
|
||||||
Base class for all Assimp-classes.
|
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
|
Loads a whole array out of data, and constructs a new object. If data
|
||||||
is NULL, an empty list will be returned.
|
is NULL, an empty list will be returned.
|
||||||
|
@ -42,6 +43,19 @@ class AssimpBase(object):
|
||||||
return [cons(data[i]) for i in range(count)]
|
return [cons(data[i]) for i in range(count)]
|
||||||
else:
|
else:
|
||||||
return []
|
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):
|
class Material(object):
|
||||||
|
@ -179,11 +193,53 @@ class Bone(AssimpBase):
|
||||||
self.matrix = Matrix(bone.mOffsetMatrix)
|
self.matrix = Matrix(bone.mOffsetMatrix)
|
||||||
|
|
||||||
#and of course the weights!
|
#and of course the weights!
|
||||||
self._load_array(bone.mWeights,
|
Bone._load_array(bone.mWeights,
|
||||||
bone.mNumWeights,
|
bone.mNumWeights,
|
||||||
VertexWeight)
|
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):
|
class Scene(AssimpBase):
|
||||||
"""
|
"""
|
||||||
The root structure of the imported data.
|
The root structure of the imported data.
|
||||||
|
@ -214,14 +270,19 @@ class Scene(AssimpBase):
|
||||||
self.flags = model.flags
|
self.flags = model.flags
|
||||||
|
|
||||||
#load mesh-data
|
#load mesh-data
|
||||||
self.meshes = self._load_array(model.mMeshes,
|
self.meshes = Scene._load_array(model.mMeshes,
|
||||||
model.mNumMeshes,
|
model.mNumMeshes,
|
||||||
lambda x: Mesh(x.contents))
|
Scene.make_loader(Mesh))
|
||||||
|
|
||||||
#load materials
|
#load materials
|
||||||
self.materials = self._load_array(model.mMaterials,
|
self.materials = Scene._load_array(model.mMaterials,
|
||||||
model.mNumMaterials,
|
model.mNumMaterials,
|
||||||
lambda x: Material(x.contents))
|
Scene.make_loader(Material))
|
||||||
|
|
||||||
|
#load textures
|
||||||
|
self.textures = Scene._load_array(model.mTextures,
|
||||||
|
model.mNumTextures,
|
||||||
|
Scene.make_loader(Texture))
|
||||||
|
|
||||||
|
|
||||||
def list_flags(self):
|
def list_flags(self):
|
||||||
|
@ -283,22 +344,22 @@ class Mesh(AssimpBase):
|
||||||
mesh - raw mesh-data
|
mesh - raw mesh-data
|
||||||
"""
|
"""
|
||||||
#load vertices
|
#load vertices
|
||||||
self.vertices = self._load_array(mesh.mVertices,
|
self.vertices = Mesh._load_array(mesh.mVertices,
|
||||||
mesh.mNumVertices,
|
mesh.mNumVertices,
|
||||||
helper.vec2tuple)
|
helper.vec2tuple)
|
||||||
|
|
||||||
#load normals
|
#load normals
|
||||||
self.normals = self._load_array(mesh.mNormals,
|
self.normals = Mesh._load_array(mesh.mNormals,
|
||||||
mesh.mNumVertices,
|
mesh.mNumVertices,
|
||||||
helper.vec2tuple)
|
helper.vec2tuple)
|
||||||
|
|
||||||
#load tangents
|
#load tangents
|
||||||
self.tangents = self._load_array(mesh.mTangents,
|
self.tangents = Mesh._load_array(mesh.mTangents,
|
||||||
mesh.mNumVertices,
|
mesh.mNumVertices,
|
||||||
helper.vec2tuple)
|
helper.vec2tuple)
|
||||||
|
|
||||||
#load bitangents
|
#load bitangents
|
||||||
self.bitangents = self._load_array(mesh.mBitangents,
|
self.bitangents = Mesh._load_array(mesh.mBitangents,
|
||||||
mesh.mNumVertices,
|
mesh.mNumVertices,
|
||||||
helper.vec2tuple)
|
helper.vec2tuple)
|
||||||
|
|
||||||
|
@ -337,9 +398,9 @@ class Mesh(AssimpBase):
|
||||||
|
|
||||||
#read bones
|
#read bones
|
||||||
bones = mesh.mBones.contents
|
bones = mesh.mBones.contents
|
||||||
self._load_array(bones,
|
return Mesh._load_array(bones,
|
||||||
count,
|
count,
|
||||||
Bone)
|
Bone)
|
||||||
|
|
||||||
|
|
||||||
def _load_faces(self, mesh):
|
def _load_faces(self, mesh):
|
||||||
|
@ -376,7 +437,7 @@ class Mesh(AssimpBase):
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
for i in range(structs.MESH.AI_MAX_NUMBER_OF_TEXTURECOORDS):
|
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,
|
mesh.mNumVertices,
|
||||||
helper.vec2tuple))
|
helper.vec2tuple))
|
||||||
|
|
||||||
|
|
|
@ -445,12 +445,12 @@ class TEXTURE(Structure):
|
||||||
#If mHeight is zero the texture is compressed in a format
|
#If mHeight is zero the texture is compressed in a format
|
||||||
#like JPEG. In this case mWidth specifies the size of the
|
#like JPEG. In this case mWidth specifies the size of the
|
||||||
#memory area pcData is pointing to, in bytes.
|
#memory area pcData is pointing to, in bytes.
|
||||||
("mWidth", c_uint),
|
("mWidth", c_uint), #OK
|
||||||
|
|
||||||
#Height of the texture, in pixels
|
#Height of the texture, in pixels
|
||||||
#If this value is zero, pcData points to an compressed texture
|
#If this value is zero, pcData points to an compressed texture
|
||||||
#in an unknown format (e.g. JPEG).
|
#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
|
#A hint from the loader to make it easier for applications
|
||||||
#to determine the type of embedded compressed textures.
|
#to determine the type of embedded compressed textures.
|
||||||
|
@ -459,7 +459,7 @@ class TEXTURE(Structure):
|
||||||
#information about the texture file format used OR the
|
#information about the texture file format used OR the
|
||||||
#file extension of the format without a leading dot.
|
#file extension of the format without a leading dot.
|
||||||
#E.g. 'dds\0', 'pcx\0'. All characters are lower-case.
|
#E.g. 'dds\0', 'pcx\0'. All characters are lower-case.
|
||||||
("achFormatHint", c_char*4),
|
("achFormatHint", c_char*4), #OK
|
||||||
|
|
||||||
#Data of the texture.
|
#Data of the texture.
|
||||||
#Points to an array of mWidth * mHeight aiTexel's.
|
#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
|
#as possible. If mHeight = 0 this is a pointer to a memory
|
||||||
#buffer of size mWidth containing the compressed texture
|
#buffer of size mWidth containing the compressed texture
|
||||||
#data. Good luck, have fun!
|
#data. Good luck, have fun!
|
||||||
("pcData", POINTER(TEXEL))
|
("pcData", POINTER(TEXEL)) #OK
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -513,11 +513,11 @@ class SCENE(Structure):
|
||||||
("mAnimations", POINTER(POINTER(ANIMATION))),
|
("mAnimations", POINTER(POINTER(ANIMATION))),
|
||||||
|
|
||||||
#The number of textures embedded into the file
|
#The number of textures embedded into the file
|
||||||
("mNumTextures", c_uint),
|
("mNumTextures", c_uint), #OK
|
||||||
|
|
||||||
#The array of embedded textures.
|
#The array of embedded textures.
|
||||||
#Not many file formats embedd their textures into the file.
|
#Not many file formats embedd their textures into the file.
|
||||||
#An example is Quake's MDL format (which is also used by
|
#An example is Quake's MDL format (which is also used by
|
||||||
#some GameStudio(TM) versions)
|
#some GameStudio(TM) versions)
|
||||||
("mTextures", POINTER(POINTER(TEXTURE)))
|
("mTextures", POINTER(POINTER(TEXTURE))) #OK
|
||||||
]
|
]
|
|
@ -11,7 +11,7 @@ import os
|
||||||
#get a model out of assimp's test-data
|
#get a model out of assimp's test-data
|
||||||
MODEL = os.path.join(os.path.dirname(__file__),
|
MODEL = os.path.join(os.path.dirname(__file__),
|
||||||
"..", "..",
|
"..", "..",
|
||||||
"test", "3DSFiles", "test1.3ds")
|
"test", "MDLFiles", "MDL3 (3DGS A4)", "minigun.MDL")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
scene = pyassimp.load(MODEL)
|
scene = pyassimp.load(MODEL)
|
||||||
|
@ -25,12 +25,7 @@ def main():
|
||||||
print " flags:", ", ".join(scene.list_flags())
|
print " flags:", ", ".join(scene.list_flags())
|
||||||
print " meshes:", len(scene.meshes)
|
print " meshes:", len(scene.meshes)
|
||||||
print " materials:", len(scene.materials)
|
print " materials:", len(scene.materials)
|
||||||
print
|
print " textures:", len(scene.textures)
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
print "MESHES:"
|
print "MESHES:"
|
||||||
|
@ -49,6 +44,21 @@ def main():
|
||||||
print " bones:", len(mesh.bones), "first:", mesh.bones[:3]
|
print " bones:", len(mesh.bones), "first:", mesh.bones[:3]
|
||||||
print
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue