sample.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. #-*- coding: UTF-8 -*-
  3. """
  4. This module demonstrates the functionality of PyAssimp.
  5. """
  6. import pyassimp.core as pyassimp
  7. import os, sys
  8. #get a model out of assimp's test-data if none is provided on the command line
  9. DEFAULT_MODEL = os.path.join(os.path.dirname(__file__),
  10. "..", "..",
  11. "test", "models", "MDL", "MDL3 (3DGS A4)", "minigun.MDL")
  12. def recur_node(node,level = 0):
  13. print(" " + "\t" * level + "- " + str(node))
  14. for child in node.children:
  15. recur_node(child, level + 1)
  16. def main(filename=None):
  17. filename = filename or DEFAULT_MODEL
  18. print "Reading model", filename
  19. scene = pyassimp.load(filename)
  20. #the model we load
  21. print "MODEL:", filename
  22. print
  23. #write some statistics
  24. print "SCENE:"
  25. print " meshes:", len(scene.meshes)
  26. print " materials:", len(scene.materials)
  27. print " textures:", len(scene.textures)
  28. print
  29. print "NODES:"
  30. recur_node(scene.rootnode)
  31. print
  32. print "MESHES:"
  33. for index, mesh in enumerate(scene.meshes):
  34. print " MESH", index+1
  35. print " material id:", mesh.materialindex+1
  36. print " vertices:", len(mesh.vertices)
  37. print " first 3 verts:", mesh.vertices[:3]
  38. if len(mesh.normals) > 0:
  39. print " first 3 normals:", mesh.normals[:3]
  40. else:
  41. print " no normals"
  42. print " colors:", len(mesh.colors)
  43. tc = mesh.texturecoords
  44. if len(tc) >= 4:
  45. print " texture-coords 1:", len(tc[0]), "first3:", tc[0][:3]
  46. print " texture-coords 2:", len(tc[1]), "first3:", tc[1][:3]
  47. print " texture-coords 3:", len(tc[2]), "first3:", tc[2][:3]
  48. print " texture-coords 4:", len(tc[3]), "first3:", tc[3][:3]
  49. elif len(tc) == 0:
  50. print " no texture coordinates"
  51. else:
  52. print " tc is an unexpected number of elements (expect 4, got", len(tc), ")"
  53. print " uv-component-count:", len(mesh.numuvcomponents)
  54. print " faces:", len(mesh.faces), "first:", [f for f in mesh.faces[:3]]
  55. print " bones:", len(mesh.bones), "first:", [str(b) for b in mesh.bones[:3]]
  56. print
  57. print "MATERIALS:"
  58. for index, material in enumerate(scene.materials):
  59. print(" MATERIAL (id:" + str(index+1) + ")")
  60. for key, value in material.properties.items():
  61. print " %s: %s" % (key, value)
  62. print
  63. print "TEXTURES:"
  64. for index, texture in enumerate(scene.textures):
  65. print " TEXTURE", index+1
  66. print " width:", texture.width
  67. print " height:", texture.height
  68. print " hint:", texture.achformathint
  69. print " data (size):", len(texture.data)
  70. # Finally release the model
  71. pyassimp.release(scene)
  72. print "Finished parsing the model."
  73. if __name__ == "__main__":
  74. main(sys.argv[1] if len(sys.argv)>1 else None)