export_mesh.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import Blender
  2. import string
  3. import os.path
  4. ################
  5. # MATRIX FUNCS #
  6. ################
  7. def MatrixMul( matrix, vector ):
  8. vec = [0, 0, 0]
  9. for i in range(0, 3):
  10. for j in range(0, 3):
  11. vec[i] = vec[i] + matrix[j][i] * vector[j]
  12. vec[i] = vec[i] + matrix[3][i]
  13. for i in range(0, 3):
  14. vector[i] = vec[i]
  15. def PrintMatrix( matrix ):
  16. for i in range(0, 4):
  17. print "[",
  18. for j in range(0, 4):
  19. f = round( matrix[j][i], 5)
  20. if f >= 0 :
  21. print " %04f" % f,
  22. else:
  23. print "%04f" % f,
  24. print "]"
  25. ########
  26. # main #
  27. ########
  28. def main():
  29. print "\n---- Export Mesh ----"
  30. ########
  31. # vars #
  32. ########
  33. file = 0
  34. mesh = 0
  35. matrix = 0
  36. path = "c:\\src\\VisualC++\\gmdl_3d_format\\models\\satan\\"
  37. if not os.path.exists( path ): path = "c:\\data\\projects\\VisualC++\\gmdl_3d_format\\models\\satan\\"
  38. ######################
  39. # init and open file #
  40. ######################
  41. objs = Blender.Object.GetSelected()
  42. if len( objs ) < 1:
  43. print "-->ERROR: No object selected"
  44. return 0
  45. if objs[0].getType() != "Mesh":
  46. print "-->ERROR: The selected object must link to a mesh and not in a(n) " + objs[0].getType()
  47. return 0
  48. mesh = objs[0].getData( 0, 1 )
  49. if not mesh.faceUV:
  50. print "-->ERROR: The mesh doesnt have UVs"
  51. return 0
  52. matrix = objs[0].getMatrix( "localspace" )
  53. filename = path + "mesh.txt"
  54. file = open( filename, "w" )
  55. ###########
  56. # vgroups #
  57. ###########
  58. group_names = mesh.getVertGroupNames()
  59. file.write( "GROUPS_NUM " + str(len( group_names )) + "\n" )
  60. i = 0
  61. group_names.sort() # !!!!!!!!!!! everythins is put alphabeticaly
  62. for group_name in group_names:
  63. file.write( "GROUP %d VGROUP_NAME %s VERTS_NUM " % (i, group_name) )
  64. verts = mesh.getVertsFromGroup( group_name )
  65. file.write( str(len(verts)) + " VERTS " )
  66. for vert in verts:
  67. file.write( str(vert) + " " )
  68. file.write( "\n" )
  69. i = i+1
  70. #########
  71. # verts #
  72. #########
  73. file.write( "VERTS_NUM %d\n" % len(mesh.verts) )
  74. for vert in mesh.verts:
  75. coords = [0.0, 0.0, 0.0]
  76. normal = [0.0, 0.0, 0.0]
  77. coords[0] = vert.co.x
  78. coords[1] = vert.co.y
  79. coords[2] = vert.co.z
  80. normal[0] = vert.no.x
  81. normal[1] = vert.no.y
  82. normal[2] = vert.no.z
  83. MatrixMul( matrix, coords )
  84. MatrixMul( matrix, normal )
  85. file.write( "VERT %d COORDS %f %f %f\n" %(vert.index, coords[0], coords[1], coords[2]) )
  86. #file.write( " NORMAL %f %f %f\n" %(normal[0], normal[1], normal[2]) )
  87. #########
  88. # faces #
  89. #########
  90. #first count the faces as triangles
  91. tris_num = 0
  92. for face in mesh.faces:
  93. if len(face.v) == 3:
  94. tris_num = tris_num + 1
  95. else:
  96. tris_num = tris_num + 2
  97. file.write( "FACE_NUM %d\n" % tris_num )
  98. # for every face
  99. i = 0
  100. for face in mesh.faces:
  101. file.write( "FACE %d VERTS " % i )
  102. order = [0,1,2]
  103. # print index
  104. for j in order:
  105. file.write( "%d " % face.v[j].index )
  106. # WRITE UVs
  107. file.write( "UV " )
  108. for j in order:
  109. file.write( "%f %f " % (face.uv[j].x, face.uv[j].y) )
  110. file.write( "\n" )
  111. i = i+1
  112. # if the face is quad then triangulate
  113. if( len( face.v ) == 4 ):
  114. order = [0,2,3]
  115. file.write( "FACE %d VERTS " % i )
  116. for j in order:
  117. file.write( "%d " % face.v[j].index )
  118. file.write( "UV " )
  119. for j in order:
  120. file.write( "%f %f " % (face.uv[j].x, face.uv[j].y) )
  121. file.write( "\n" )
  122. i = i+1
  123. ########
  124. # DONE #
  125. ########
  126. print "Done! File \"%s\" created" % filename
  127. file.close()
  128. main()