helper.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #-*- coding: UTF-8 -*-
  2. """
  3. Some fancy helper functions.
  4. """
  5. import os
  6. import ctypes
  7. import structs
  8. import operator
  9. from errors import AssimpError
  10. from ctypes import POINTER
  11. def vec2tuple(x):
  12. """
  13. Converts a VECTOR3D to a Tuple
  14. """
  15. return (x.x, x.y, x.z)
  16. def search_library():
  17. """
  18. Loads the assimp-Library.
  19. result (load-function, release-function)
  20. exception AssimpError if no library is found
  21. """
  22. #this path
  23. folder = os.path.dirname(__file__)
  24. ctypes.windll.kernel32.SetErrorMode(0x8007)
  25. candidates = []
  26. #test every file
  27. for filename in os.listdir(folder):
  28. library = os.path.join(folder, filename)
  29. try:
  30. dll = ctypes.cdll.LoadLibrary(library)
  31. except:
  32. #OK, this except is evil. But different OSs will throw different
  33. #errors. So just ignore errors.
  34. pass
  35. else:
  36. #get the functions
  37. try:
  38. load = dll.aiImportFile
  39. release = dll.aiReleaseImport
  40. except AttributeError:
  41. #OK, this is a library, but it has not the functions we need
  42. pass
  43. else:
  44. #Library found!
  45. load.restype = POINTER(structs.SCENE)
  46. candidates.append((library, load, release))
  47. if not candidates:
  48. #no library found
  49. raise AssimpError, "assimp library not found"
  50. else:
  51. #get the newest library
  52. candidates = map(lambda x: (os.lstat(x[0])[-2], x), candidates)
  53. return max(candidates, key=operator.itemgetter(0))[1][1:]