mesh.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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++\\3d_engine\\models\\imp\\"
  37. ######################
  38. # init and open file #
  39. ######################
  40. objs = Blender.Object.GetSelected()
  41. if len( objs ) < 1:
  42. print "-->ERROR: No object selected"
  43. return 0
  44. if objs[0].getType() != "Mesh":
  45. print "-->ERROR: The selected object must link to a mesh and not in a(n) " + objs[0].getType()
  46. return 0
  47. mesh = objs[0].getData( 0, 1 )
  48. if not mesh.faceUV:
  49. print "-->ERROR: The mesh doesnt have UVs"
  50. return 0
  51. matrix = objs[0].getMatrix( "localspace" )
  52. filename = path + "mesh.txt"
  53. file = open( filename, "w" )
  54. #########
  55. # verts #
  56. #########
  57. file.write( "VERTS_NUM %d\n" % len(mesh.verts) )
  58. for vert in mesh.verts:
  59. coords = [0.0, 0.0, 0.0]
  60. normal = [0.0, 0.0, 0.0]
  61. coords[0] = vert.co.x
  62. coords[1] = vert.co.y
  63. coords[2] = vert.co.z
  64. normal[0] = vert.no.x
  65. normal[1] = vert.no.y
  66. normal[2] = vert.no.z
  67. MatrixMul( matrix, coords )
  68. MatrixMul( matrix, normal )
  69. file.write( "\tVERT %d COORDS %f %f %f\n" %(vert.index, coords[0], coords[1], coords[2]) )
  70. #file.write( " NORMAL %f %f %f\n" %(normal[0], normal[1], normal[2]) )
  71. #########
  72. # faces #
  73. #########
  74. #first count the faces as triangles
  75. tris_num = 0
  76. for face in mesh.faces:
  77. if len(face.v) == 3:
  78. tris_num = tris_num + 1
  79. else:
  80. tris_num = tris_num + 2
  81. file.write( "FACE_NUM %d\n" % tris_num )
  82. # for every face
  83. i = 0
  84. for face in mesh.faces:
  85. file.write( "\tFACE %d VERT_IDS " % i )
  86. order = [0,1,2]
  87. # print index
  88. for j in order:
  89. file.write( "%d " % face.v[j].index )
  90. file.write( "\n" )
  91. i = i+1
  92. # if the face is quad then triangulate
  93. if( len( face.v ) == 4 ):
  94. order = [0,2,3]
  95. file.write( "\tFACE %d VERT_IDS " % i )
  96. for j in order:
  97. file.write( "%d " % face.v[j].index )
  98. file.write( "\n" )
  99. i = i+1
  100. #######
  101. # uvs #
  102. #######
  103. file.write( "UVS_NUM %d\n" % tris_num )
  104. i = 0
  105. for face in mesh.faces:
  106. order = [0,1,2]
  107. file.write( "\tUV %d " % i )
  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( "\tUV %d " % i )
  116. for j in order:
  117. file.write( "%f %f " % (face.uv[j].x, face.uv[j].y) )
  118. file.write( "\n" )
  119. i = i+1
  120. ###############
  121. # vert groups #
  122. ###############
  123. vert_groups_names = mesh.getVertGroupNames()
  124. vert_groups_names.sort()
  125. file.write( "VERT_GROUPS_NUM %d\n" % (len(vert_groups_names)) )
  126. i = 0
  127. for vert_group_name in vert_groups_names:
  128. file.write( "\tVERT_GROUP %d NAME %s\n" %( i, vert_group_name ) )
  129. i = i+1
  130. ###########
  131. # weights #
  132. ###########
  133. # if there are vert groups then put weights
  134. if len(vert_groups_names) > 0:
  135. file.write( "VERTEX_WEIGHTS_NUM %d\n" % len( mesh.verts ) )
  136. i = 0
  137. for vert in mesh.verts: # for all verts
  138. infs = mesh.getVertexInfluences( vert.index )
  139. # make a check
  140. if len( infs ) > 4:
  141. print "-->ERROR: You cannot have more than 4 bones/groups per vert"
  142. file.close()
  143. return 0
  144. # calc the sum of all weights (and dividde later with the sum)
  145. sum_w = 0
  146. for inf in infs:
  147. sum_w = sum_w + inf[1]
  148. # now write to file
  149. file.write( "\tWEIGHTS %d VERT_ID %d PAIRS_NUM %d " %(i, i, len(infs)) )
  150. for inf in infs:
  151. # find group id
  152. j = 0
  153. for vert_group_name in vert_groups_names:
  154. if inf[0] == vert_group_name:
  155. break
  156. j = j+1
  157. # write to file
  158. file.write( "GROUP_ID %d WEIGHT %f " % ( j, inf[1]/sum_w ) )
  159. file.write("\n")
  160. i = i+1
  161. # else if there arent vert groups
  162. else:
  163. file.write( "VERTEX_WEIGHTS_NUM 0\n" )
  164. ########
  165. # DONE #
  166. ########
  167. print "Done! File \"%s\" created" % filename
  168. file.close()
  169. main()