material.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from .. import constants, logger
  2. from . import base_classes, utilities, api
  3. class Material(base_classes.BaseNode):
  4. """Class that wraps material nodes"""
  5. def __init__(self, node, parent):
  6. logger.debug("Material().__init__(%s)", node)
  7. base_classes.BaseNode.__init__(self, node, parent,
  8. constants.MATERIAL)
  9. self._common_attributes()
  10. if self[constants.TYPE] == constants.THREE_PHONG:
  11. self._phong_attributes()
  12. textures = self.parent.options.get(constants.MAPS)
  13. if textures:
  14. self._update_maps()
  15. def _common_attributes(self):
  16. """Parse the common material attributes"""
  17. logger.debug('Material()._common_attributes()')
  18. dispatch = {
  19. constants.PHONG: constants.THREE_PHONG,
  20. constants.LAMBERT: constants.THREE_LAMBERT,
  21. constants.BASIC: constants.THREE_BASIC
  22. }
  23. shader_type = api.material.type(self.node)
  24. self[constants.TYPE] = dispatch[shader_type]
  25. diffuse = api.material.diffuse_color(self.node)
  26. self[constants.COLOR] = utilities.rgb2int(diffuse)
  27. if self[constants.TYPE] != constants.THREE_BASIC:
  28. emissive = api.material.emissive_color(self.node)
  29. self[constants.EMISSIVE] = utilities.rgb2int(emissive)
  30. vertex_color = api.material.use_vertex_colors(self.node)
  31. if vertex_color:
  32. self[constants.VERTEX_COLORS] = constants.VERTEX_COLORS_ON
  33. else:
  34. self[constants.VERTEX_COLORS] = constants.VERTEX_COLORS_OFF
  35. self[constants.BLENDING] = api.material.blending(self.node)
  36. if api.material.transparent(self.node):
  37. self[constants.TRANSPARENT] = True
  38. if api.material.double_sided(self.node):
  39. self[constants.SIDE] = constants.SIDE_DOUBLE
  40. self[constants.DEPTH_TEST] = api.material.depth_test(self.node)
  41. self[constants.DEPTH_WRITE] = api.material.depth_write(self.node)
  42. def _phong_attributes(self):
  43. """Parse phong specific attributes"""
  44. logger.debug("Material()._phong_attributes()")
  45. specular = api.material.specular_color(self.node)
  46. self[constants.SPECULAR] = utilities.rgb2int(specular)
  47. self[constants.SHININESS] = api.material.specular_coef(self.node)
  48. def _update_maps(self):
  49. """Parses maps/textures and updates the textures array
  50. with any new nodes found.
  51. """
  52. logger.debug("Material()._update_maps()")
  53. mapping = (
  54. (api.material.diffuse_map, constants.MAP),
  55. (api.material.specular_map, constants.SPECULAR_MAP),
  56. (api.material.light_map, constants.LIGHT_MAP)
  57. )
  58. for func, key in mapping:
  59. map_node = func(self.node)
  60. if map_node:
  61. logger.info('Found map node %s for %s', map_node, key)
  62. tex_inst = self.scene.texture(map_node.name)
  63. self[key] = tex_inst[constants.UUID]
  64. if self[constants.TYPE] == constants.THREE_PHONG:
  65. mapping = (
  66. (api.material.bump_map, constants.BUMP_MAP,
  67. constants.BUMP_SCALE, api.material.bump_scale),
  68. (api.material.normal_map, constants.NORMAL_MAP,
  69. constants.NORMAL_SCALE, api.material.normal_scale)
  70. )
  71. for func, map_key, scale_key, scale_func in mapping:
  72. map_node = func(self.node)
  73. if not map_node:
  74. continue
  75. logger.info("Found map node %s for %s", map_node, map_key)
  76. tex_inst = self.scene.texture(map_node.name)
  77. self[map_key] = tex_inst[constants.UUID]
  78. self[scale_key] = scale_func(self.node)