-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodeshandler.py
More file actions
600 lines (552 loc) · 29.1 KB
/
Copy pathnodeshandler.py
File metadata and controls
600 lines (552 loc) · 29.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import bpy
from pathlib import Path
from . functions import ( ShowMessageBox, check_special_keywords, detect_a_map, find_file,
get_output_node, get_shader_node, get_target_mats, guess_sockets,
line_index, mat_name_cleaner, node_links, p_lines, props,
refresh_shader_links, safe_refresh, set_enum_sockets_items,
set_nodes_groups, stm_nodes,get_a_mat
)
class MaterialHolder():
def __init__(self):
self.wish = {}
self._mat = None
self._tree = None
self._nodes = None
self._links = None
@property
def mat(self):
if self._mat is None and bpy.context.object.active_material:
try :
self.mat = bpy.context.window_manager['current_mat'] = bpy.context.object.active_material
return bpy.context.object.active_material
except (TypeError,NameError,KeyError,ValueError,AttributeError,OverflowError):
pass
return self._mat
@mat.setter
def mat(self, value):
if value is None or isinstance(value, bpy.types.Material):
self._mat = value
else:
raise ValueError("mat must be a bpy.types.Material or None")
self._update_tree_and_nodes()
@property
def tree(self):
return self._tree
@tree.setter
def tree(self, value):
self._tree = value
@property
def nodes(self):
if not self._nodes and self.mat and self.mat.node_tree:
self._nodes = self.mat.node_tree.nodes
return self._nodes
@nodes.setter
def nodes(self, value):
self._nodes = value
@property
def links(self):
return self._links
@links.setter
def links(self, value):
self._links = value
def _update_tree_and_nodes(self):
if self._mat is not None:
self._tree = getattr(self._mat, 'node_tree', None)
self._nodes = getattr(self._tree, 'nodes', None)
self._links = getattr(self._tree, 'links', None)
else:
self._tree = None
self._nodes = None
self._links = None
class NodeHandler(MaterialHolder):
def __init__(self):
super().__init__()
self.coord_node = None
self.mapping_node = None
self.displaced = None
self.has_normal = None
self.has_height = None
self.add_curve = None
self.add_ramp = None
self.output_node = None
self.new_image_node = None
self.extra_node = None
self.bump_map_node = None
self.normal_map_node = None
self.base_loc = (300.0, 300.0)
self.shader_node = None
self.report_content = []
self.ao_mix = None
self.socket = 'no_socket'
self.separate_rgb = None
self.iterator = 0
self.directx_converter = None
self.has_ao = False
self.vect_disp = None
self.disp_map_node = None
self.disp_vec_node = None
def handle_nodes(self,only_setup_nodes=False):
selected = get_target_mats(bpy.context)
self.report_content.clear()
for mat in selected:
self.mat = bpy.context.window_manager['current_mat'] = mat
self.mat.use_nodes = True
if not get_shader_node() and not props().replace_shader:
self.report_content.append(f"Connect a shader node to {self.mat.name} output node or enable Replace Shader in the STM panel Options")
continue
safe_refresh()
self.process_materials(only_setup_nodes)
def process_materials(self,only_setup_nodes=False):
self.clean_props()
self.setup_nodes() if only_setup_nodes else self.assign_images()
def clean_props(self):
self.coord_node = None
self.mapping_node = None
self.output_node = None
self.base_loc = (300.0, 300.0)
self.shader_node = None
self.report_content.clear()
self.iterator = 0
def clean_line_params(self):
self.displaced = None
self.has_normal = None
self.has_height = None
self.add_curve = None
self.add_ramp = None
self.separate_rgb = None
ao_nodes = [nod for nod in stm_nodes() if "NODES_Ambient_Occlusion_mixer" in nod.name]
self.ao_mix = ao_nodes[0] if len(ao_nodes) else None
self.socket = 'no_socket'
self.new_image_node = None
self.extra_node = None
self.bump_map_node = None
self.normal_map_node = None
self.directx_converter = None
self.disp_vec_node = None
self.disp_map_node = None
self.has_ao = False
self.vect_disp = None
def set_shader_node(self):
shader_node = get_shader_node()
if shader_node and 'GROUP' in shader_node.type and not props().include_ngroups:
shader_node = stm_nodes().new('ShaderNodeBsdfPrincipled')
self.links.new(shader_node.outputs[0], get_output_node().inputs[0])
if self.node_invalid(shader_node) or props().replace_shader or props().clear_nodes:
if props().replace_shader or not shader_node:
substitute_shader = props().shaders_list
else:
substitute_shader = shader_node.bl_idname
if props().include_ngroups:
#handles custom shaders
if substitute_shader in bpy.data.node_groups.keys() and props().replace_shader:
shader_node = stm_nodes().new('ShaderNodeGroup')
shader_node.node_tree = bpy.data.node_groups[substitute_shader]
else :
node_links().clear()
set_nodes_groups()
set_enum_sockets_items()
refresh_shader_links()
guess_sockets()
if substitute_shader in bpy.data.node_groups.keys() and props().replace_shader:
self.shader_node = shader_node = stm_nodes().new('ShaderNodeGroup')
self.shader_node.node_tree = bpy.data.node_groups[substitute_shader]
elif not shader_node:
shader_node = stm_nodes().new(props().shaders_list)
elif hasattr(bpy.types,substitute_shader) :
shader_node = stm_nodes().new(substitute_shader)
shader_node.name = "STM_" + shader_node.name
self.links.new(shader_node.outputs[0], self.output_node.inputs[0])
if not (self.node_invalid(shader_node) or props().clear_nodes):
self.copy_bsdf_parameters(get_shader_node(), shader_node)
self.shader_node = shader_node
def check_mat(self):
mat = get_a_mat()
if not mat:
ShowMessageBox("No valid material found",'FAKE_USER_ON')
return False
self.mat = bpy.context.window_manager['current_mat'] = mat
return True
def set_output_node(self):
shd = get_shader_node()
self.output_node = get_output_node()
if props().replace_shader or props().clear_nodes:
if self.output_node:
stm_nodes().remove(self.output_node)
self.output_node = stm_nodes().new("ShaderNodeOutputMaterial")
self.output_node.location = self.base_loc
if shd :
self.links.new(shd.outputs[0], self.output_node.inputs[0])
self.base_loc = self.output_node.location
def node_invalid(self,node):
invalidtypes = ["ADD_SHADER", "MIX_SHADER", "HOLDOUT"]
return (node.type in invalidtypes if node else True) or not self.output_node.inputs['Surface'].is_linked
def create_coords_nodes(self):
self.coord_node = stm_nodes().new('ShaderNodeTexCoord')
self.coord_node.label = f"{mat_name_cleaner(self.mat.name)} Coordinates"
self.coord_node.name = f"STM_{self.coord_node.name}"
def make_tex_mapping_nodes(self):
mat_name = mat_name_cleaner(self.mat.name)
self.mapping_node = stm_nodes().new('ShaderNodeMapping')
self.mapping_node.label = f"{mat_name} Mapping"
self.mapping_node.name = f"STM_{self.mapping_node.name}"
self.create_coords_nodes()
self.links.new(self.coord_node.outputs['UV'], self.mapping_node.inputs['Vector'])
def check_split_rgb(self,line):
if line.split_rgb:
self.separate_rgb = stm_nodes().new(type="ShaderNodeSeparateColor")
self.separate_rgb.name=f"STM_split_{line.name}"
self.separate_rgb.label=f"split_{line.name}"
self.separate_rgb.color = self.get_colors()[(line_index(line))%9]
self.separate_rgb.use_custom_color = True
def make_new_image_node(self,line):
self.new_image_node = stm_nodes().new(type="ShaderNodeTexImage")
self.new_image_node.name = f"STM_img_{line.name}"
self.new_image_node.label = line.name
self.new_image_node.use_custom_color = True
self.new_image_node.color = self.get_colors()[(line_index(line))%9]
self.links.new(self.mapping_node.outputs[0], self.new_image_node.inputs['Vector'])
self.check_split_rgb(line)
def set_bools_params(self,line):
socket = line.input_sockets
self.vect_disp = self.in_sockets(line,"Disp Vect")
self.displaced = self.in_sockets(line,"Displacement")
self.has_normal = self.in_sockets(line,"Normal")
self.has_height = "bump" in [check_special_keywords(w) for w in line.name.lower().strip().split(props().separators_list) ] or (self.has_normal and line.split_rgb)
self.has_ao = self.in_sockets(line,"Ambient Occlusion")
if props().tweak_levels:
skip_ramps = len([x for x in ["subsurface radius", "normal","disp. vector", "tangent"] if line.name.lower() in x]) > 0
self.add_curve = not self.has_height and "color" in socket.lower() or self.has_normal or "disp vector" in socket.lower() or "emission" in socket.lower() or line.split_rgb
self.add_ramp = self.has_height or (not (("color" in socket.lower()) or ("emission" in socket.lower())) and (not skip_ramps) )
def check_add_extras(self,line):
self.extra_node = None
if props().tweak_levels:
if self.add_curve:
self.extra_node = stm_nodes().new('ShaderNodeRGBCurve')
self.extra_node.name = f"STM_curve_{line.name}"
self.links.new(self.new_image_node.outputs[0], self.extra_node.inputs[1])
elif self.add_ramp:
ramp = stm_nodes().new('ShaderNodeValToRGB')
self.extra_node = ramp
self.extra_node.name = f"STM_ramp_{line.name}"
self.links.new(self.new_image_node.outputs[0], ramp.inputs[0])
if self.extra_node:
self.extra_node.label = f"{self.extra_node.name}"
self.extra_node.color = self.get_colors()[(line_index(line))%9]
self.extra_node.use_custom_color = True
def check_bump_node(self,line):
if not (self.has_height and not props().skip_normals):
return
self.bump_map_node = stm_nodes().new('ShaderNodeBump')
self.bump_map_node.name = f"STM_bump_{line.name}"
if not line.split_rgb:
self.links.new(self.new_image_node.outputs[0], self.bump_map_node.inputs[2])
self.bump_map_node.inputs[0].default_value = .5
self.bump_map_node.color = self.get_colors()[(line_index(line))%9]
self.bump_map_node.use_custom_color = True
if props().tweak_levels and not line.split_rgb:
self.links.new(self.extra_node.outputs[0], self.bump_map_node.inputs[2])
if not self.in_sockets(line,'Normal',True):
stm_nodes().remove(self.bump_map_node)
self.bump_map_node = None
if self.normal_map_node :
stm_nodes().remove(self.normal_map_node)
self.normal_map_node = None
def check_ao_mix(self,line):
if self.has_ao:
self.prepare_ao(line)
def check_shader_displacement(self,line):
if not self.displaced :
return
if self.bump_map_node :
stm_nodes().remove(self.bump_map_node)
self.bump_map_node = None
self.disp_map_node = stm_nodes().new('ShaderNodeDisplacement')
self.disp_map_node.color = self.get_colors()[(line_index(line))%9]
self.disp_map_node.use_custom_color = True
self.disp_map_node.name = f"STM_displace_{line.name}"
self.links.new(self.disp_map_node.outputs[0], self.output_node.inputs['Displacement'])
if props().tweak_levels:
self.links.new(self.extra_node.outputs[0], self.disp_map_node.inputs['Height'])
if self.has_height and not props().skip_normals:
pass
if self.has_normal and not props().skip_normals and self.normal_map_node:
self.links.new(self.normal_map_node.outputs[0], self.disp_map_node.inputs['Normal'])
if (not self.has_normal or props().skip_normals) and not props().tweak_levels:
self.links.new(self.new_image_node.outputs[0], self.disp_map_node.inputs['Height'])
def check_vector_displacement(self,line):
if not (('Disp Vector' in line.input_sockets) or 'Disp Vector' in [check_special_keywords(w) for w in line.name.lower().strip().split(props().separators_list) ]):
return
if self.bump_map_node :
stm_nodes().remove(self.bump_map_node)
self.bump_map_node = None
if self.normal_map_node :
stm_nodes().remove(self.normal_map_node)
self.normal_map_node = None
self.disp_vec_node = stm_nodes().new('ShaderNodeVectorDisplacement')
self.disp_vec_node.color = self.get_colors()[(line_index(line))%9]
self.disp_vec_node.use_custom_color = True
self.disp_vec_node.name = f"STM_vectdisp_{line.name}"
self.links.new(self.disp_vec_node.outputs[0], self.output_node.inputs['Displacement'])
if props().tweak_levels:
if self.extra_node:
self.links.new(self.extra_node.outputs[0], self.disp_vec_node.inputs['Vector'])
if not props().tweak_levels:
self.links.new(self.new_image_node.outputs[0], self.disp_vec_node.inputs['Vector'])
if line.split_rgb:
stm_nodes().remove(self.disp_vec_node)
self.disp_vec_node = None
self.vect_disp = False
def in_line(self,line,term):
if term.replace(" ","") in [check_special_keywords(w).replace(" ","") for w in line.name.lower().strip().split(props().separators_list) ]:
return True
return False
def in_sockets(self,line,term,only_sockets=False):
if self.in_line(line,term) and not only_sockets:
return True
if not line.split_rgb and term in line.input_sockets :
return True
if not line.split_rgb :
return False
for ch in line.channels.socket:
if term in ch.input_sockets:
return True
return False
def prepare_ao(self,line):
if len([nod for nod in stm_nodes() if "STM_ambient_occlusion_" in nod.name]) == 0:
self.ao_mix = stm_nodes().new('ShaderNodeMixShader')
self.ao_mix.name = f"STM_ambient_occlusion_{line.name}"
self.ao_mix.label = f"AO converter {line.name}"
self.ao_mix.color = self.get_colors()[(line_index(line))%9]
self.ao_mix.use_custom_color = True
self.links.new(self.ao_mix.outputs['Shader'], self.output_node.inputs['Surface'])
self.links.new(self.shader_node.outputs[0], self.ao_mix.inputs[2])
def check_normal_map_node(self,line):
if self.has_normal and not props().skip_normals and not line.split_rgb:
self.normal_map_node = stm_nodes().new('ShaderNodeNormalMap')
self.normal_map_node.name = f"STM_normal_map_{line.name}"
self.normal_map_node.color = self.get_colors()[(line_index(line))%9]
self.normal_map_node.use_custom_color = True
def check_opengl_mode(self,line):
if props().mode_opengl or line.split_rgb:
return
if self.has_normal or self.disp_vec_node :
self.directx_converter = self.make_rgb_green_inverted()
self.directx_converter.name = f"STM_directx_{line.name}"
self.directx_converter.label = "G-Inverter"
self.directx_converter.color = self.get_colors()[(line_index(line))%9]
self.directx_converter.use_custom_color = True
if self.directx_converter and self.extra_node:
stm_nodes().remove(self.extra_node)
self.extra_node = None
def handle_bumps(self,line):
self.check_add_extras(line)
self.check_normal_map_node(line)
self.check_bump_node(line)
self.check_ao_mix(line)
self.check_vector_displacement(line)
self.check_shader_displacement(line)
self.check_opengl_mode(line)
def plug_node(self,socket,val):
if self.shader_node and self.shader_node.inputs.get(val,''):
self.links.new(socket, self.shader_node.inputs[val])
def plug_multi(self,line):
self.links.new(self.new_image_node.outputs[0], self.separate_rgb.inputs[0])
for i,sock in enumerate(line.channels.socket):
if self.shader_node.inputs.get(sock.input_sockets,''):
self.plug_node(self.separate_rgb.outputs[i],sock.input_sockets)
if 'Normal' in sock.input_sockets:
if not props().skip_normals and self.bump_map_node and self.has_height :
self.plug_node(self.bump_map_node.outputs[0],sock.input_sockets)
self.links.new(self.separate_rgb.outputs[i], self.bump_map_node.inputs[2])
else:
self.plug_node(self.separate_rgb.outputs[i],sock.input_sockets)
if self.has_ao and 'Ambient Occlusion' in sock.input_sockets:
if self.separate_rgb.name.replace("STM_split_","") in self.ao_mix.name.replace("STM_ambient_occlusion_",""):
self.links.new(self.separate_rgb.outputs[i], self.ao_mix.inputs[0])
if 'Displacement' in sock.input_sockets and self.disp_map_node:
self.links.new(self.separate_rgb.outputs[i], self.disp_map_node.inputs[0])
if self.add_curve and self.extra_node:
self.links.new(self.new_image_node.outputs[0], self.extra_node.inputs[1])
self.links.new(self.extra_node.outputs[0], self.separate_rgb.inputs[0])
def plug_nodes_links(self,line):
if line.split_rgb:
self.plug_multi(line)
return
if self.has_height and not props().skip_normals and self.bump_map_node:
if self.displaced:
self.links.new(self.bump_map_node.outputs[0], self.output_node.inputs[2])
else:
self.plug_node(self.bump_map_node.outputs[0],line.input_sockets)
if not (self.has_height or self.has_normal) or props().skip_normals:
if self.ao_mix:
if line.name in self.ao_mix.label and "Ambient Occlusion" in line.input_sockets:
if self.new_image_node.name.replace("STM_img_","") in self.ao_mix.name.replace("STM_ambient_occlusion_",""):
self.links.new(self.new_image_node.outputs[0], self.ao_mix.inputs[0])
self.links.new(self.ao_mix.outputs['Shader'], self.output_node.inputs['Surface'])
elif not self.disp_vec_node and not self.disp_map_node :
self.plug_node(self.new_image_node.outputs[0],line.input_sockets)
if not props().tweak_levels:
if self.has_normal:
if self.normal_map_node:
self.links.new(self.new_image_node.outputs[0], self.normal_map_node.inputs[1])
self.plug_node(self.normal_map_node.outputs[0],line.input_sockets)
if props().skip_normals:
if self.normal_map_node:
stm_nodes().remove(self.normal_map_node)
self.normal_map_node = None
if not props().mode_opengl and not self.disp_vec_node and not self.ao_mix:
self.plug_node(self.directx_converter.outputs[0],line.input_sockets)
else:
if not self.directx_converter:
if self.normal_map_node:
self.links.new(self.new_image_node.outputs[0], self.normal_map_node.inputs[1])
if self.has_height and self.bump_map_node:
self.links.new(self.extra_node.outputs[0], self.bump_map_node.inputs[2])
self.plug_node(self.bump_map_node.outputs[0],line.input_sockets)
if props().tweak_levels and self.extra_node and not self.has_height and not self.normal_map_node and not self.disp_map_node and not self.disp_vec_node and not self.ao_mix:
self.plug_node(self.extra_node.outputs[0],line.input_sockets)
if props().tweak_levels and self.extra_node and self.normal_map_node :
self.plug_node(self.normal_map_node.outputs[0],line.input_sockets)
self.links.new(self.extra_node.outputs[0], self.normal_map_node.inputs[1])
if props().tweak_levels and self.extra_node and self.disp_vec_node :
self.links.new(self.extra_node.outputs[0], self.disp_vec_node.inputs[0])
if props().tweak_levels and self.extra_node and self.ao_mix:
self.links.new(self.extra_node.outputs[0], self.ao_mix.inputs[0])
if not props().mode_opengl and self.directx_converter:
self.links.new(self.new_image_node.outputs[0], self.directx_converter.inputs[0])
if self.normal_map_node :
self.links.new(self.directx_converter.outputs[0], self.normal_map_node.inputs[1])
self.plug_node(self.normal_map_node.outputs[0],line.input_sockets)
else :
self.plug_node(self.directx_converter.outputs[0],line.input_sockets)
if self.directx_converter and self.disp_vec_node :
self.links.new(self.directx_converter.outputs[0], self.disp_vec_node.inputs[0])
self.links.new(self.new_image_node.outputs[0], self.directx_converter.inputs[0])
def clean_stm_nodes(self):
og_node = None
if not props().replace_shader:
og_node = get_shader_node()
for node in reversed(stm_nodes()):
if node.name.startswith("STM_") and not node.bl_idname in props().shaders_list and not node == og_node:
stm_nodes().remove(node)
def copy_bsdf_parameters(self,source_node, target_node):
if source_node.bl_idname == target_node.bl_idname :
for input_source, input_target in zip(source_node.inputs, target_node.inputs):
if hasattr(input_source, 'default_value'):
input_target.default_value = input_source.default_value
def make_rgb_green_inverted(self):
material = self.mat
if material:
material.use_nodes = True
nodes = stm_nodes()
rgb_curves_node = nodes.new(type="ShaderNodeRGBCurve")
green_curve = rgb_curves_node.mapping.curves[1]
for i,point in enumerate(green_curve.points):
point.location = (point.location.x, (1.0-point.location.y)*(1-i))
return rgb_curves_node
return None
def setup_nodes(self):
self.clean_stm_nodes()
self.set_output_node()
self.set_shader_node()
if len(p_lines()) == 0:
return
keep_nodes = [self.output_node,self.shader_node]
if props().clear_nodes:
for nodes in [nd for nd in stm_nodes() if nd not in keep_nodes]:
stm_nodes().remove(nodes)
self.make_tex_mapping_nodes()
for line in p_lines():
self.clean_line_params()
if not line.manual:
detect_a_map(line)
self.set_bools_params(line)
self.make_new_image_node(line)
self.handle_bumps(line)
self.plug_nodes_links(line)
self.iterator += 1
self.report_content.append(f"Image texture node created in {self.mat.name} for {line.name} map ")
self.move_nodes()
def assign_images(self):
for line in p_lines():
line.file_name = find_file(line)
if not line.file_name:
self.report_content.append(f"No image found matching {line.name} for material: {self.mat.name} in folder {Path(props().usr_dir).name if props().usr_dir else 'SELECT MAPS FOLDER !'}")
continue
image_name = Path(line.file_name).name
node_images = stm_nodes().find(f"STM_{line.name}_img")
if not node_images:
self.report_content.append(f"node {image_name} not found, run Setup Nodes before ")
continue
if not Path(line.file_name).is_file():
self.report_content.append(f"Texture {image_name} not found ")
continue
nodes = [node for node in stm_nodes() if node.label and node.label in line.name]
if len(nodes) and nodes[0]:
image = bpy.data.images.load(filepath=line.file_name) if not image_name in bpy.data.images else bpy.data.images[image_name]
if not "Color" in check_special_keywords(line.name.replace(" ","").lower()):
image.colorspace_settings.name = 'Non-Color'
nodes[0].image = bpy.data.images[image.name]
self.report_content.append(f"Texture file {image_name} assigned to {line.name} node in {self.mat.name} ")
def strip_digits(self,_text):
if "STM_" in _text :
_text = _text.replace("STM_", "")
if _text[-3:].isdigit():
_text = _text[:-4]
return _text
def move_nodes(self):
imgs = [nod for nod in stm_nodes() if nod.type == 'TEX_IMAGE']
aos = [nod for nod in stm_nodes() if nod.bl_idname in ["ShaderNodeMixShader","ShaderNodeDisplacement","ShaderNodeVectorDisplacement"]]
bumps = [nod for nod in stm_nodes() if nod.bl_idname in "ShaderNodeBump"]
ramps = [nod for nod in stm_nodes() if nod.bl_idname in "ShaderNodeValToRGB"]
norm = [nod for nod in stm_nodes() if nod.bl_idname in "ShaderNodeNormalMap"]
curves = [nod for nod in stm_nodes() if nod.bl_idname in "ShaderNodeRGBCurve"]
splitters = [nod for nod in stm_nodes() if nod.bl_idname in "ShaderNodeSeparateColor"]
spacer = -50
delta = -260 + spacer
cols = 0.0
ao = 0
if len(aos):
ao = 1
if len(ramps) or len(curves):
cols += 1
if len(bumps) or len(norm):
cols += 0.5
if len(splitters):
cols += 0.5
self.mapping_node.location = (self.output_node.location.x + delta*(cols+3) + delta*ao -1.5*spacer, self.mapping_node.location.y + 250)
self.coord_node.location = (self.output_node.location.x + delta*(cols+3) + delta*ao +2*spacer, self.coord_node.location.y + 250)
self.shader_node.location = (self.output_node.location.x + delta*(1+ao) - 0.5*spacer - 1.5*spacer*ao, self.shader_node.location.y + 250)
remaining_nodes = [nod for nod in stm_nodes() if not nod in [self.output_node,self.shader_node,self.coord_node,self.mapping_node]]
for nod in remaining_nodes:
nod.location.y += 250
nod.location = self.shader_node.location
if nod.bl_idname in ["ShaderNodeMixShader","ShaderNodeDisplacement","ShaderNodeVectorDisplacement"]:
nod.location.x += -delta/2 - 3*spacer
if nod.bl_idname in ["ShaderNodeNormalMap","ShaderNodeBump"]:
nod.location.x += delta - 2.25*spacer
if nod.bl_idname in "ShaderNodeSeparateColor":
nod.location.x += delta*(cols) - 6*spacer*int(bool(len(ramps) or len(curves))) + cols*spacer -0.75*spacer*int(bool(len(bumps) or len(norm)))
nod.location.x += spacer*int(1-bool(len(ramps) or len(curves)))
if nod.bl_idname in ["ShaderNodeRGBCurve","ShaderNodeValToRGB"]:
nod.location.x += delta*(max(1,cols))+spacer
if nod.bl_idname in "ShaderNodeTexImage" :
nod.location.x += (delta)*(cols+1) +spacer
for i,nod in enumerate(imgs):
nod.location.y += (len(imgs)*150)/2 -50
nod.location.y += i*-330
for i,nod in enumerate(bumps+norm):
nod.location.y = stm_nodes()[f'STM_img_{nod.name.replace("STM_bump_","").replace("STM_normal_map_","")}'].location.y
for i,nod in enumerate(aos):
nod.location.y += (len(aos)*150)/2
nod.location.y += i*-250
for i,nod in enumerate(splitters):
nod.location.y = stm_nodes()[f'STM_img_{nod.name.replace("STM_split_","")}'].location.y
for i,nod in enumerate(curves):
nod.location.y = stm_nodes()[f'STM_img_{nod.name.replace("STM_curve_","").replace("STM_directx_","")}'].location.y
for i,nod in enumerate(ramps):
nod.location.y = stm_nodes()[f'STM_img_{nod.name.replace("STM_ramp_","")}'].location.y
def get_colors(self):
return [(0.126, 0.162, 0.126), (0.122, 0.180, 0.214), (0.169, 0.123, 0.238),
(0.223, 0.145, 0.210), (0.158, 0.115, 0.132), (0.15, 0.15, 0.15),
(0.218, 0.127, 0.127), (0.208, 0.162, 0.117), (0.176, 0.167, 0.108)]