2
0

material.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import sys
  2. from common import *
  3. reload( sys.modules["common"] )
  4. #===================================================================================================
  5. # GetMaterial =
  6. #===================================================================================================
  7. def GetMaterial( mesh ):
  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 0
  12. # check
  13. mats = mesh.materials
  14. if( len(mats) < 1 or len(mats) > 1 ):
  15. ERROR( "Mesh \"" + mesh.getName() + "\" has non or too many materials" )
  16. return 0
  17. mat = mats[0]
  18. return mat
  19. #===================================================================================================
  20. # ScriptMaterial =
  21. #===================================================================================================
  22. def ScriptMaterial( mtl, rpath, b_cmnts ):
  23. #check if mesh is the correct class
  24. if( mtl.__class__.__name__ != "Blender Material" ):
  25. ERROR( "The given func param is not a \"Blender Material\" class but a \"" + mtl.__class__.__name__ + "\"" )
  26. return "error"
  27. ftxt = ""
  28. # write the colors
  29. col = mtl.getRGBCol()
  30. ftxt += "DIFFUSE_COL {" + str(col[0]) + " " + str(col[1]) + " " + str(col[2]) + " 1.0}\n"
  31. ftxt += "SPECULAR_COL {" + str(col[0]) + " " + str(col[1]) + " " + str(col[2]) + " 1.0}\n"
  32. # shininess
  33. # we borow the shininess factor from the specular hardness
  34. sh = mtl.getHardness()
  35. if sh > 128:
  36. WARNING( "Mat \"" + mtl.getName() + ": Choose spec hardness from 0 to 128" )
  37. sh = 128
  38. ftxt += "SHININESS " + str(sh) + "\n"
  39. #from now on only user defined vars
  40. ftxt += "USER_DEFINED_VARS\n{\n"
  41. #** TEXTURES **
  42. texs = mtl.getTextures()
  43. has_diff = 0
  44. has_norm = 0
  45. has_spec = 0
  46. for tex in texs:
  47. if tex != None:
  48. tex = tex.tex
  49. # get the script identifier
  50. if tex.name == "diffuse_map":
  51. has_diff = 1
  52. elif tex.name == "normal_map":
  53. has_norm = 1
  54. elif tex.name == "specular_map":
  55. has_spec = 1
  56. else:
  57. WARNING( "Mat \"" + mtl.getName() + "\": Tex name \"" + tex.name + "\" cannot be processed" )
  58. continue
  59. img = tex.getImage()
  60. # check if someone forgot to actually put an image
  61. if img == None:
  62. WARNING( "Mat \"" + mtl.getName() + "\": There is no image set for \"" + tex.name + "\" texture" )
  63. continue
  64. ftxt += "\t"
  65. # compute the fname
  66. fname = img.getFilename()
  67. if fname.find( rpath ) == -1: # the img is not in the engine's path
  68. WARNING( "Mat \"" + mtl.getName() + "\": Img \"" + fname + "\" is not in the engine's dir" )
  69. continue
  70. relative = fname.replace( rpath, "" )
  71. ftxt += "TEXTURE " + tex.name + " \"" + relative + "\"\n"
  72. #endif
  73. #endfor
  74. ftxt += "}\n"
  75. # shader
  76. # choose a shader from one of the standards
  77. shader_str = "ms_mp_"
  78. if has_diff:
  79. shader_str += "d"
  80. else:
  81. shader_str += "*"
  82. if has_norm:
  83. shader_str += "n"
  84. else:
  85. shader_str += "*"
  86. if has_spec:
  87. shader_str += "s"
  88. else:
  89. shader_str += "*"
  90. shader_str += "*****"
  91. shader_str = "shaders/" + shader_str + ".shdr"
  92. ftxt += "SHADER \"" + shader_str + "\"\n"
  93. # end
  94. return ftxt
  95. #===================================================================================================
  96. # ExportMaterial =
  97. #===================================================================================================
  98. def ExportMaterial( path, rpath, mtl, b_cmnts ):
  99. #check if mesh is the correct class
  100. if( mtl.__class__.__name__ != "Blender Material" ):
  101. ERROR( "The given func param is not a \"Blender Material\" class but a \"" + mtl.__class__.__name__ + "\"" )
  102. return false
  103. INFO( "Trying to export material \"" + mtl.getName() + "\"" )
  104. filename = path + mtl.getName() + ".mtl"
  105. WriteFile( filename, ScriptMaterial( mtl, rpath, b_cmnts ) )
  106. INFO( "Material exported!! \"" + filename + "\"" )