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. ambient = api.material.ambient_color(self.node)
  29. self[constants.AMBIENT] = utilities.rgb2int(ambient)
  30. emissive = api.material.emissive_color(self.node)
  31. self[constants.EMISSIVE] = utilities.rgb2int(emissive)
  32. vertex_color = api.material.use_vertex_colors(self.node)
  33. self[constants.VERTEX_COLORS] = vertex_color
  34. self[constants.BLENDING] = api.material.blending(self.node)
  35. if api.material.transparent(self.node):
  36. self[constants.TRANSPARENT] = True
  37. if api.material.double_sided(self.node):
  38. self[constants.SIDE] = constants.SIDE_DOUBLE
  39. self[constants.DEPTH_TEST] = api.material.depth_test(self.node)
  40. self[constants.DEPTH_WRITE] = api.material.depth_write(self.node)
  41. def _phong_attributes(self):
  42. """Parse phong specific attributes"""
  43. logger.debug("Material()._phong_attributes()")
  44. specular = api.material.specular_color(self.node)
  45. self[constants.SPECULAR] = utilities.rgb2int(specular)
  46. self[constants.SHININESS] = api.material.specular_coef(self.node)
  47. def _update_maps(self):
  48. """Parses maps/textures and updates the textures array
  49. with any new nodes found.
  50. """
  51. logger.debug("Material()._update_maps()")
  52. mapping = (
  53. (api.material.diffuse_map, constants.MAP),
  54. (api.material.specular_map, constants.SPECULAR_MAP),
  55. (api.material.light_map, constants.LIGHT_MAP)
  56. )
  57. for func, key in mapping:
  58. map_node = func(self.node)
  59. if map_node:
  60. logger.info('Found map node %s for %s', map_node, key)
  61. tex_inst = self.scene.texture(map_node.name)
  62. self[key] = tex_inst[constants.UUID]
  63. if self[constants.TYPE] == constants.THREE_PHONG:
  64. mapping = (
  65. (api.material.bump_map, constants.BUMP_MAP,
  66. constants.BUMP_SCALE, api.material.bump_scale),
  67. (api.material.normal_map, constants.NORMAL_MAP,
  68. constants.NORMAL_SCALE, api.material.normal_scale)
  69. )
  70. for func, map_key, scale_key, scale_func in mapping:
  71. map_node = func(self.node)
  72. if not map_node:
  73. continue
  74. logger.info("Found map node %s for %s", map_node, map_key)
  75. tex_inst = self.scene.texture(map_node.name)
  76. self[map_key] = tex_inst[constants.UUID]
  77. self[scale_key] = scale_func(self.node)