core.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. """
  2. PyAssimp
  3. This is the main-module of PyAssimp.
  4. """
  5. import sys
  6. if sys.version_info < (2,6):
  7. raise 'pyassimp: need python 2.6 or newer'
  8. # xrange was renamed range in Python 3 and the original range from Python 2 was removed.
  9. # To keep compatibility with both Python 2 and 3, xrange is set to range for version 3.0 and up.
  10. if sys.version_info >= (3,0):
  11. xrange = range
  12. import ctypes
  13. import os
  14. try: import numpy
  15. except: numpy = None
  16. import logging
  17. logger = logging.getLogger("pyassimp")
  18. # attach default null handler to logger so it doesn't complain
  19. # even if you don't attach another handler to logger
  20. logger.addHandler(logging.NullHandler())
  21. from . import structs
  22. from . import helper
  23. from . import postprocess
  24. from .errors import AssimpError
  25. from .formats import available_formats
  26. class AssimpLib(object):
  27. """
  28. Assimp-Singleton
  29. """
  30. load, load_mem, export, release, dll = helper.search_library()
  31. _assimp_lib = AssimpLib()
  32. def make_tuple(ai_obj, type = None):
  33. res = None
  34. #notes:
  35. # ai_obj._fields_ = [ ("attr", c_type), ... ]
  36. # getattr(ai_obj, e[0]).__class__ == float
  37. if isinstance(ai_obj, structs.Matrix4x4):
  38. if numpy:
  39. res = numpy.array([getattr(ai_obj, e[0]) for e in ai_obj._fields_]).reshape((4,4))
  40. #import pdb;pdb.set_trace()
  41. else:
  42. res = [getattr(ai_obj, e[0]) for e in ai_obj._fields_]
  43. res = [res[i:i+4] for i in xrange(0,16,4)]
  44. elif isinstance(ai_obj, structs.Matrix3x3):
  45. if numpy:
  46. res = numpy.array([getattr(ai_obj, e[0]) for e in ai_obj._fields_]).reshape((3,3))
  47. else:
  48. res = [getattr(ai_obj, e[0]) for e in ai_obj._fields_]
  49. res = [res[i:i+3] for i in xrange(0,9,3)]
  50. else:
  51. if numpy:
  52. res = numpy.array([getattr(ai_obj, e[0]) for e in ai_obj._fields_])
  53. else:
  54. res = [getattr(ai_obj, e[0]) for e in ai_obj._fields_]
  55. return res
  56. # It is faster and more correct to have an init function for each assimp class
  57. def _init_face(aiFace):
  58. aiFace.indices = [aiFace.mIndices[i] for i in range(aiFace.mNumIndices)]
  59. assimp_struct_inits = { structs.Face : _init_face }
  60. def call_init(obj, caller = None):
  61. if helper.hasattr_silent(obj,'contents'): #pointer
  62. _init(obj.contents, obj, caller)
  63. else:
  64. _init(obj,parent=caller)
  65. def _is_init_type(obj):
  66. if helper.hasattr_silent(obj,'contents'): #pointer
  67. return _is_init_type(obj[0])
  68. # null-pointer case that arises when we reach a mesh attribute
  69. # like mBitangents which use mNumVertices rather than mNumBitangents
  70. # so it breaks the 'is iterable' check.
  71. # Basically:
  72. # FIXME!
  73. elif not bool(obj):
  74. return False
  75. tname = obj.__class__.__name__
  76. return not (tname[:2] == 'c_' or tname == 'Structure' \
  77. or tname == 'POINTER') and not isinstance(obj,int)
  78. def _init(self, target = None, parent = None):
  79. """
  80. Custom initialize() for C structs, adds safely accessible member functionality.
  81. :param target: set the object which receive the added methods. Useful when manipulating
  82. pointers, to skip the intermediate 'contents' deferencing.
  83. """
  84. if not target:
  85. target = self
  86. dirself = dir(self)
  87. for m in dirself:
  88. if m.startswith("_"):
  89. continue
  90. if m.startswith('mNum'):
  91. if 'm' + m[4:] in dirself:
  92. continue # will be processed later on
  93. else:
  94. name = m[1:].lower()
  95. obj = getattr(self, m)
  96. setattr(target, name, obj)
  97. continue
  98. if m == 'mName':
  99. obj = self.mName
  100. target.name = str(obj.data.decode("utf-8"))
  101. target.__class__.__repr__ = lambda x: str(x.__class__) + "(" + x.name + ")"
  102. target.__class__.__str__ = lambda x: x.name
  103. continue
  104. name = m[1:].lower()
  105. obj = getattr(self, m)
  106. # Create tuples
  107. if isinstance(obj, structs.assimp_structs_as_tuple):
  108. setattr(target, name, make_tuple(obj))
  109. logger.debug(str(self) + ": Added array " + str(getattr(target, name)) + " as self." + name.lower())
  110. continue
  111. if m.startswith('m'):
  112. if name == "parent":
  113. setattr(target, name, parent)
  114. logger.debug("Added a parent as self." + name)
  115. continue
  116. if helper.hasattr_silent(self, 'mNum' + m[1:]):
  117. length = getattr(self, 'mNum' + m[1:])
  118. # -> special case: properties are
  119. # stored as a dict.
  120. if m == 'mProperties':
  121. setattr(target, name, _get_properties(obj, length))
  122. continue
  123. if not length: # empty!
  124. setattr(target, name, [])
  125. logger.debug(str(self) + ": " + name + " is an empty list.")
  126. continue
  127. try:
  128. if obj._type_ in structs.assimp_structs_as_tuple:
  129. if numpy:
  130. setattr(target, name, numpy.array([make_tuple(obj[i]) for i in range(length)], dtype=numpy.float32))
  131. logger.debug(str(self) + ": Added an array of numpy arrays (type "+ str(type(obj)) + ") as self." + name)
  132. else:
  133. setattr(target, name, [make_tuple(obj[i]) for i in range(length)])
  134. logger.debug(str(self) + ": Added a list of lists (type "+ str(type(obj)) + ") as self." + name)
  135. else:
  136. setattr(target, name, [obj[i] for i in range(length)]) #TODO: maybe not necessary to recreate an array?
  137. logger.debug(str(self) + ": Added list of " + str(obj) + " " + name + " as self." + name + " (type: " + str(type(obj)) + ")")
  138. # initialize array elements
  139. try:
  140. init = assimp_struct_inits[type(obj[0])]
  141. except KeyError:
  142. if _is_init_type(obj[0]):
  143. for e in getattr(target, name):
  144. call_init(e, target)
  145. else:
  146. for e in getattr(target, name):
  147. init(e)
  148. except IndexError:
  149. logger.error("in " + str(self) +" : mismatch between mNum" + name + " and the actual amount of data in m" + name + ". This may be due to version mismatch between libassimp and pyassimp. Quitting now.")
  150. sys.exit(1)
  151. except ValueError as e:
  152. logger.error("In " + str(self) + "->" + name + ": " + str(e) + ". Quitting now.")
  153. if "setting an array element with a sequence" in str(e):
  154. logger.error("Note that pyassimp does not currently "
  155. "support meshes with mixed triangles "
  156. "and quads. Try to load your mesh with"
  157. " a post-processing to triangulate your"
  158. " faces.")
  159. raise e
  160. else: # starts with 'm' but not iterable
  161. setattr(target, name, obj)
  162. logger.debug("Added " + name + " as self." + name + " (type: " + str(type(obj)) + ")")
  163. if _is_init_type(obj):
  164. call_init(obj, target)
  165. if isinstance(self, structs.Mesh):
  166. _finalize_mesh(self, target)
  167. if isinstance(self, structs.Texture):
  168. _finalize_texture(self, target)
  169. return self
  170. def pythonize_assimp(type, obj, scene):
  171. """ This method modify the Assimp data structures
  172. to make them easier to work with in Python.
  173. Supported operations:
  174. - MESH: replace a list of mesh IDs by reference to these meshes
  175. - ADDTRANSFORMATION: add a reference to an object's transformation taken from their associated node.
  176. :param type: the type of modification to operate (cf above)
  177. :param obj: the input object to modify
  178. :param scene: a reference to the whole scene
  179. """
  180. if type == "MESH":
  181. meshes = []
  182. for i in obj:
  183. meshes.append(scene.meshes[i])
  184. return meshes
  185. if type == "ADDTRANSFORMATION":
  186. def getnode(node, name):
  187. if node.name == name: return node
  188. for child in node.children:
  189. n = getnode(child, name)
  190. if n: return n
  191. node = getnode(scene.rootnode, obj.name)
  192. if not node:
  193. raise AssimpError("Object " + str(obj) + " has no associated node!")
  194. setattr(obj, "transformation", node.transformation)
  195. def recur_pythonize(node, scene):
  196. '''
  197. Recursively call pythonize_assimp on
  198. nodes tree to apply several post-processing to
  199. pythonize the assimp datastructures.
  200. '''
  201. node.meshes = pythonize_assimp("MESH", node.meshes, scene)
  202. for mesh in node.meshes:
  203. mesh.material = scene.materials[mesh.materialindex]
  204. for cam in scene.cameras:
  205. pythonize_assimp("ADDTRANSFORMATION", cam, scene)
  206. for c in node.children:
  207. recur_pythonize(c, scene)
  208. def load(filename,
  209. file_type = None,
  210. processing = postprocess.aiProcess_Triangulate):
  211. '''
  212. Load a model into a scene. On failure throws AssimpError.
  213. Arguments
  214. ---------
  215. filename: Either a filename or a file object to load model from.
  216. If a file object is passed, file_type MUST be specified
  217. Otherwise Assimp has no idea which importer to use.
  218. This is named 'filename' so as to not break legacy code.
  219. processing: assimp postprocessing parameters. Verbose keywords are imported
  220. from postprocessing, and the parameters can be combined bitwise to
  221. generate the final processing value. Note that the default value will
  222. triangulate quad faces. Example of generating other possible values:
  223. processing = (pyassimp.postprocess.aiProcess_Triangulate |
  224. pyassimp.postprocess.aiProcess_OptimizeMeshes)
  225. file_type: string of file extension, such as 'stl'
  226. Returns
  227. ---------
  228. Scene object with model data
  229. '''
  230. if hasattr(filename, 'read'):
  231. '''
  232. This is the case where a file object has been passed to load.
  233. It is calling the following function:
  234. const aiScene* aiImportFileFromMemory(const char* pBuffer,
  235. unsigned int pLength,
  236. unsigned int pFlags,
  237. const char* pHint)
  238. '''
  239. if file_type == None:
  240. raise AssimpError('File type must be specified when passing file objects!')
  241. data = filename.read()
  242. model = _assimp_lib.load_mem(data,
  243. len(data),
  244. processing,
  245. file_type)
  246. else:
  247. # a filename string has been passed
  248. model = _assimp_lib.load(filename.encode("ascii"), processing)
  249. if not model:
  250. raise AssimpError('Could not import file!')
  251. scene = _init(model.contents)
  252. recur_pythonize(scene.rootnode, scene)
  253. return scene
  254. def export(scene,
  255. filename,
  256. file_type = None,
  257. processing = postprocess.aiProcess_Triangulate):
  258. '''
  259. Export a scene. On failure throws AssimpError.
  260. Arguments
  261. ---------
  262. scene: scene to export.
  263. filename: Filename that the scene should be exported to.
  264. file_type: string of file exporter to use. For example "collada".
  265. processing: assimp postprocessing parameters. Verbose keywords are imported
  266. from postprocessing, and the parameters can be combined bitwise to
  267. generate the final processing value. Note that the default value will
  268. triangulate quad faces. Example of generating other possible values:
  269. processing = (pyassimp.postprocess.aiProcess_Triangulate |
  270. pyassimp.postprocess.aiProcess_OptimizeMeshes)
  271. '''
  272. from ctypes import pointer
  273. exportStatus = _assimp_lib.export(pointer(scene), file_type.encode("ascii"), filename.encode("ascii"), processing)
  274. if exportStatus != 0:
  275. raise AssimpError('Could not export scene!')
  276. def release(scene):
  277. from ctypes import pointer
  278. _assimp_lib.release(pointer(scene))
  279. def _finalize_texture(tex, target):
  280. setattr(target, "achformathint", tex.achFormatHint)
  281. if numpy:
  282. data = numpy.array([make_tuple(getattr(tex, "pcData")[i]) for i in range(tex.mWidth * tex.mHeight)])
  283. else:
  284. data = [make_tuple(getattr(tex, "pcData")[i]) for i in range(tex.mWidth * tex.mHeight)]
  285. setattr(target, "data", data)
  286. def _finalize_mesh(mesh, target):
  287. """ Building of meshes is a bit specific.
  288. We override here the various datasets that can
  289. not be process as regular fields.
  290. For instance, the length of the normals array is
  291. mNumVertices (no mNumNormals is available)
  292. """
  293. nb_vertices = getattr(mesh, "mNumVertices")
  294. def fill(name):
  295. mAttr = getattr(mesh, name)
  296. if numpy:
  297. if mAttr:
  298. data = numpy.array([make_tuple(getattr(mesh, name)[i]) for i in range(nb_vertices)], dtype=numpy.float32)
  299. setattr(target, name[1:].lower(), data)
  300. else:
  301. setattr(target, name[1:].lower(), numpy.array([], dtype="float32"))
  302. else:
  303. if mAttr:
  304. data = [make_tuple(getattr(mesh, name)[i]) for i in range(nb_vertices)]
  305. setattr(target, name[1:].lower(), data)
  306. else:
  307. setattr(target, name[1:].lower(), [])
  308. def fillarray(name):
  309. mAttr = getattr(mesh, name)
  310. data = []
  311. for index, mSubAttr in enumerate(mAttr):
  312. if mSubAttr:
  313. data.append([make_tuple(getattr(mesh, name)[index][i]) for i in range(nb_vertices)])
  314. if numpy:
  315. setattr(target, name[1:].lower(), numpy.array(data, dtype=numpy.float32))
  316. else:
  317. setattr(target, name[1:].lower(), data)
  318. fill("mNormals")
  319. fill("mTangents")
  320. fill("mBitangents")
  321. fillarray("mColors")
  322. fillarray("mTextureCoords")
  323. # prepare faces
  324. if numpy:
  325. faces = numpy.array([f.indices for f in target.faces], dtype=numpy.int32)
  326. else:
  327. faces = [f.indices for f in target.faces]
  328. setattr(target, 'faces', faces)
  329. class PropertyGetter(dict):
  330. def __getitem__(self, key):
  331. semantic = 0
  332. if isinstance(key, tuple):
  333. key, semantic = key
  334. return dict.__getitem__(self, (key, semantic))
  335. def keys(self):
  336. for k in dict.keys(self):
  337. yield k[0]
  338. def __iter__(self):
  339. return self.keys()
  340. def items(self):
  341. for k, v in dict.items(self):
  342. yield k[0], v
  343. def _get_properties(properties, length):
  344. """
  345. Convenience Function to get the material properties as a dict
  346. and values in a python format.
  347. """
  348. result = {}
  349. #read all properties
  350. for p in [properties[i] for i in range(length)]:
  351. #the name
  352. p = p.contents
  353. key = (str(p.mKey.data.decode("utf-8")).split('.')[1], p.mSemantic)
  354. #the data
  355. from ctypes import POINTER, cast, c_int, c_float, sizeof
  356. if p.mType == 1:
  357. arr = cast(p.mData, POINTER(c_float * int(p.mDataLength/sizeof(c_float)) )).contents
  358. value = [x for x in arr]
  359. elif p.mType == 3: #string can't be an array
  360. value = cast(p.mData, POINTER(structs.MaterialPropertyString)).contents.data.decode("utf-8")
  361. elif p.mType == 4:
  362. arr = cast(p.mData, POINTER(c_int * int(p.mDataLength/sizeof(c_int)) )).contents
  363. value = [x for x in arr]
  364. else:
  365. value = p.mData[:p.mDataLength]
  366. if len(value) == 1:
  367. [value] = value
  368. result[key] = value
  369. return PropertyGetter(result)
  370. def decompose_matrix(matrix):
  371. if not isinstance(matrix, structs.Matrix4x4):
  372. raise AssimpError("pyassimp.decompose_matrix failed: Not a Matrix4x4!")
  373. scaling = structs.Vector3D()
  374. rotation = structs.Quaternion()
  375. position = structs.Vector3D()
  376. from ctypes import byref, pointer
  377. _assimp_lib.dll.aiDecomposeMatrix(pointer(matrix), byref(scaling), byref(rotation), byref(position))
  378. return scaling._init(), rotation._init(), position._init()