/usr/lib/python2.7/dist-packages/pyassimp/pyassimp.py is in python-pyassimp 3.0~dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | #-*- coding: UTF-8 -*-
"""
PyAssimp
This is the main-module of PyAssimp.
"""
import sys
if sys.version_info < (2,5):
raise 'pyassimp: need python 2.5 or newer'
import structs
import ctypes
import os
import helper
from errors import AssimpError
class aiArray:
"""
A python class to 'safely' access C arrays.
For m<Name> and mNum<Name> assimp class members.
"""
def __init__(self, instance, dataName, sizeName, i=None):
self.instance = instance
self.dataName = dataName
self.sizeName = sizeName
self.i = i
self.count = 0
def _GetSize(self):
return getattr(self.instance, self.sizeName)
def _GetData(self, index):
if self.i != None:
if not bool(getattr(self.instance, self.dataName)[self.i]):
return None
item = getattr(self.instance, self.dataName)[self.i][index]
else:
item = getattr(self.instance, self.dataName)[index]
if hasattr(item, 'contents'):
return item.contents._init()
elif hasattr(item, '_init'):
return item._init()
else:
return item
def next(self):
if self.count >= self._GetSize():
self.count = 0
raise StopIteration
else:
c = self.count
self.count += 1
return self._GetData(c)
def __getitem__(self, index):
if isinstance(index, slice):
indices = index.indices(len(self))
return [self.__getitem__(i) for i in range(*indices)]
if index < 0 or index >= self._GetSize():
raise IndexError("aiArray index out of range")
return self._GetData(index)
def __iter__(self):
return self
def __len__(self):
return int(self._GetSize())
def __str__(self):
return str([x for x in self])
def __repr__(self):
return str([x for x in self])
class aiTuple:
"""
A python class to 'safely' access C structs in a python tuple fashion.
For C structs like vectors, matrices, colors, ...
"""
def __init__(self, instance):
self.instance = instance
self.count = 0
def _GetSize(self):
return len(self.instance._fields_)
def _GetData(self, index):
return getattr(self.instance, self.instance._fields_[index][0])
def next(self):
if self.count >= self._GetSize():
self.count = 0
raise StopIteration
else:
c = self.count
self.count += 1
return self._GetData(c)
def __getitem__(self, index):
if isinstance(index, slice):
indices = index.indices(len(self))
return [self.__getitem__(i) for i in range(*indices)]
if index < 0 or index >= self._GetSize():
raise IndexError("aiTuple index out of range")
return self._GetData(index)
def __iter__(self):
return self
def __len__(self):
return int(self._GetSize())
def __str__(self):
return str([x for x in self])
def __repr__(self):
return str([x for x in self])
def _init(self):
"""
Custom initialize() for C structs, adds safely accessable member functionality.
"""
if hasattr(self, '_is_init'):
return self
self._is_init = True
if str(self.__class__.__name__) == "MaterialProperty":
self.mKey._init()
for m in self.__class__.__dict__.keys():
if m.startswith('mNum'):
name = m.split('mNum')[1]
if 'm'+name in self.__class__.__dict__.keys():
setattr(self.__class__, name.lower(), aiArray(self, 'm'+name , m))
if name.lower() == "vertices":
setattr(self.__class__, "normals", aiArray(self, 'mNormals' , m))
setattr(self.__class__, "tangents", aiArray(self, 'mTangents' , m))
setattr(self.__class__, "bitangets", aiArray(self, 'mBitangents' , m))
setattr(self.__class__, "colors", [aiArray(self, 'mColors' , m, o) for o in xrange(len(self.mColors))])
setattr(self.__class__, "texcoords", [aiArray(self, 'mTextureCoords' , m, o) for o in xrange(len(self.mColors))])
elif m == "x" or m == "a1" or m == "b": # Vector, matrix, quat, color
self._tuple = aiTuple(self)
setattr(self.__class__, '__getitem__', lambda x, y: x._tuple.__getitem__(y))
setattr(self.__class__, '__iter__', lambda x: x._tuple)
setattr(self.__class__, 'next', lambda x: x._tuple.next)
setattr(self.__class__, '__repr__', lambda x: str([c for c in x]))
break
elif m == "data": #String
setattr(self.__class__, '__repr__', lambda x: str(x.data))
setattr(self.__class__, '__str__', lambda x: str(x.data))
break
if hasattr(getattr(self, m), '_init'):
getattr(self, m)._init()
return self
"""
Python magic to add the _init() function to all C struct classes.
"""
for struct in dir(structs):
if not (struct.startswith('_') or struct.startswith('c_') or struct == "Structure" or struct == "POINTER") and not isinstance(getattr(structs, struct),int):
setattr(getattr(structs, struct), '_init', _init)
class AssimpLib(object):
"""
Assimp-Singleton
"""
load, release, dll = helper.search_library()
#the loader as singleton
_assimp_lib = AssimpLib()
def load(filename, processing=0):
"""
Loads the model with some specific processing parameters.
filename - file to load model from
processing - processing parameters
result Scene-object with model-data
throws AssimpError - could not open file
"""
#read pure data
model = _assimp_lib.load(filename, processing)
if not model:
#Uhhh, something went wrong!
raise AssimpError, ("could not import file: %s" % filename)
return model.contents._init()
def release(scene):
from ctypes import pointer
_assimp_lib.release(pointer(scene))
def aiGetMaterialFloatArray(material, key):
AI_SUCCESS = 0
from ctypes import byref, pointer, cast, c_float, POINTER, sizeof, c_uint
out = structs.Color4D()
max = c_uint(sizeof(structs.Color4D))
r=_assimp_lib.dll.aiGetMaterialFloatArray(pointer(material),
key[0],
key[1],
key[2],
byref(out),
byref(max))
if (r != AI_SUCCESS):
raise AssimpError("aiGetMaterialFloatArray failed!")
out._init()
return [out[i] for i in xrange(max.value)]
def aiGetMaterialString(material, key):
AI_SUCCESS = 0
from ctypes import byref, pointer, cast, c_float, POINTER, sizeof, c_uint
out = structs.String()
r=_assimp_lib.dll.aiGetMaterialString(pointer(material),
key[0],
key[1],
key[2],
byref(out))
if (r != AI_SUCCESS):
raise AssimpError("aiGetMaterialString failed!")
return str(out.data)
def GetMaterialProperties(material):
"""
Convenience Function to get the material properties as a dict
and values in a python format.
"""
result = {}
#read all properties
for p in material.properties:
#the name
key = p.mKey.data
#the data
from ctypes import POINTER, cast, c_int, c_float, sizeof
if p.mType == 1:
arr = cast(p.mData, POINTER(c_float*(p.mDataLength/sizeof(c_float)) )).contents
value = [x for x in arr]
elif p.mType == 3: #string can't be an array
value = cast(p.mData, POINTER(structs.String)).contents.data
elif p.mType == 4:
arr = cast(p.mData, POINTER(c_int*(p.mDataLength/sizeof(c_int)) )).contents
value = [x for x in arr]
else:
value = p.mData[:p.mDataLength]
result[key] = value
return result
def aiDecomposeMatrix(matrix):
if not isinstance(matrix, structs.Matrix4x4):
raise AssimpError("aiDecomposeMatrix failed: Not a aiMatrix4x4!")
scaling = structs.Vector3D()
rotation = structs.Quaternion()
position = structs.Vector3D()
from ctypes import byref, pointer
_assimp_lib.dll.aiDecomposeMatrix(pointer(matrix), byref(scaling), byref(rotation), byref(position))
return scaling._init(), rotation._init(), position._init()
|