material.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Blender imports
  2. import bpy
  3. from bpy.types import Operator
  4. from bpy.props import StringProperty
  5. from bpy_extras.io_utils import ImportHelper
  6. # Local imports
  7. from ..lib import gui as GUI
  8. from ..lib import material as MAT
  9. from ..lib import mesh as MESH
  10. class ANKI_MATERIAL_clear_slot(Operator):
  11. """Clears a given material stol"""
  12. bl_idname = "anki.clear_texture_slot"
  13. bl_label = "Clear Slot by ID"
  14. bl_options = {'REGISTER', 'UNDO'}
  15. port_type = bpy.props.StringProperty(default = "diffuse")
  16. def execute(self, context):
  17. mat = context.active_object.active_material
  18. MAT.clear_texture_slot(mat, self.port_type)
  19. return {'FINISHED'}
  20. class ANKI_MATERIAL_apply_image(Operator, ImportHelper):
  21. bl_idname = "anki.apply_texture_image"
  22. bl_label = "Apply Material Image"
  23. bl_options = {'REGISTER', 'UNDO'}
  24. # filename_ext = ".jpg"
  25. filter_glob = GUI.image_filter_glob()
  26. filepath = GUI.image_file_path()
  27. mat_port_type = bpy.props.StringProperty(default ="diffuse")
  28. @classmethod
  29. def poll(cls, context):
  30. material = context.active_object.active_material
  31. return MAT.check_material(material)
  32. def execute(self, context):
  33. material = context.active_object.active_material
  34. img = MAT.get_image_node( self.filepath)
  35. texture_node = MAT.get_texture_node(self.filepath)
  36. texture_node.image = img
  37. texture_slot = MAT.get_texture_slot( material, self.mat_port_type)
  38. if self.mat_port_type == "diffuse":
  39. context.active_object.active_material.ANKI.diff_path = self.filepath
  40. MAT.set_diffuse_texture_attr(texture_slot, texture_node)
  41. if self.mat_port_type == "roughness":
  42. context.active_object.active_material.ANKI.spec_path = self.filepath
  43. texture_slot.texture = texture_node
  44. # texture_slot.use_map_specular = True
  45. # texture_slot.use_map_specular = 1.0
  46. texture_slot.use_map_color_diffuse = False
  47. texture_slot.use_map_hardness = True
  48. # texture_slot.use_map_hardness = 1.0
  49. MESH.set_textured(context.active_object, draw_texture = True)
  50. MAT.set_uv_texture_attr(texture_slot)
  51. return {'FINISHED'}
  52. def invoke(self, context, event):
  53. context.window_manager.fileselect_add(self)
  54. return {'RUNNING_MODAL'}