OLD_export_mesh.py 4.1 KB

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