core.py 21 KB

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