skeleton.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import Blender
  2. import os.path
  3. def PrintMatrixR( matrix ):
  4. for i in range(0, 4):
  5. print "[",
  6. for j in range(0, 4):
  7. f = round( matrix[j][i], 5)
  8. if f >= 0 :
  9. print " %04f" % f,
  10. else:
  11. print "%04f" % f,
  12. print "]"
  13. ########
  14. # main #
  15. ########
  16. def main():
  17. print "\n\n---- Export Armature ----"
  18. ########
  19. # vars #
  20. ########
  21. file = 0
  22. path = "c:\\src\\VisualC++\\3d_engine\\models\\imp\\"
  23. armat = 0
  24. mesh = 0
  25. ##################################
  26. # make some checks and open file #
  27. ##################################
  28. objs = Blender.Object.GetSelected()
  29. if len( objs ) < 1:
  30. print "-->ERROR: No object selected"
  31. return 0
  32. obj = objs[0]
  33. if obj.getType() != "Mesh":
  34. print "-->ERROR: The selected object must be a mesh and not in a(n) " + objs[0].getType()
  35. return 0
  36. mesh = obj.getData(0,1)
  37. # get the armature
  38. if len( obj.modifiers )<1:
  39. print "-->ERROR: Mesh doesnt have modifiers"
  40. return 0
  41. for mod in obj.modifiers:
  42. if mod.type == Blender.Modifier.Types.ARMATURE:
  43. aobj = mod[Blender.Modifier.Settings.OBJECT]
  44. armat = Blender.Object.Get( aobj.name ).getData( 0, 1 )
  45. if armat == 0:
  46. print "-->ERROR: There is no modifier of type armature"
  47. return 0
  48. filename = path + "armature.txt"
  49. file = open( filename, "w" )
  50. # create bones dictonary
  51. bone_names = armat.bones.keys()
  52. bones_d = {}
  53. bone_names.sort()
  54. i = 0
  55. for bone_nam in bone_names:
  56. bones_d[ bone_nam ] = i
  57. i = i+1
  58. ###############
  59. # write bones #
  60. ###############
  61. file.write( "BONES_NUM " + str(len( bone_names )) + "\n" )
  62. i = 0
  63. bone_names.sort() # the bones are written in alpabetical order
  64. for bone_nam in bone_names:
  65. vgroup_names = mesh.getVertGroupNames()
  66. vgroup_names.sort()
  67. vg_i = 0
  68. for vgroup_nam in vgroup_names:
  69. if vgroup_nam == bone_nam:
  70. break
  71. vg_i = vg_i+1
  72. if vg_i == len(vgroup_names): vg_i=-1
  73. bone = armat.bones[ bone_nam ]
  74. file.write( "\tBONE %d\n \t\tNAME %s\n" %(i, bone.name) )
  75. """co = bone.head["ARMATURESPACE"]
  76. file.write( "\t\tHEAD_ARMATURESPACE %f %f %f\n" %(co.x, co.y, co.z) )
  77. co = bone.head["BONESPACE"]
  78. file.write( "\t\tHEAD_BONESPACE %f %f %f\n" %(co.x, co.y, co.z) )
  79. co = bone.tail["ARMATURESPACE"]
  80. file.write( "\t\tTAIL_ARMATURESPACE %f %f %f\n" %(co.x, co.y, co.z) )
  81. co = bone.tail["BONESPACE"]
  82. file.write( "\t\tTAIL_BONESPACE %f %f %f\n" %(co.x, co.y, co.z) )
  83. file.write( "\t\tMATRIX4_ARMATURESPACE " )
  84. for i_ in range(0, 4):
  85. for j_ in range(0, 4):
  86. file.write( "%f " % bone.matrix["ARMATURESPACE"][j_][i_] )
  87. file.write( "\n" )
  88. file.write( "\t\tMATRIX3_BONESPACE " )
  89. for i_ in range(0, 3):
  90. for j_ in range(0, 3):
  91. file.write( "%f " % bone.matrix["BONESPACE"][j_][i_] )
  92. file.write( "\n" )
  93. file.write("\t\tLENGTH %f\n" % bone.length)"""
  94. co = bone.head["ARMATURESPACE"]
  95. file.write( "\t\tHEAD %f %f %f\n" %(co.x, co.y, co.z) )
  96. co = bone.tail["ARMATURESPACE"]
  97. file.write( "\t\tTAIL %f %f %f\n" %(co.x, co.y, co.z) )
  98. # write the parent
  99. bpar = bone.parent
  100. if not bpar:
  101. file.write( "\t\tPARENT -1\n" )
  102. else:
  103. bpar_index = bones_d[ bpar.name ]
  104. file.write( "\t\tPARENT %d\n" %(bpar_index) )
  105. # write the childs
  106. file.write( "\t\tCHILDS_NUM %d CHILD_INDECES " % len(bone.children) )
  107. for bch in bone.children:
  108. file.write( "%d " % bones_d[bch.name] )
  109. file.write( "\n" )
  110. i = i+1
  111. print "Done! File \"%s\" created" % filename
  112. file.close()
  113. main()