Node Wrangler: do not rely on image name to detect Viewer Node image
[blender-addons.git] / io_coat3D / texVR.py
blob95556758b846193c7b6b7b8991d271d97d2e20d2
1 # SPDX-FileCopyrightText: 2010-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
6 import os
7 import re
8 import json
10 def find_index(objekti):
12 luku = 0
13 for tex in objekti.active_material.texture_slots:
14 if(not(hasattr(tex,'texture'))):
15 break
16 luku = luku +1
17 return luku
20 def RemoveFbxNodes(objekti):
21 Node_Tree = objekti.active_material.node_tree
22 for node in Node_Tree.nodes:
23 if node.type != 'OUTPUT_MATERIAL':
24 Node_Tree.nodes.remove(node)
25 else:
26 output = node
27 output.location = 340,400
28 Prin_mat = Node_Tree.nodes.new(type="ShaderNodeBsdfPrincipled")
29 Prin_mat.location = 13, 375
31 Node_Tree.links.new(Prin_mat.outputs[0], output.inputs[0])
34 def UVTiling(objekti, index, texturelist):
35 """ Checks what Tiles are linked with Material """
37 objekti.coat3D.applink_scale = objekti.scale
38 tiles_index = []
39 tile_number =''
40 for poly in objekti.data.polygons:
41 if (poly.material_index == (index)):
42 loop_index = poly.loop_indices[0]
43 uv_x = objekti.data.uv_layers.active.data[loop_index].uv[0]
44 if(uv_x >= 0 and uv_x <=1):
45 tile_number_x = '1'
46 elif (uv_x >= 1 and uv_x <= 2):
47 tile_number_x = '2'
48 elif (uv_x >= 2 and uv_x <= 3):
49 tile_number_x = '3'
50 elif (uv_x >= 3 and uv_x <= 4):
51 tile_number_x = '4'
52 elif (uv_x >= 4 and uv_x <= 5):
53 tile_number_x = '5'
54 elif (uv_x >= 5 and uv_x <= 6):
55 tile_number_x = '6'
56 elif (uv_x >= 6 and uv_x <= 7):
57 tile_number_x = '7'
58 elif (uv_x >= 7 and uv_x <= 8):
59 tile_number_x = '8'
60 elif (uv_x >= 8 and uv_x <= 9):
61 tile_number_x = '9'
63 uv_y = objekti.data.uv_layers.active.data[loop_index].uv[1]
64 if (uv_y >= 0 and uv_y <= 1):
65 tile_number_y = '0'
66 elif (uv_y >= 1 and uv_y <= 2):
67 tile_number_y = '1'
68 elif (uv_x >= 2 and uv_y <= 3):
69 tile_number_y = '2'
70 elif (uv_x >= 3 and uv_y <= 4):
71 tile_number_y = '3'
72 elif (uv_x >= 4 and uv_y <= 5):
73 tile_number_y = '4'
74 elif (uv_x >= 5 and uv_y <= 6):
75 tile_number_y = '5'
76 elif (uv_x >= 6 and uv_y <= 7):
77 tile_number_y = '6'
78 elif (uv_x >= 7 and uv_y <= 8):
79 tile_number_y = '7'
80 elif (uv_x >= 8 and uv_y <= 9):
81 tile_number_y = '8'
83 tile_number = '10' + tile_number_y + tile_number_x
85 if tile_number not in tiles_index:
86 tiles_index.append(tile_number)
88 return tiles_index
90 def updatetextures(objekti): # Update 3DC textures
92 for index_mat in objekti.material_slots:
94 for node in index_mat.material.node_tree.nodes:
95 if (node.type == 'TEX_IMAGE'):
96 if (node.name == '3DC_color'):
97 node.image.reload()
98 elif (node.name == '3DC_metalness'):
99 node.image.reload()
100 elif (node.name == '3DC_rough'):
101 node.image.reload()
102 elif (node.name == '3DC_nmap'):
103 node.image.reload()
104 elif (node.name == '3DC_displacement'):
105 node.image.reload()
106 elif (node.name == '3DC_emissive'):
107 node.image.reload()
108 elif (node.name == '3DC_AO'):
109 node.image.reload()
110 elif (node.name == '3DC_alpha'):
111 node.image.reload()
113 for index_node_group in bpy.data.node_groups:
115 for node in index_node_group.nodes:
116 if (node.type == 'TEX_IMAGE'):
117 if (node.name == '3DC_color'):
118 node.image.reload()
119 elif (node.name == '3DC_metalness'):
120 node.image.reload()
121 elif (node.name == '3DC_rough'):
122 node.image.reload()
123 elif (node.name == '3DC_nmap'):
124 node.image.reload()
125 elif (node.name == '3DC_displacement'):
126 node.image.reload()
127 elif (node.name == '3DC_emissive'):
128 node.image.reload()
129 elif (node.name == '3DC_AO'):
130 node.image.reload()
131 elif (node.name == '3DC_alpha'):
132 node.image.reload()
135 def readtexturefolder(objekti, mat_list, texturelist, is_new, udim_textures): #read textures from texture file
137 # Let's check are we UVSet or MATERIAL mode
138 create_nodes = False
139 for ind, index_mat in enumerate(objekti.material_slots):
141 if(udim_textures):
142 tile_list = UVTiling(objekti,ind, texturelist)
143 else:
144 tile_list = []
146 texcoat = {}
147 texcoat['color'] = []
148 texcoat['ao'] = []
149 texcoat['rough'] = []
150 texcoat['metalness'] = []
151 texcoat['nmap'] = []
152 texcoat['emissive'] = []
153 texcoat['emissive_power'] = []
154 texcoat['displacement'] = []
155 texcoat['alpha'] = []
157 create_group_node = False
158 if(udim_textures == False):
159 for slot_index, texture_info in enumerate(texturelist):
161 uv_MODE_mat = 'MAT'
162 for index, layer in enumerate(objekti.data.uv_layers):
163 if(layer.name == texturelist[slot_index][0]):
164 uv_MODE_mat = 'UV'
165 break
167 print('aaa')
168 print(texture_info[0])
169 print(index_mat)
170 if(texture_info[0] == index_mat.name):
171 if texture_info[2] == 'color' or texture_info[2] == 'diffuse':
172 if(index_mat.material.coat3D_diffuse):
174 texcoat['color'].append(texture_info[3])
175 create_nodes = True
176 else:
177 os.remove(texture_info[3])
179 elif texture_info[2] == 'metalness' or texture_info[2] == 'specular' or texture_info[2] == 'reflection':
180 if (index_mat.material.coat3D_metalness):
181 texcoat['metalness'].append(texture_info[3])
182 create_nodes = True
183 else:
184 os.remove(texture_info[3])
186 elif texture_info[2] == 'rough' or texture_info[2] == 'roughness':
187 if (index_mat.material.coat3D_roughness):
188 texcoat['rough'].append(texture_info[3])
189 create_nodes = True
190 else:
191 os.remove(texture_info[3])
193 elif texture_info[2] == 'nmap' or texture_info[2] == 'normalmap' or texture_info[2] == 'normal_map' or texture_info[2] == 'normal':
194 if (index_mat.material.coat3D_normal):
195 texcoat['nmap'].append(texture_info[3])
196 create_nodes = True
197 else:
198 os.remove(texture_info[3])
200 elif texture_info[2] == 'emissive':
201 if (index_mat.material.coat3D_emissive):
202 texcoat['emissive'].append(texture_info[3])
203 create_nodes = True
204 else:
205 os.remove(texture_info[3])
207 elif texture_info[2] == 'emissive_power':
208 if (index_mat.material.coat3D_emissive):
209 texcoat['emissive_power'].append(texture_info[3])
210 create_nodes = True
211 else:
212 os.remove(texture_info[3])
214 elif texture_info[2] == 'ao':
215 if (index_mat.material.coat3D_ao):
216 texcoat['ao'].append(texture_info[3])
217 create_nodes = True
218 else:
219 os.remove(texture_info[3])
221 elif texture_info[2].startswith('displacement'):
222 if (index_mat.material.coat3D_displacement):
223 texcoat['displacement'].append(texture_info[3])
224 create_nodes = True
225 else:
226 os.remove(texture_info[3])
228 elif texture_info[2] == 'alpha' or texture_info[2] == 'opacity':
229 if (index_mat.material.coat3D_alpha):
230 texcoat['alpha'].append(texture_info[3])
231 create_nodes = True
232 else:
233 os.remove(texture_info[3])
235 create_group_node = True
237 else:
238 for texture_info in texturelist:
240 if texture_info[2] == 'color' or texture_info[2] == 'diffuse':
241 texcoat['color'].append([texture_info[0],texture_info[3]])
242 create_nodes = True
243 elif texture_info[2] == 'metalness' or texture_info[2] == 'specular' or texture_info[
244 2] == 'reflection':
245 texcoat['metalness'].append([texture_info[0],texture_info[3]])
246 create_nodes = True
247 elif texture_info[2] == 'rough' or texture_info[2] == 'roughness':
248 texcoat['rough'].append([texture_info[0],texture_info[3]])
249 create_nodes = True
250 elif texture_info[2] == 'nmap' or texture_info[2] == 'normalmap' or texture_info[
251 2] == 'normal_map' or texture_info[2] == 'normal':
252 texcoat['nmap'].append([texture_info[0],texture_info[3]])
253 create_nodes = True
254 elif texture_info[2] == 'emissive':
255 texcoat['emissive'].append([texture_info[0],texture_info[3]])
256 create_nodes = True
257 elif texture_info[2] == 'emissive_power':
258 texcoat['emissive_power'].append([texture_info[0],texture_info[3]])
259 create_nodes = True
260 elif texture_info[2] == 'ao':
261 texcoat['ao'].append([texture_info[0],texture_info[3]])
262 create_nodes = True
263 elif texture_info[2].startswith('displacement'):
264 texcoat['displacement'].append([texture_info[0],texture_info[3]])
265 create_nodes = True
266 if texture_info[2] == 'alpha' or texture_info[2] == 'opacity':
267 texcoat['alpha'].append([texture_info[0], texture_info[3]])
268 create_nodes = True
269 create_group_node = True
271 if(create_nodes):
272 coat3D = bpy.context.scene.coat3D
273 path3b_n = coat3D.exchangedir
274 path3b_n += ('%slast_saved_3b_file.txt' % (os.sep))
276 if (os.path.isfile(path3b_n)):
277 export_file = open(path3b_n)
278 for line in export_file:
279 objekti.coat3D.applink_3b_path = line
280 export_file.close()
281 coat3D.remove_path = True
282 createnodes(index_mat, texcoat, create_group_node, tile_list, objekti, ind, is_new)
284 def createnodes(active_mat,texcoat, create_group_node, tile_list, objekti, ind, is_new): # Creates new nodes and link textures into them
285 bring_color = True # Meaning of these is to check if we can only update textures or do we need to create new nodes
286 bring_metalness = True
287 bring_roughness = True
288 bring_normal = True
289 bring_displacement = True
290 bring_emissive = True
291 bring_AO = True
292 bring_alpha = True
294 coat3D = bpy.context.scene.coat3D
295 coatMat = active_mat.material
297 if(coatMat.use_nodes == False):
298 coatMat.use_nodes = True
300 act_material = coatMat.node_tree
301 main_material = coatMat.node_tree
302 applink_group_node = False
304 # First go through all image nodes and let's check if it starts with 3DC and reload if needed
306 for node in coatMat.node_tree.nodes:
307 if (node.type == 'OUTPUT_MATERIAL'):
308 out_mat = node
309 break
311 for node in act_material.nodes:
312 if(node.name == '3DC_Applink' and node.type == 'GROUP'):
313 applink_group_node = True
314 act_material = node.node_tree
315 applink_tree = node
316 break
318 for node in act_material.nodes:
319 if (node.type != 'GROUP'):
320 if (node.type != 'GROUP_OUTPUT'):
321 if (node.type == 'TEX_IMAGE'):
322 if (node.name == '3DC_color'):
323 bring_color = False
324 elif (node.name == '3DC_metalness'):
325 bring_metalness = False
326 elif (node.name == '3DC_rough'):
327 bring_roughness = False
328 elif (node.name == '3DC_nmap'):
329 bring_normal = False
330 elif (node.name == '3DC_displacement'):
331 bring_displacement = False
332 elif (node.name == '3DC_emissive'):
333 bring_emissive = False
334 elif (node.name == '3DC_AO'):
335 bring_AO = False
336 elif (node.name == '3DC_alpha'):
337 bring_alpha = False
338 elif (node.type == 'GROUP' and node.name.startswith('3DC_')):
339 if (node.name == '3DC_color'):
340 bring_color = False
341 elif (node.name == '3DC_metalness'):
342 bring_metalness = False
343 elif (node.name == '3DC_rough'):
344 bring_roughness = False
345 elif (node.name == '3DC_nmap'):
346 bring_normal = False
347 elif (node.name == '3DC_displacement'):
348 bring_displacement = False
349 elif (node.name == '3DC_emissive'):
350 bring_emissive = False
351 elif (node.name == '3DC_AO'):
352 bring_AO = False
353 elif (node.name == '3DC_alpha'):
354 bring_alpha = False
356 #Let's start to build new node tree. Let's start linking with Material Output
358 if(create_group_node):
359 if(applink_group_node == False):
360 main_mat2 = out_mat.inputs['Surface'].links[0].from_node
361 for input_ind in main_mat2.inputs:
362 if(input_ind.is_linked):
363 main_mat3 = input_ind.links[0].from_node
364 if(main_mat3.type == 'BSDF_PRINCIPLED'):
365 main_mat = main_mat3
367 group_tree = bpy.data.node_groups.new( type="ShaderNodeTree", name="3DC_Applink")
368 group_tree.outputs.new("NodeSocketColor", "Color")
369 group_tree.outputs.new("NodeSocketColor", "Metallic")
370 group_tree.outputs.new("NodeSocketColor", "Roughness")
371 group_tree.outputs.new("NodeSocketVector", "Normal map")
372 group_tree.outputs.new("NodeSocketColor", "Emissive")
373 group_tree.outputs.new("NodeSocketColor", "Displacement")
374 group_tree.outputs.new("NodeSocketColor", "Emissive Power")
375 group_tree.outputs.new("NodeSocketColor", "AO")
376 group_tree.outputs.new("NodeSocketColor", "Alpha")
377 applink_tree = act_material.nodes.new('ShaderNodeGroup')
378 applink_tree.name = '3DC_Applink'
379 applink_tree.node_tree = group_tree
380 applink_tree.location = -400, -100
381 act_material = group_tree
382 notegroup = act_material.nodes.new('NodeGroupOutput')
383 notegroup.location = 220, -260
385 if(texcoat['emissive'] != []):
386 from_output = out_mat.inputs['Surface'].links[0].from_node
387 if(from_output.type == 'BSDF_PRINCIPLED'):
388 add_shader = main_material.nodes.new('ShaderNodeAddShader')
389 emission_shader = main_material.nodes.new('ShaderNodeEmission')
391 emission_shader.name = '3DC_Emission'
393 add_shader.location = 420, 110
394 emission_shader.location = 70, -330
395 out_mat.location = 670, 130
397 main_material.links.new(from_output.outputs[0], add_shader.inputs[0])
398 main_material.links.new(add_shader.outputs[0], out_mat.inputs[0])
399 main_material.links.new(emission_shader.outputs[0], add_shader.inputs[1])
400 main_mat = from_output
401 else:
402 main_mat = out_mat.inputs['Surface'].links[0].from_node
404 else:
405 main_mat = out_mat.inputs['Surface'].links[0].from_node
406 index = 0
407 for node in coatMat.node_tree.nodes:
408 if (node.type == 'GROUP' and node.name =='3DC_Applink'):
409 for in_node in node.node_tree.nodes:
410 if(in_node.type == 'GROUP_OUTPUT'):
411 notegroup = in_node
412 index = 1
413 break
414 if(index == 1):
415 break
417 # READ DATA.JSON FILE
419 json_address = os.path.dirname(bpy.app.binary_path) + os.sep + str(bpy.app.version[0]) + '.' + str(bpy.app.version[1]) + os.sep + 'scripts' + os.sep + 'addons' + os.sep + 'io_coat3D' + os.sep + 'data.json'
420 with open(json_address, encoding='utf-8') as data_file:
421 data = json.loads(data_file.read())
423 if(out_mat.inputs['Surface'].is_linked == True):
424 if(bring_color == True and texcoat['color'] != []):
425 CreateTextureLine(data['color'], act_material, main_mat, texcoat, coat3D, notegroup,
426 main_material, applink_tree, out_mat, coatMat, tile_list, objekti, ind, is_new)
428 if(bring_metalness == True and texcoat['metalness'] != []):
429 CreateTextureLine(data['metalness'], act_material, main_mat, texcoat, coat3D, notegroup,
430 main_material, applink_tree, out_mat, coatMat, tile_list, objekti, ind, is_new)
432 if(bring_roughness == True and texcoat['rough'] != []):
433 CreateTextureLine(data['rough'], act_material, main_mat, texcoat, coat3D, notegroup,
434 main_material, applink_tree, out_mat, coatMat,tile_list, objekti, ind, is_new)
436 if(bring_normal == True and texcoat['nmap'] != []):
437 CreateTextureLine(data['nmap'], act_material, main_mat, texcoat, coat3D, notegroup,
438 main_material, applink_tree, out_mat, coatMat, tile_list, objekti, ind, is_new)
440 if (bring_emissive == True and texcoat['emissive'] != []):
441 CreateTextureLine(data['emissive'], act_material, main_mat, texcoat, coat3D, notegroup,
442 main_material, applink_tree, out_mat, coatMat, tile_list, objekti, ind, is_new)
444 if (bring_displacement == True and texcoat['displacement'] != []):
445 CreateTextureLine(data['displacement'], act_material, main_mat, texcoat, coat3D, notegroup,
446 main_material, applink_tree, out_mat, coatMat, tile_list, objekti, ind, is_new)
447 if (bring_alpha == True and texcoat['alpha'] != []):
448 CreateTextureLine(data['alpha'], act_material, main_mat, texcoat, coat3D, notegroup,
449 main_material, applink_tree, out_mat, coatMat, tile_list, objekti, ind, is_new)
452 def CreateTextureLine(type, act_material, main_mat, texcoat, coat3D, notegroup, main_material, applink_tree, out_mat, coatMat, tile_list, objekti, ind, is_new):
454 if(tile_list):
455 texture_name = coatMat.name + '_' + type['name']
456 texture_tree = bpy.data.node_groups.new(type="ShaderNodeTree", name=texture_name)
457 texture_tree.outputs.new("NodeSocketColor", "Color")
458 texture_tree.outputs.new("NodeSocketColor", "Alpha")
459 texture_node_tree = act_material.nodes.new('ShaderNodeGroup')
460 texture_node_tree.name = '3DC_' + type['name']
461 texture_node_tree.node_tree = texture_tree
462 texture_node_tree.location[0] = type['node_location'][0]
463 texture_node_tree.location[0] -= 400
464 texture_node_tree.location[1] = type['node_location'][1]
465 notegroupend = texture_tree.nodes.new('NodeGroupOutput')
467 count = len(tile_list)
468 uv_loc = [-1400, 200]
469 map_loc = [-1100, 200]
470 tex_loc = [-700, 200]
471 mix_loc = [-400, 100]
473 nodes = []
475 for index, tile in enumerate(tile_list):
477 tex_img_node = texture_tree.nodes.new('ShaderNodeTexImage')
479 for ind, tex_index in enumerate(texcoat[type['name']]):
480 if(tex_index[0] == tile):
481 tex_img_node.image = bpy.data.images.load(texcoat[type['name']][ind][1])
482 break
483 tex_img_node.location = tex_loc
485 if tex_img_node.image and type['colorspace'] != 'color':
486 tex_img_node.image.colorspace_settings.is_data = True
488 tex_uv_node = texture_tree.nodes.new('ShaderNodeUVMap')
489 tex_uv_node.location = uv_loc
490 if(is_new):
491 tex_uv_node.uv_map = objekti.data.uv_layers[ind].name
492 else:
493 tex_uv_node.uv_map = objekti.data.uv_layers[0].name
495 map_node = texture_tree.nodes.new('ShaderNodeMapping')
496 map_node.location = map_loc
497 map_node.name = '3DC_' + tile
498 map_node.vector_type = 'TEXTURE'
500 tile_int_x = int(tile[3])
501 tile_int_y = int(tile[2])
503 min_node = texture_tree.nodes.new('ShaderNodeVectorMath')
504 min_node.operation = "MINIMUM"
505 min_node.inputs[1].default_value[0] = tile_int_x - 1
506 min_node.inputs[1].default_value[1] = tile_int_y
508 max_node = texture_tree.nodes.new('ShaderNodeVectorMath')
509 max_node.operation = "MAXIMUM"
510 max_node.inputs[1].default_value[0] = tile_int_x
511 max_node.inputs[1].default_value[1] = tile_int_y + 1
514 if(index == 0):
515 nodes.append(tex_img_node.name)
516 if(count == 1):
517 texture_tree.links.new(tex_img_node.outputs[0], notegroupend.inputs[0])
518 texture_tree.links.new(tex_img_node.outputs[1], notegroupend.inputs[1])
520 if(index == 1):
521 mix_node = texture_tree.nodes.new('ShaderNodeMixRGB')
522 mix_node.blend_type = 'ADD'
523 mix_node.inputs[0].default_value = 1
524 mix_node.location = mix_loc
525 mix_loc[1] -= 300
526 texture_tree.links.new(tex_img_node.outputs[0], mix_node.inputs[2])
527 texture_tree.links.new(texture_tree.nodes[nodes[0]].outputs[0], mix_node.inputs[1])
528 mix_node_alpha = texture_tree.nodes.new('ShaderNodeMath')
529 mix_node_alpha.location = mix_loc
530 mix_loc[1] -= 200
531 texture_tree.links.new(tex_img_node.outputs[1], mix_node_alpha.inputs[1])
532 texture_tree.links.new(texture_tree.nodes[nodes[0]].outputs[1], mix_node_alpha.inputs[0])
533 nodes.clear()
534 nodes.append(tex_img_node.name)
535 nodes.append(mix_node.name)
536 nodes.append(mix_node_alpha.name)
539 elif(index > 1):
540 mix_node = texture_tree.nodes.new('ShaderNodeMixRGB')
541 mix_node.blend_type = 'ADD'
542 mix_node.inputs[0].default_value = 1
543 mix_node.location = mix_loc
544 mix_loc[1] -= 300
545 texture_tree.links.new(texture_tree.nodes[nodes[1]].outputs[0], mix_node.inputs[1])
546 texture_tree.links.new(tex_img_node.outputs[0], mix_node.inputs[2])
547 mix_node_alpha = texture_tree.nodes.new('ShaderNodeMath')
548 mix_node_alpha.location = mix_loc
549 mix_loc[1] -= 200
550 texture_tree.links.new(texture_tree.nodes[nodes[2]].outputs[0], mix_node_alpha.inputs[0])
551 texture_tree.links.new(tex_img_node.outputs[1], mix_node_alpha.inputs[1])
553 nodes.clear()
554 nodes.append(tex_img_node.name)
555 nodes.append(mix_node.name)
556 nodes.append(mix_node_alpha.name)
558 tex_loc[1] -= 300
559 uv_loc[1] -= 300
560 map_loc[1] -= 300
562 texture_tree.links.new(tex_uv_node.outputs[0], map_node.inputs[0])
563 texture_tree.links.new(map_node.outputs[0], min_node.inputs[0])
564 texture_tree.links.new(min_node.outputs['Vector'], max_node.inputs[0])
565 texture_tree.links.new(max_node.outputs['Vector'], tex_img_node.inputs[0])
567 if(count > 1):
568 texture_tree.links.new(mix_node.outputs[0], notegroupend.inputs[0])
569 texture_tree.links.new(mix_node_alpha.outputs[0], notegroupend.inputs[1])
571 if(tile_list):
572 node = texture_node_tree
573 if(texcoat['alpha'] != []):
574 if (type['name'] == 'color'):
575 act_material.links.new(node.outputs[1], notegroup.inputs[8])
576 else:
577 if(type['name'] == 'alpha'):
578 act_material.links.new(node.outputs[1], notegroup.inputs[8])
581 else:
582 node = act_material.nodes.new('ShaderNodeTexImage')
583 uv_node = act_material.nodes.new('ShaderNodeUVMap')
585 uv_node.uv_map = objekti.data.uv_layers[0].name
586 act_material.links.new(uv_node.outputs[0], node.inputs[0])
587 uv_node.use_custom_color = True
588 uv_node.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
590 node.use_custom_color = True
591 node.color = (type['node_color'][0],type['node_color'][1],type['node_color'][2])
594 if type['name'] == 'nmap':
595 normal_node = act_material.nodes.new('ShaderNodeNormalMap')
596 normal_node.use_custom_color = True
597 normal_node.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
599 node.location = -671, -510
600 if(tile_list == []):
601 uv_node.location = -750, -600
602 normal_node.location = -350, -350
603 normal_node.name = '3DC_normalnode'
605 elif type['name'] == 'displacement':
606 disp_node = main_material.nodes.new('ShaderNodeDisplacement')
608 node.location = -630, -1160
609 disp_node.location = 90, -460
610 disp_node.inputs[2].default_value = 0.1
611 disp_node.name = '3DC_dispnode'
613 node.name = '3DC_' + type['name']
614 node.label = type['name']
616 if (type['name'] != 'displacement'):
617 for input_index in type['find_input']:
618 input_color = main_mat.inputs.find(input_index)
619 if(input_color != -1):
620 break
622 if (tile_list == []):
624 load_image = True
626 for image in bpy.data.images:
627 if(texcoat[type['name']][0] == image.filepath):
628 load_image = False
629 node.image = image
630 break
632 if (load_image):
633 node.image = bpy.data.images.load(texcoat[type['name']][0])
635 if node.image and type['colorspace'] == 'noncolor':
636 node.image.colorspace_settings.is_data = True
638 if (coat3D.createnodes):
640 if(type['name'] == 'nmap'):
641 act_material.links.new(node.outputs[0], normal_node.inputs[1])
642 if(input_color != -1):
643 act_material.links.new(normal_node.outputs[0], main_mat.inputs[input_color])
645 act_material.links.new(normal_node.outputs[0], notegroup.inputs[type['input']])
646 if (main_mat.inputs[input_color].name == 'Normal' and input_color != -1):
647 main_material.links.new(applink_tree.outputs[type['input']], main_mat.inputs[input_color])
649 elif (type['name'] == 'displacement'):
651 rampnode = act_material.nodes.new('ShaderNodeValToRGB')
652 rampnode.name = '3DC_ColorRamp'
653 rampnode.use_custom_color = True
654 rampnode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
655 rampnode.location = -270, -956
657 act_material.links.new(node.outputs[0], rampnode.inputs[0])
658 act_material.links.new(rampnode.outputs[0], notegroup.inputs[5])
660 main_material.links.new(applink_tree.outputs[5], disp_node.inputs[0])
661 main_material.links.new(disp_node.outputs[0], out_mat.inputs[2])
662 coatMat.displacement_method = 'BOTH'
664 else:
665 if (texcoat['alpha'] != []):
666 if (type['name'] == 'alpha'):
667 act_material.links.new(node.outputs[1], notegroup.inputs[8])
668 else:
669 if (type['name'] == 'color'):
670 act_material.links.new(node.outputs[1], notegroup.inputs[8])
671 if(type['name'] != 'alpha'):
672 huenode = createExtraNodes(act_material, node, type, notegroup)
673 else:
674 huenode = node
675 huenode.location = -100, -800
677 if(type['name'] != 'alpha'):
678 act_material.links.new(huenode.outputs[0], notegroup.inputs[type['input']])
679 if (main_mat.type != 'MIX_SHADER' and input_color != -1):
680 main_material.links.new(applink_tree.outputs[type['input']], main_mat.inputs[input_color])
681 if(type['name'] == 'color'): #Alpha connection into Principled shader
682 main_material.links.new(applink_tree.outputs['Alpha'], main_mat.inputs['Alpha'])
684 else:
685 location = main_mat.location
686 #applink_tree.location = main_mat.location[0], main_mat.location[1] + 200
688 if(type['name'] == 'emissive'):
689 for material in main_material.nodes:
690 if(material.name == '3DC_Emission'):
691 main_material.links.new(applink_tree.outputs[type['input']], material.inputs[0])
692 break
693 if(tile_list == []):
694 uv_node.location = node.location
695 uv_node.location[0] -= 300
696 uv_node.location[1] -= 200
698 else:
699 node.location = type['node_location'][0], type['node_location'][1]
700 if (tile_list == []):
701 uv_node.location = node.location
702 uv_node.location[0] -= 300
703 act_material.links.new(node.outputs[0], notegroup.inputs[type['input']])
704 if (input_color != -1):
705 main_material.links.new(applink_tree.outputs[type['input']], main_mat.inputs[input_color])
708 def createExtraNodes(act_material, node, type, notegroup):
710 curvenode = act_material.nodes.new('ShaderNodeRGBCurve')
711 curvenode.name = '3DC_RGBCurve'
712 curvenode.use_custom_color = True
713 curvenode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
715 if(type['huenode'] == 'yes'):
716 huenode = act_material.nodes.new('ShaderNodeHueSaturation')
717 huenode.name = '3DC_HueSaturation'
718 huenode.use_custom_color = True
719 huenode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
720 else:
721 huenode = act_material.nodes.new('ShaderNodeMath')
722 huenode.name = '3DC_HueSaturation'
723 huenode.operation = 'MULTIPLY'
724 huenode.inputs[1].default_value = 1
725 huenode.use_custom_color = True
726 huenode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
729 if(type['rampnode'] == 'yes'):
730 rampnode = act_material.nodes.new('ShaderNodeValToRGB')
731 rampnode.name = '3DC_ColorRamp'
732 rampnode.use_custom_color = True
733 rampnode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
735 if (type['rampnode'] == 'yes'):
736 act_material.links.new(node.outputs[0], curvenode.inputs[1])
737 act_material.links.new(curvenode.outputs[0], rampnode.inputs[0])
738 if(type['huenode'] == 'yes'):
739 act_material.links.new(rampnode.outputs[0], huenode.inputs[4])
740 else:
741 act_material.links.new(rampnode.outputs[0], huenode.inputs[0])
742 else:
743 act_material.links.new(node.outputs[0], curvenode.inputs[1])
744 if (type['huenode'] == 'yes'):
745 act_material.links.new(curvenode.outputs[0], huenode.inputs[4])
746 else:
747 act_material.links.new(curvenode.outputs[0], huenode.inputs[0])
749 if type['name'] == 'metalness':
750 node.location = -1300, 119
751 curvenode.location = -1000, 113
752 rampnode.location = -670, 115
753 huenode.location = -345, 118
755 elif type['name'] == 'rough':
756 node.location = -1300, -276
757 curvenode.location = -1000, -245
758 rampnode.location = -670, -200
759 huenode.location = -340, -100
761 elif type['name'] == 'color':
762 node.location = -990, 530
763 curvenode.location = -660, 480
764 huenode.location = -337, 335
766 elif type['name'] == 'emissive':
767 node.location = -1200, -900
768 curvenode.location = -900, -900
769 huenode.location = -340, -700
771 elif type['name'] == 'alpha':
772 node.location = -1200, -1200
773 curvenode.location = -900, -1250
774 rampnode.location = -600, -1200
775 huenode.location = -300, -1200
777 if(type['name'] == 'color'):
778 node_vertex = act_material.nodes.new('ShaderNodeVertexColor')
779 node_mixRGB = act_material.nodes.new('ShaderNodeMixRGB')
780 node_vectormath = act_material.nodes.new('ShaderNodeVectorMath')
782 node_mixRGB.blend_type = 'MULTIPLY'
783 node_mixRGB.inputs[0].default_value = 1
785 node_vectormath.operation = 'MULTIPLY'
786 node_vectormath.inputs[1].default_value = [2,2,2]
788 node_vertex.layer_name = 'Col'
790 node_vertex.location = -337, 525
791 node_mixRGB.location = 0, 425
793 act_material.links.new(node_vertex.outputs[0], node_mixRGB.inputs[1])
794 act_material.links.new(huenode.outputs[0], node_mixRGB.inputs[2])
795 act_material.links.new(node_vertex.outputs[1], notegroup.inputs[8])
796 act_material.links.new(node_mixRGB.outputs[0], node_vectormath.inputs[0])
798 return node_vectormath
800 return huenode
802 def matlab(objekti,mat_list,texturelist,is_new):
804 print('Welcome facture matlab function')
806 ''' FBX Materials: remove all nodes and create principle node'''
807 if(is_new):
808 RemoveFbxNodes(objekti)
810 '''Main Loop for Texture Update'''
812 updatetextures(objekti)
814 ''' Check if bind textures with UVs or Materials '''
816 if(texturelist != []):
818 udim_textures = False
819 if texturelist[0][0].startswith('100'):
820 udim_textures = True
822 if(udim_textures == False):
823 readtexturefolder(objekti,mat_list,texturelist,is_new, udim_textures)
824 else:
825 path = texturelist[0][3]
826 only_name = os.path.basename(path)
827 if(only_name.startswith(objekti.coat3D.applink_index)):
828 readtexturefolder(objekti, mat_list, texturelist, is_new, udim_textures)
831 return('FINISHED')