Skip to content

Commit 9c85ea9

Browse files
committed
1_7_0
Added convert nodes and material name matching
1 parent 22fcf85 commit 9c85ea9

4 files changed

Lines changed: 208 additions & 32 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

22
.vscode/
3+
__pycache__

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Provides a material panel that creates Metallic/Roughness or Specular/Gloss Node
99
- Optional Auto Textures
1010
- Optional Ambient Occlusion Map
1111
- Optional Metallic Map
12+
- Convert Existing Nodes
13+
- Use Material Name for texture matching
1214

1315
# Roadmap
1416

@@ -37,3 +39,5 @@ Installing an Addon in Blender
3739
![alt](/screenshots/pbr_nodes_m_r.png)
3840

3941
![alt](/screenshots/pbr_fullscreen.png)
42+
43+
![alt](/screenshots/pbr_matt_name.png)

__init__.py

Lines changed: 203 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"name": "PBR",
2121
"description": "PBR Workflow Tools",
2222
"author": "Digiography.Studio",
23-
"version": (1, 6, 5),
23+
"version": (1, 7, 0),
2424
"blender": (2, 79, 0),
2525
"location": "Properties > Material > PBR Material",
2626
"wiki_url": "https://github.com/Digiography/blender_addon_pbr/wiki",
@@ -60,7 +60,140 @@ def execute(self, context):
6060

6161
return {'FINISHED'}
6262

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
64197

65198
class ds_pbr_auto_textures(Operator):
66199

@@ -90,48 +223,58 @@ def execute(self, context):
90223
_filename=filename.lower()
91224
_filepath=path.join(_path,filename)
92225

93-
# Base Color
226+
_match=True
94227

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:
100232

101-
if (_ds_pbr_material_options.option_nodes_type == "specular"):
233+
# Base Color
102234

103-
if 'specular' in _filename or '_spec' in _filename or '_s.' in _filename or '_s_' in _filename:
235+
if is_texture_base_color(_filename):
104236
_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
108240

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"):
112242

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
114248

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
118252

119-
# Ambient Occlusion
253+
# Roughness
120254

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
122258

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):
128262

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
130268

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
135278

136279
class ds_pbr_nodes_metallic_roughness(Operator):
137280

@@ -250,6 +393,8 @@ def execute(self, context):
250393

251394
return {'FINISHED'}
252395

396+
# Specular Gloss Nodes
397+
253398
class ds_pbr_nodes_specular_gloss(Operator):
254399

255400
bl_idname = "ds_pbr.nodes_specular_gloss"
@@ -452,6 +597,10 @@ def set_option_relative(self, value):
452597
get = get_option_relative,
453598
set = set_option_relative
454599
)
600+
option_use_matt_name = BoolProperty(
601+
name="Use Material Name",
602+
description="Use Material Name for image matching."
603+
)
455604
option_textures_path = StringProperty(
456605
name="Auto Textures Path",
457606
description="Use auto assign textures images to input nodes.",
@@ -712,6 +861,23 @@ def draw(self, context):
712861
_ds_pbr_material_options = context.material.ds_pbr_material_options
713862
_material = context.material or bpy.data.materials.new('Material')
714863
_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'
715881

716882
if ('ds_pbr_texture_normal' in _nodes):
717883

@@ -788,7 +954,10 @@ def draw(self, context):
788954
col=layout.row(align=True)
789955
box=col.row()
790956
box.prop(_ds_pbr_material_options, "option_relative")
957+
box=col.row()
958+
box.prop(_ds_pbr_material_options, "option_use_matt_name")
791959

960+
792961
# Auto Textures Folder
793962

794963
col=layout.row(align=True)
@@ -850,6 +1019,7 @@ def register():
8501019
register_class(ds_pbr_addon_prefs)
8511020
register_class(ds_pbr_material_options)
8521021
register_class(ds_pbr_nodes_remove)
1022+
register_class(ds_pbr_nodes_convert)
8531023
register_class(ds_pbr_auto_textures)
8541024
register_class(ds_pbr_nodes_metallic_roughness)
8551025
register_class(ds_pbr_nodes_specular_gloss)
@@ -881,6 +1051,7 @@ def unregister():
8811051
unregister_class(ds_pbr_addon_prefs)
8821052
unregister_class(ds_pbr_material_options)
8831053
unregister_class(ds_pbr_nodes_remove)
1054+
unregister_class(ds_pbr_nodes_convert)
8841055
unregister_class(ds_pbr_auto_textures)
8851056
unregister_class(ds_pbr_nodes_metallic_roughness)
8861057
unregister_class(ds_pbr_nodes_specular_gloss)

screenshots/pbr_matt_name.png

472 KB
Loading

0 commit comments

Comments
 (0)