vweights.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from common import *
  2. import sys
  3. reload( sys.modules["common"] )
  4. #===================================================================================================
  5. # ScriptVWeights =
  6. #===================================================================================================
  7. def ScriptVWeights( mesh, skeleton, b_cmnts ):
  8. #check if mesh is the correct class
  9. if( mesh.__class__.__name__ != "Blender Mesh" ):
  10. ERROR( "The given func param is not a \"Blender Mesh\" class but a \"" + mesh.__class__.__name__ + "\"" )
  11. return "error"
  12. #check if skeleton is the correct class
  13. if( skeleton.__class__.__name__ != "Armature" ):
  14. ERROR( "The given func param is not a \"Armature\" class but a \"" + skeleton.__class__.__name__ + "\"" )
  15. return "error"
  16. bone_names = skeleton.bones.keys()
  17. bone_names.sort()
  18. # init text
  19. ftxt = ""
  20. # link the vert groups to the bone ids
  21. vgroup2bone_id = {} # we give the vgroup name and we get the bone's id in the skeleton
  22. vgroup_names = mesh.getVertGroupNames()
  23. for vgroup_name in vgroup_names:
  24. bone_id = -1
  25. for bone_name in bone_names:
  26. if bone_name == vgroup_name:
  27. bone_id = bone_names.index( bone_name )
  28. break
  29. if bone_id == -1:
  30. WARNING( "Vert group \"" + vgroup_name + "\" cant link to a bone" )
  31. vgroup2bone_id[ vgroup_name ] = bone_id
  32. if( b_cmnts ):ftxt += "/*VERTS*/ "
  33. ftxt += str( len( mesh.verts ) ) + "\n"
  34. # for every vert do some shit
  35. for vert in mesh.verts:
  36. influences = mesh.getVertexInfluences( vert.index )
  37. influences_num = 0
  38. sumw = 0.0
  39. # calc the influences num and the total weight (NOTE:we may have...
  40. # ...a vert group that doesnt connect to a bone)
  41. for influence in influences:
  42. vgroup = influence[0]
  43. weight = influence[1]
  44. if vgroup2bone_id[ vgroup ] != -1:
  45. influences_num = influences_num + 1
  46. sumw = sumw + weight
  47. # a check
  48. if( influences_num > 4 ):
  49. ERROR( "Cannot have more than 4 bones per vert" );
  50. return "error"
  51. # write to file
  52. if( b_cmnts ): ftxt += "\t/*VERT_ID " + str( vert.index ) + "*/\n"
  53. if( b_cmnts ): ftxt += "\t/*BONE_CONNECTIONS*/ "
  54. ftxt += str( influences_num ) + "\n"
  55. for influence in influences:
  56. vgroup = influence[0]
  57. weight = influence[1]
  58. if vgroup2bone_id[ vgroup ] != -1:
  59. if( b_cmnts ): ftxt += "\t\t/*BONE_ID*/ "
  60. ftxt += str( vgroup2bone_id[ vgroup ] ) + " "
  61. if( b_cmnts ): ftxt += "/*WEIGHT*/ "
  62. ftxt += str( weight/sumw ) + "\n"
  63. # end for all verts
  64. return ftxt
  65. #===================================================================================================
  66. # ExportVWeights =
  67. #===================================================================================================
  68. def ExportVWeights( path, mesh, skeleton, b_cmnts ):
  69. #check if mesh is the correct class
  70. if( mesh.__class__.__name__ != "Blender Mesh" ):
  71. ERROR( "The given func param is not a \"Blender Mesh\" class but a \"" + mesh.__class__.__name__ + "\"" )
  72. return 0
  73. #check if skeleton is the correct class
  74. if( skeleton.__class__.__name__ != "Armature" ):
  75. ERROR( "The given func param is not a \"Armature\" class but a \"" + skeleton.__class__.__name__ + "\"" )
  76. return 0
  77. INFO( "Trying to export vert weights for mesh \"" + mesh.name + "\"" )
  78. filename = path + mesh.name + ".vw"
  79. WriteFile( filename, ScriptVWeights( mesh, skeleton, b_cmnts ) )
  80. INFO( "Vert weights exported!! \"" + filename + "\"" )