|
20 | 20 | "name": "PBR", |
21 | 21 | "description": "PBR Workflow Tools", |
22 | 22 | "author": "Digiography.Studio", |
23 | | - "version": (1, 6, 5), |
| 23 | + "version": (1, 7, 0), |
24 | 24 | "blender": (2, 79, 0), |
25 | 25 | "location": "Properties > Material > PBR Material", |
26 | 26 | "wiki_url": "https://github.com/Digiography/blender_addon_pbr/wiki", |
@@ -60,7 +60,140 @@ def execute(self, context): |
60 | 60 |
|
61 | 61 | return {'FINISHED'} |
62 | 62 |
|
63 | | -# Auto Textures - Match input nodes bases on filename |
| 63 | +# Texture Matching Functions |
| 64 | + |
| 65 | +def is_texture_base_color(filename): |
| 66 | + |
| 67 | + _filename=filename.lower() |
| 68 | + |
| 69 | + if 'base_color' in _filename or 'basecolor' in _filename or 'diffuse' in _filename or 'alberto' in _filename or '_alb.' in _filename or '_alb_' in _filename: |
| 70 | + return True |
| 71 | + else: |
| 72 | + return False |
| 73 | + |
| 74 | +def is_texture_normal(filename): |
| 75 | + |
| 76 | + _filename=filename.lower() |
| 77 | + |
| 78 | + if 'normal' in _filename or '_norm' in _filename or '_n.' in _filename or '_n_' in _filename or '_bump' in _filename: |
| 79 | + return True |
| 80 | + else: |
| 81 | + return False |
| 82 | + |
| 83 | +def is_texture_specular(filename): |
| 84 | + |
| 85 | + _filename=filename.lower() |
| 86 | + |
| 87 | + if 'specular' in _filename or '_spec' in _filename or '_s.' in _filename or '_s_' in _filename: |
| 88 | + return True |
| 89 | + else: |
| 90 | + return False |
| 91 | + |
| 92 | +def is_texture_roughness(filename): |
| 93 | + |
| 94 | + _filename=filename.lower() |
| 95 | + |
| 96 | + if 'roughness' in _filename or '_rough' in _filename: |
| 97 | + return True |
| 98 | + else: |
| 99 | + return False |
| 100 | + |
| 101 | +def is_texture_ao(filename): |
| 102 | + |
| 103 | + _filename=filename.lower() |
| 104 | + |
| 105 | + if 'ambient' in _filename or 'occlusion' in _filename or 'ambient_occlusion' in _filename or '_ao.' in _filename or '_ao_' in _filename: |
| 106 | + return True |
| 107 | + else: |
| 108 | + return False |
| 109 | + |
| 110 | +def is_texture_metallic(filename): |
| 111 | + |
| 112 | + _filename=filename.lower() |
| 113 | + |
| 114 | + if 'metallic' in _filename or '_m.' in _filename or '_m_' in _filename: |
| 115 | + return True |
| 116 | + else: |
| 117 | + return False |
| 118 | + |
| 119 | +# Convert Existing Nodes |
| 120 | + |
| 121 | +class ds_pbr_nodes_convert(Operator): |
| 122 | + |
| 123 | + bl_idname = "ds_pbr.nodes_convert" |
| 124 | + bl_label = "Convert Nodes" |
| 125 | + bl_context = "material" |
| 126 | + node_type = bpy.props.StringProperty( |
| 127 | + name="node_type", |
| 128 | + default = 'metallic_roughness' |
| 129 | + ) |
| 130 | + def execute(self, context): |
| 131 | + |
| 132 | + layout = self.layout |
| 133 | + |
| 134 | + _images={} |
| 135 | + |
| 136 | + _material = context.material |
| 137 | + _ds_pbr_material_options = context.material.ds_pbr_material_options |
| 138 | + |
| 139 | + if _material and _material.node_tree: |
| 140 | + |
| 141 | + _nodes = _material.node_tree.nodes |
| 142 | + |
| 143 | + for node in _nodes: |
| 144 | + |
| 145 | + if node.type=='TEX_IMAGE': |
| 146 | + |
| 147 | + _image_name = node.image.name |
| 148 | + |
| 149 | + # Base Color |
| 150 | + |
| 151 | + if is_texture_base_color(_image_name): |
| 152 | + _images['base_color']=node.image |
| 153 | + |
| 154 | + # Specular |
| 155 | + |
| 156 | + if (_ds_pbr_material_options.option_nodes_type == "specular"): |
| 157 | + |
| 158 | + if is_texture_specular(_image_name): |
| 159 | + _images['base_color']=node.image |
| 160 | + |
| 161 | + # Normal |
| 162 | + |
| 163 | + if is_texture_normal(_image_name): |
| 164 | + _images['normal']=node.image |
| 165 | + |
| 166 | + # Roughness |
| 167 | + |
| 168 | + if is_texture_roughness(_image_name): |
| 169 | + _images['roughness']=node.image |
| 170 | + |
| 171 | + # Ambient Occlusion |
| 172 | + |
| 173 | + if is_texture_ao(_image_name): |
| 174 | + _images['ao']=node.image |
| 175 | + |
| 176 | + # Metallic |
| 177 | + |
| 178 | + if is_texture_metallic(_image_name): |
| 179 | + _images['metallic']=node.image |
| 180 | + |
| 181 | + if len(_images)>0: |
| 182 | + |
| 183 | + ds_pbr_nodes_remove.execute(self,context) |
| 184 | + |
| 185 | + if self.node_type=='metallic_roughness': |
| 186 | + ds_pbr_nodes_metallic_roughness.execute(self,context) |
| 187 | + elif self.node_type=='specular_gloss': |
| 188 | + ds_pbr_nodes_specular_gloss.execute(self,context) |
| 189 | + |
| 190 | + for _image in _images: |
| 191 | + _nodes['ds_pbr_texture_'+_image].image = _images[_image] |
| 192 | + _ds_pbr_material_options['option_texture_'+_image+'_path']=_images[_image].name |
| 193 | + |
| 194 | + return {'FINISHED'} |
| 195 | + |
| 196 | +# Auto Textures - Match input nodes based on filename |
64 | 197 |
|
65 | 198 | class ds_pbr_auto_textures(Operator): |
66 | 199 |
|
@@ -90,48 +223,58 @@ def execute(self, context): |
90 | 223 | _filename=filename.lower() |
91 | 224 | _filepath=path.join(_path,filename) |
92 | 225 |
|
93 | | - # Base Color |
| 226 | + _match=True |
94 | 227 |
|
95 | | - if 'base_color' in _filename or 'basecolor' in _filename or 'alberto' in _filename or '_alb.' in _filename or '_alb_' in _filename: |
96 | | - _nodes['ds_pbr_texture_base_color'].image = bpy.data.images.load(_filepath) |
97 | | - context.material.ds_pbr_material_options.option_texture_base_color_path=_filepath |
98 | | - |
99 | | - # Specular |
| 228 | + if _ds_pbr_material_options.option_use_matt_name and _material.name.lower() not in _filename: |
| 229 | + _match=False |
| 230 | + |
| 231 | + if _match: |
100 | 232 |
|
101 | | - if (_ds_pbr_material_options.option_nodes_type == "specular"): |
| 233 | + # Base Color |
102 | 234 |
|
103 | | - if 'specular' in _filename or '_spec' in _filename or '_s.' in _filename or '_s_' in _filename: |
| 235 | + if is_texture_base_color(_filename): |
104 | 236 | _nodes['ds_pbr_texture_base_color'].image = bpy.data.images.load(_filepath) |
105 | | - context.material.ds_pbr_material_options.option_texture_base_color_path=_filepath |
106 | | - |
107 | | - # Normal |
| 237 | + _ds_pbr_material_options.option_texture_base_color_path=_filepath |
| 238 | + |
| 239 | + # Specular |
108 | 240 |
|
109 | | - if 'normal' in _filename or '_norm' in _filename or '_n.' in _filename or '_n_' in _filename: |
110 | | - _nodes['ds_pbr_texture_normal'].image = bpy.data.images.load(_filepath) |
111 | | - context.material.ds_pbr_material_options.option_texture_normal_path=_filepath |
| 241 | + if (_ds_pbr_material_options.option_nodes_type == "specular"): |
112 | 242 |
|
113 | | - # Roughness |
| 243 | + if is_texture_specular(_filename): |
| 244 | + _nodes['ds_pbr_texture_base_color'].image = bpy.data.images.load(_filepath) |
| 245 | + _ds_pbr_material_options.option_texture_base_color_path=_filepath |
| 246 | + |
| 247 | + # Normal |
114 | 248 |
|
115 | | - if 'roughness' in _filename: |
116 | | - _nodes['ds_pbr_texture_roughness'].image = bpy.data.images.load(_filepath) |
117 | | - context.material.ds_pbr_material_options.option_texture_roughness_path=_filepath |
| 249 | + if is_texture_normal(_filename): |
| 250 | + _nodes['ds_pbr_texture_normal'].image = bpy.data.images.load(_filepath) |
| 251 | + _ds_pbr_material_options.option_texture_normal_path=_filepath |
118 | 252 |
|
119 | | - # Ambient Occlusion |
| 253 | + # Roughness |
120 | 254 |
|
121 | | - if (_ds_pbr_material_options.option_ao_map == True): |
| 255 | + if is_texture_roughness(_filename): |
| 256 | + _nodes['ds_pbr_texture_roughness'].image = bpy.data.images.load(_filepath) |
| 257 | + _ds_pbr_material_options.option_texture_roughness_path=_filepath |
122 | 258 |
|
123 | | - if 'ambient_occlusion' in _filename or '_ao.' in _filename or '_ao_' in _filename: |
124 | | - _nodes['ds_pbr_texture_ao'].image = bpy.data.images.load(_filepath) |
125 | | - context.material.ds_pbr_material_options.option_texture_ao_path=_filepath |
126 | | - |
127 | | - # Metallic |
| 259 | + # Ambient Occlusion |
| 260 | + |
| 261 | + if (_ds_pbr_material_options.option_ao_map == True): |
128 | 262 |
|
129 | | - if (_ds_pbr_material_options.option_metallic_map == True): |
| 263 | + if is_texture_ao(_filename): |
| 264 | + _nodes['ds_pbr_texture_ao'].image = bpy.data.images.load(_filepath) |
| 265 | + _ds_pbr_material_options.option_texture_ao_path=_filepath |
| 266 | + |
| 267 | + # Metallic |
130 | 268 |
|
131 | | - if 'ds_pbr_texture_metallic' in _nodes: |
132 | | - if 'metallic' in _filename or '_m.' in _filename or '_m_' in _filename: |
133 | | - _nodes['ds_pbr_texture_metallic'].image = bpy.data.images.load(_filepath) |
134 | | - context.material.ds_pbr_material_options.option_texture_metallic_path=_filepath |
| 269 | + if (_ds_pbr_material_options.option_metallic_map == True): |
| 270 | + |
| 271 | + if 'ds_pbr_texture_metallic' in _nodes: |
| 272 | + |
| 273 | + if is_texture_metallic(_filename): |
| 274 | + _nodes['ds_pbr_texture_metallic'].image = bpy.data.images.load(_filepath) |
| 275 | + _ds_pbr_material_options.option_texture_metallic_path=_filepath |
| 276 | + |
| 277 | +# Metallic Roughness Nodes |
135 | 278 |
|
136 | 279 | class ds_pbr_nodes_metallic_roughness(Operator): |
137 | 280 |
|
@@ -250,6 +393,8 @@ def execute(self, context): |
250 | 393 |
|
251 | 394 | return {'FINISHED'} |
252 | 395 |
|
| 396 | +# Specular Gloss Nodes |
| 397 | + |
253 | 398 | class ds_pbr_nodes_specular_gloss(Operator): |
254 | 399 |
|
255 | 400 | bl_idname = "ds_pbr.nodes_specular_gloss" |
@@ -452,6 +597,10 @@ def set_option_relative(self, value): |
452 | 597 | get = get_option_relative, |
453 | 598 | set = set_option_relative |
454 | 599 | ) |
| 600 | + option_use_matt_name = BoolProperty( |
| 601 | + name="Use Material Name", |
| 602 | + description="Use Material Name for image matching." |
| 603 | + ) |
455 | 604 | option_textures_path = StringProperty( |
456 | 605 | name="Auto Textures Path", |
457 | 606 | description="Use auto assign textures images to input nodes.", |
@@ -712,6 +861,23 @@ def draw(self, context): |
712 | 861 | _ds_pbr_material_options = context.material.ds_pbr_material_options |
713 | 862 | _material = context.material or bpy.data.materials.new('Material') |
714 | 863 | _nodes = _material.node_tree.nodes |
| 864 | + |
| 865 | + if ('ds_pbr_texture_base_color' not in _nodes): |
| 866 | + |
| 867 | + if _material and _material.node_tree: |
| 868 | + |
| 869 | + _found = False |
| 870 | + _nodes = _material.node_tree.nodes |
| 871 | + |
| 872 | + for node in _nodes: |
| 873 | + |
| 874 | + if node.type=='TEX_IMAGE': |
| 875 | + _found=True |
| 876 | + |
| 877 | + if _found: |
| 878 | + |
| 879 | + layout.operator("ds_pbr.nodes_convert",text="Convert to Metallic Roughness").node_type='metallic_roughness' |
| 880 | + layout.operator("ds_pbr.nodes_convert",text="Convert to Specular Gloss").node_type='specular_gloss' |
715 | 881 |
|
716 | 882 | if ('ds_pbr_texture_normal' in _nodes): |
717 | 883 |
|
@@ -788,7 +954,10 @@ def draw(self, context): |
788 | 954 | col=layout.row(align=True) |
789 | 955 | box=col.row() |
790 | 956 | box.prop(_ds_pbr_material_options, "option_relative") |
| 957 | + box=col.row() |
| 958 | + box.prop(_ds_pbr_material_options, "option_use_matt_name") |
791 | 959 |
|
| 960 | + |
792 | 961 | # Auto Textures Folder |
793 | 962 |
|
794 | 963 | col=layout.row(align=True) |
@@ -850,6 +1019,7 @@ def register(): |
850 | 1019 | register_class(ds_pbr_addon_prefs) |
851 | 1020 | register_class(ds_pbr_material_options) |
852 | 1021 | register_class(ds_pbr_nodes_remove) |
| 1022 | + register_class(ds_pbr_nodes_convert) |
853 | 1023 | register_class(ds_pbr_auto_textures) |
854 | 1024 | register_class(ds_pbr_nodes_metallic_roughness) |
855 | 1025 | register_class(ds_pbr_nodes_specular_gloss) |
@@ -881,6 +1051,7 @@ def unregister(): |
881 | 1051 | unregister_class(ds_pbr_addon_prefs) |
882 | 1052 | unregister_class(ds_pbr_material_options) |
883 | 1053 | unregister_class(ds_pbr_nodes_remove) |
| 1054 | + unregister_class(ds_pbr_nodes_convert) |
884 | 1055 | unregister_class(ds_pbr_auto_textures) |
885 | 1056 | unregister_class(ds_pbr_nodes_metallic_roughness) |
886 | 1057 | unregister_class(ds_pbr_nodes_specular_gloss) |
|
0 commit comments