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