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
24from io_coat3D import updateimage
25
26def find_index(objekti):
27
28    luku = 0
29    for tex in objekti.active_material.texture_slots:
30        if(not(hasattr(tex,'texture'))):
31            break
32        luku = luku +1
33    return luku
34
35
36def RemoveFbxNodes(objekti):
37    Node_Tree = objekti.active_material.node_tree
38    for node in Node_Tree.nodes:
39        if node.type != 'OUTPUT_MATERIAL':
40            Node_Tree.nodes.remove(node)
41        else:
42            output = node
43            output.location = 340,400
44    Prin_mat = Node_Tree.nodes.new(type="ShaderNodeBsdfPrincipled")
45    Prin_mat.location = 13, 375
46
47    Node_Tree.links.new(Prin_mat.outputs[0], output.inputs[0])
48
49
50def updatetextures(objekti): # Update 3DC textures
51
52    for index_mat in objekti.material_slots:
53
54        for node in index_mat.material.node_tree.nodes:
55            if (node.type == 'TEX_IMAGE'):
56                if (node.name == '3DC_color' or node.name == '3DC_metalness' or node.name == '3DC_rough' or node.name == '3DC_nmap'
57                or node.name == '3DC_displacement' or node.name == '3DC_emissive' or node.name == '3DC_AO' or node.name == '3DC_alpha'):
58                    try:
59                        node.image.reload()
60                    except:
61                        pass
62
63    for index_node_group in bpy.data.node_groups:
64
65        for node in index_node_group.nodes:
66            if (node.type == 'TEX_IMAGE'):
67                if (node.name == '3DC_color' or node.name == '3DC_metalness' or node.name == '3DC_rough' or node.name == '3DC_nmap'
68                or node.name == '3DC_displacement' or node.name == '3DC_emissive' or node.name == '3DC_AO' or node.name == '3DC_alpha'):
69                    try:
70                        node.image.reload()
71                    except:
72                        pass
73
74def testi(objekti, texture_info, index_mat_name, uv_MODE_mat, mat_index):
75    if uv_MODE_mat == 'UV':
76
77        uv_set_founded = False
78        for uvset in objekti.data.uv_layers:
79
80            if(uvset.name == texture_info):
81                uv_set_founded = True
82
83                break
84
85        if(uv_set_founded):
86            for uv_poly in objekti.data.uv_layers[texture_info].id_data.polygons:
87                if(mat_index == uv_poly.material_index):
88                    return True
89        else:
90            return False
91
92    elif uv_MODE_mat == 'MAT':
93        return (texture_info == index_mat_name)
94
95def readtexturefolder(objekti, mat_list, texturelist, is_new, udim_textures, udim_len): #read textures from texture file
96
97    # Let's check are we UVSet or MATERIAL modee
98    create_nodes = False
99    for ind, index_mat in enumerate(objekti.material_slots):
100
101        texcoat = {}
102        texcoat['color'] = []
103        texcoat['ao'] = []
104        texcoat['rough'] = []
105        texcoat['metalness'] = []
106        texcoat['nmap'] = []
107        texcoat['emissive'] = []
108        texcoat['emissive_power'] = []
109        texcoat['displacement'] = []
110        texcoat['alpha'] = []
111
112        create_group_node = False
113        if(udim_textures == False):
114            for slot_index, texture_info in enumerate(texturelist):
115
116                uv_MODE_mat = 'MAT'
117                for index, layer in enumerate(objekti.data.uv_layers):
118                    if(layer.name == texturelist[slot_index][0]):
119                        uv_MODE_mat = 'UV'
120                        break
121
122
123                if(testi(objekti, texturelist[slot_index][0], index_mat.name, uv_MODE_mat, ind)) :
124                    if (os.path.isfile(texture_info[3])):
125                        if texture_info[2] == 'color' or texture_info[2] == 'diffuse':
126                            if(index_mat.material.coat3D_diffuse):
127
128                                texcoat['color'].append(texture_info[3])
129                                create_nodes = True
130                            else:
131                                os.remove(texture_info[3])
132
133                        elif texture_info[2] == 'metalness' or texture_info[2] == 'specular' or texture_info[2] == 'reflection':
134                            if (index_mat.material.coat3D_metalness):
135                                texcoat['metalness'].append(texture_info[3])
136                                create_nodes = True
137                            else:
138                                os.remove(texture_info[3])
139
140                        elif texture_info[2] == 'rough' or texture_info[2] == 'roughness':
141                            if (index_mat.material.coat3D_roughness):
142                                texcoat['rough'].append(texture_info[3])
143                                create_nodes = True
144                            else:
145                                os.remove(texture_info[3])
146
147                        elif texture_info[2] == 'nmap' or texture_info[2] == 'normalmap' or texture_info[2] == 'normal_map'  or texture_info[2] == 'normal':
148                            if (index_mat.material.coat3D_normal):
149                                texcoat['nmap'].append(texture_info[3])
150                                create_nodes = True
151                            else:
152                                os.remove(texture_info[3])
153
154                        elif texture_info[2] == 'emissive':
155                            if (index_mat.material.coat3D_emissive):
156                                texcoat['emissive'].append(texture_info[3])
157                                create_nodes = True
158                            else:
159                                os.remove(texture_info[3])
160
161                        elif texture_info[2] == 'emissive_power':
162                            if (index_mat.material.coat3D_emissive):
163                                texcoat['emissive_power'].append(texture_info[3])
164                                create_nodes = True
165                            else:
166                                os.remove(texture_info[3])
167
168                        elif texture_info[2] == 'ao':
169                            if (index_mat.material.coat3D_ao):
170                                texcoat['ao'].append(texture_info[3])
171                                create_nodes = True
172                            else:
173                                os.remove(texture_info[3])
174
175                        elif texture_info[2].startswith('displacement'):
176                            if (index_mat.material.coat3D_displacement):
177                                texcoat['displacement'].append(texture_info[3])
178                                create_nodes = True
179                            else:
180                                os.remove(texture_info[3])
181
182                        elif texture_info[2] == 'alpha' or texture_info[2] == 'opacity':
183                            if (index_mat.material.coat3D_alpha):
184                                texcoat['alpha'].append(texture_info[3])
185                                create_nodes = True
186                            else:
187                                os.remove(texture_info[3])
188
189                        create_group_node = True
190
191
192        else:
193            for texture_info in texturelist:
194                if (os.path.isfile(texture_list[3])):
195                    if texture_info[2] == 'color' or texture_info[2] == 'diffuse':
196                        if texcoat['color'] == [] and texture_info[1] == '1001':
197                            texcoat['color'].append(texture_info[3])
198                            create_nodes = True
199                    elif texture_info[2] == 'metalness' or texture_info[2] == 'specular' or texture_info[
200                        2] == 'reflection':
201                        if texcoat['metalness'] == [] and texture_info[1] == '1001':
202                            texcoat['metalness'].append(texture_info[3])
203                            create_nodes = True
204                    elif texture_info[2] == 'rough' or texture_info[2] == 'roughness':
205                        if texcoat['rough'] == [] and texture_info[1] == '1001':
206                            texcoat['rough'].append(texture_info[3])
207                            create_nodes = True
208                    elif texture_info[2] == 'nmap' or texture_info[2] == 'normalmap' or texture_info[
209                        2] == 'normal_map' or texture_info[2] == 'normal':
210                        if texcoat['nmap'] == [] and texture_info[1] == '1001':
211                            texcoat['nmap'].append(texture_info[3])
212                            create_nodes = True
213                    elif texture_info[2] == 'emissive':
214                        if texcoat['emissive'] == [] and texture_info[1] == '1001':
215                            texcoat['emissive'].append(texture_info[3])
216                            create_nodes = True
217                    elif texture_info[2] == 'emissive_power':
218                        if texcoat['emissive_power'] == [] and texture_info[1] == '1001':
219                            texcoat['emissive_power'].append(texture_info[3])
220                            create_nodes = True
221                    elif texture_info[2] == 'ao':
222                        if texcoat['ao'] == [] and texture_info[1] == '1001':
223                            texcoat['ao'].append(texture_info[3])
224                            create_nodes = True
225                    elif texture_info[2].startswith('displacement'):
226                        if texcoat['displacement'] == [] and texture_info[1] == '1001':
227                            texcoat['displacement'].append(texture_info[3])
228                            create_nodes = True
229                    if texture_info[2] == 'alpha' or texture_info[2] == 'opacity':
230                        if texcoat['alpha'] == [] and texture_info[1] == '1001':
231                            texcoat['alpha'].append(texture_info[3])
232                            create_nodes = True
233                    create_group_node = True
234
235        if(create_nodes):
236            coat3D = bpy.context.scene.coat3D
237            path3b_n = coat3D.exchangeFolder
238            path3b_n += ('%slast_saved_3b_file.txt' % (os.sep))
239
240            if (os.path.isfile(path3b_n)):
241                export_file = open(path3b_n)
242                for line in export_file:
243                    objekti.coat3D.applink_3b_path = line
244                export_file.close()
245                coat3D.remove_path = True
246            createnodes(index_mat, texcoat, create_group_node, objekti, ind, is_new, udim_textures, udim_len)
247
248def createnodes(active_mat,texcoat, create_group_node, objekti, ind, is_new, udim_textures, udim_len): # Creates new nodes and link textures into them
249    bring_color = True # Meaning of these is to check if we can only update textures or do we need to create new nodes
250    bring_metalness = True
251    bring_roughness = True
252    bring_normal = True
253    bring_displacement = True
254    bring_emissive = True
255    bring_AO = True
256    bring_alpha = True
257
258    active_mat.material.show_transparent_back = False # HACK FOR BLENDER BUG
259
260    coat3D = bpy.context.scene.coat3D
261    coatMat = active_mat.material
262
263    coatMat.blend_method = 'BLEND'
264
265    if(coatMat.use_nodes == False):
266        coatMat.use_nodes = True
267    act_material = coatMat.node_tree
268    main_material = coatMat.node_tree
269    applink_group_node = False
270
271    # First go through all image nodes and let's check if it starts with 3DC and reload if needed
272
273    for node in coatMat.node_tree.nodes:
274        if (node.type == 'OUTPUT_MATERIAL'):
275            out_mat = node
276            break
277
278    for node in act_material.nodes:
279        if(node.name == '3DC_Applink' and node.type == 'GROUP'):
280            applink_group_node = True
281            act_material = node.node_tree
282            applink_tree = node
283            break
284
285
286    for node in act_material.nodes:
287        if (node.type != 'GROUP'):
288            if (node.type != 'GROUP_OUTPUT'):
289                if (node.type == 'TEX_IMAGE'):
290                    if (node.name == '3DC_color'):
291                        bring_color = False
292                        updateimage.update(texcoat, 'color', node, udim_textures, udim_len)
293                    elif (node.name == '3DC_metalness'):
294                        bring_metalness = False
295                        updateimage.update(texcoat, 'metalness', node, udim_textures, udim_len)
296                    elif (node.name == '3DC_rough'):
297                        bring_roughness = False
298                        updateimage.update(texcoat, 'rough', node, udim_textures, udim_len)
299                    elif (node.name == '3DC_nmap'):
300                        bring_normal = False
301                        updateimage.update(texcoat, 'nmap', node, udim_textures, udim_len)
302                    elif (node.name == '3DC_displacement'):
303                        bring_displacement = False
304                        updateimage.update(texcoat, 'displacement', node, udim_textures, udim_len)
305                    elif (node.name == '3DC_emissive'):
306                        bring_emissive = False
307                        updateimage.update(texcoat, 'emissive', node, udim_textures, udim_len)
308                    elif (node.name == '3DC_AO'):
309                        bring_AO = False
310                        updateimage.update(texcoat, 'ao', node, udim_textures, udim_len)
311                    elif (node.name == '3DC_alpha'):
312                        bring_alpha = False
313                        updateimage.update(texcoat, 'alpha', node, udim_textures, udim_len)
314
315
316        elif (node.type == 'GROUP' and node.name.startswith('3DC_')):
317            if (node.name == '3DC_color'):
318                bring_color = False
319            elif (node.name == '3DC_metalness'):
320                bring_metalness = False
321            elif (node.name == '3DC_rough'):
322                bring_roughness = False
323            elif (node.name == '3DC_nmap'):
324                bring_normal = False
325            elif (node.name == '3DC_displacement'):
326                bring_displacement = False
327            elif (node.name == '3DC_emissive'):
328                bring_emissive = False
329            elif (node.name == '3DC_AO'):
330                bring_AO = False
331            elif (node.name == '3DC_alpha'):
332                bring_alpha = False
333
334    #Let's start to build new node tree. Let's start linking with Material Output
335
336    if(create_group_node):
337        if(applink_group_node == False):
338            main_mat2 = out_mat.inputs['Surface'].links[0].from_node
339            for input_ind in main_mat2.inputs:
340                if(input_ind.is_linked):
341                    main_mat3 = input_ind.links[0].from_node
342                    if(main_mat3.type == 'BSDF_PRINCIPLED'):
343                        main_mat = main_mat3
344
345            group_tree = bpy.data.node_groups.new( type="ShaderNodeTree", name="3DC_Applink")
346            group_tree.outputs.new("NodeSocketColor", "Color")
347            group_tree.outputs.new("NodeSocketColor", "Metallic")
348            group_tree.outputs.new("NodeSocketColor", "Roughness")
349            group_tree.outputs.new("NodeSocketVector", "Normal map")
350            group_tree.outputs.new("NodeSocketColor", "Emissive")
351            group_tree.outputs.new("NodeSocketColor", "Displacement")
352            group_tree.outputs.new("NodeSocketColor", "Emissive Power")
353            group_tree.outputs.new("NodeSocketColor", "AO")
354            group_tree.outputs.new("NodeSocketColor", "Alpha")
355            applink_tree = act_material.nodes.new('ShaderNodeGroup')
356            applink_tree.name = '3DC_Applink'
357            applink_tree.node_tree = group_tree
358            applink_tree.location = -400, -100
359            act_material = group_tree
360            notegroup = act_material.nodes.new('NodeGroupOutput')
361            notegroup.location = 220, -260
362
363            if(texcoat['emissive'] != []):
364                from_output = out_mat.inputs['Surface'].links[0].from_node
365                if(from_output.type == 'BSDF_PRINCIPLED'):
366                    add_shader = main_material.nodes.new('ShaderNodeAddShader')
367                    emission_shader = main_material.nodes.new('ShaderNodeEmission')
368
369                    emission_shader.name = '3DC_Emission'
370
371                    add_shader.location = 420, 110
372                    emission_shader.location = 70, -330
373                    out_mat.location = 670, 130
374
375                    main_material.links.new(from_output.outputs[0], add_shader.inputs[0])
376                    main_material.links.new(add_shader.outputs[0], out_mat.inputs[0])
377                    main_material.links.new(emission_shader.outputs[0], add_shader.inputs[1])
378                    main_mat = from_output
379            else:
380                main_mat = out_mat.inputs['Surface'].links[0].from_node
381
382        else:
383            main_mat = out_mat.inputs['Surface'].links[0].from_node
384            index = 0
385            for node in coatMat.node_tree.nodes:
386                if (node.type == 'GROUP' and node.name =='3DC_Applink'):
387                    for in_node in node.node_tree.nodes:
388                        if(in_node.type == 'GROUP_OUTPUT'):
389                            notegroup = in_node
390                            index = 1
391                            break
392                if(index == 1):
393                    break
394
395        # READ DATA.JSON FILE
396
397        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'
398        with open(json_address, encoding='utf-8') as data_file:
399            data = json.loads(data_file.read())
400
401        if(out_mat.inputs['Surface'].is_linked == True):
402            if(bring_color == True and texcoat['color'] != []):
403                CreateTextureLine(data['color'], act_material, main_mat, texcoat, coat3D, notegroup,
404                                  main_material, applink_tree, out_mat, coatMat, objekti, ind, is_new, udim_textures, udim_len)
405
406            if(bring_metalness == True and texcoat['metalness'] != []):
407                CreateTextureLine(data['metalness'], act_material, main_mat, texcoat, coat3D, notegroup,
408                                  main_material, applink_tree, out_mat, coatMat, objekti, ind, is_new, udim_textures, udim_len)
409
410            if(bring_roughness == True and texcoat['rough'] != []):
411                CreateTextureLine(data['rough'], act_material, main_mat, texcoat, coat3D, notegroup,
412                                  main_material, applink_tree, out_mat, coatMat, objekti, ind, is_new, udim_textures, udim_len)
413
414            if(bring_normal == True and texcoat['nmap'] != []):
415                CreateTextureLine(data['nmap'], act_material, main_mat, texcoat, coat3D, notegroup,
416                                  main_material, applink_tree, out_mat, coatMat, objekti, ind, is_new, udim_textures, udim_len)
417
418            if (bring_emissive == True and texcoat['emissive'] != []):
419                CreateTextureLine(data['emissive'], act_material, main_mat, texcoat, coat3D, notegroup,
420                                  main_material, applink_tree, out_mat, coatMat, objekti, ind, is_new, udim_textures, udim_len)
421
422            if (bring_displacement == True and texcoat['displacement'] != []):
423                CreateTextureLine(data['displacement'], act_material, main_mat, texcoat, coat3D, notegroup,
424                                  main_material, applink_tree, out_mat, coatMat, objekti, ind, is_new, udim_textures, udim_len)
425            if (bring_alpha == True and texcoat['alpha'] != []):
426                CreateTextureLine(data['alpha'], act_material, main_mat, texcoat, coat3D, notegroup,
427                                  main_material, applink_tree, out_mat, coatMat, objekti, ind, is_new, udim_textures, udim_len)
428
429
430def CreateTextureLine(type, act_material, main_mat, texcoat, coat3D, notegroup, main_material, applink_tree, out_mat, coatMat, objekti, ind, is_new, udim_textures, udim_len):
431
432    node = act_material.nodes.new('ShaderNodeTexImage')
433    uv_node = act_material.nodes.new('ShaderNodeUVMap')
434    if (is_new):
435        uv_node.uv_map = objekti.data.uv_layers[ind].name
436    else:
437        uv_node.uv_map = objekti.data.uv_layers[0].name
438    act_material.links.new(uv_node.outputs[0], node.inputs[0])
439    uv_node.use_custom_color = True
440    uv_node.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
441
442    node.use_custom_color = True
443    node.color = (type['node_color'][0],type['node_color'][1],type['node_color'][2])
444
445
446    if type['name'] == 'nmap':
447        normal_node = act_material.nodes.new('ShaderNodeNormalMap')
448        normal_node.use_custom_color = True
449        normal_node.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
450
451        node.location = -671, -510
452        uv_node.location = -750, -600
453        normal_node.location = -350, -350
454        normal_node.name = '3DC_normalnode'
455
456    elif type['name'] == 'displacement':
457        disp_node = main_material.nodes.new('ShaderNodeDisplacement')
458
459        node.location = -630, -1160
460        disp_node.location = 90, -460
461        disp_node.inputs[2].default_value = 0.1
462        disp_node.name = '3DC_dispnode'
463
464    node.name = '3DC_' + type['name']
465    node.label = type['name']
466
467    if (type['name'] != 'displacement'):
468        for input_index in type['find_input']:
469            input_color = main_mat.inputs.find(input_index)
470            if(input_color != -1):
471                break
472
473    load_image = True
474
475    for image in bpy.data.images:
476
477        if(os.path.normpath(texcoat[type['name']][0]) == os.path.normpath(image.filepath)):
478
479            load_image = False
480            node.image = image
481
482            if(udim_textures):
483                node.image.source = 'TILED'
484                for udim_index in udim_len:
485                    if (udim_index != 1001):
486                        node.image.tiles.new(udim_index)
487
488            node.image.reload()
489
490            break
491
492    if (load_image):
493
494        node.image = bpy.data.images.load(os.path.normpath(texcoat[type['name']][0]))
495
496        if(udim_textures):
497            node.image.source = 'TILED'
498
499        for udim_index in udim_len:
500            if (udim_index != 1001):
501                node.image.tiles.new(udim_index)
502
503
504    if node.image and type['colorspace'] == 'noncolor':
505        node.image.colorspace_settings.is_data = True
506
507    if (coat3D.createnodes):
508
509        if(type['name'] == 'nmap'):
510            act_material.links.new(node.outputs[0], normal_node.inputs[1])
511            if(input_color != -1):
512                act_material.links.new(normal_node.outputs[0], main_mat.inputs[input_color])
513
514            act_material.links.new(normal_node.outputs[0], notegroup.inputs[type['input']])
515            if (main_mat.inputs[input_color].name == 'Normal' and input_color != -1):
516                main_material.links.new(applink_tree.outputs[type['input']], main_mat.inputs[input_color])
517
518        elif (type['name'] == 'displacement'):
519
520            rampnode = act_material.nodes.new('ShaderNodeValToRGB')
521            rampnode.name = '3DC_ColorRamp'
522            rampnode.use_custom_color = True
523            rampnode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
524            rampnode.location = -270, -956
525
526            act_material.links.new(node.outputs[0], rampnode.inputs[0])
527            act_material.links.new(rampnode.outputs[0], notegroup.inputs[5])
528
529            main_material.links.new(applink_tree.outputs[5], disp_node.inputs[0])
530            main_material.links.new(disp_node.outputs[0], out_mat.inputs[2])
531            coatMat.cycles.displacement_method = 'BOTH'
532
533        else:
534            if (texcoat['alpha'] != []):
535                if (type['name'] == 'alpha'):
536                    act_material.links.new(node.outputs[1], notegroup.inputs[8])
537            else:
538                if (type['name'] == 'color'):
539                    act_material.links.new(node.outputs[1], notegroup.inputs[8])
540            if(type['name'] != 'alpha'):
541                huenode = createExtraNodes(act_material, node, type)
542            else:
543                huenode = node
544                huenode.location = -100, -800
545
546            if(type['name'] != 'alpha'):
547                act_material.links.new(huenode.outputs[0], notegroup.inputs[type['input']])
548            if (main_mat.type != 'MIX_SHADER' and input_color != -1):
549                main_material.links.new(applink_tree.outputs[type['input']], main_mat.inputs[input_color])
550                if(type['name'] == 'color'): #Alpha connection into Principled shader
551                    main_material.links.new(applink_tree.outputs['Alpha'], main_mat.inputs['Alpha'])
552
553            else:
554                location = main_mat.location
555                #applink_tree.location = main_mat.location[0], main_mat.location[1] + 200
556
557            if(type['name'] == 'emissive'):
558                for material in main_material.nodes:
559                    if(material.name == '3DC_Emission'):
560                        main_material.links.new(applink_tree.outputs[type['input']], material.inputs[0])
561                        break
562
563        uv_node.location = node.location
564        uv_node.location[0] -= 300
565        uv_node.location[1] -= 200
566
567    else:
568        node.location = type['node_location'][0], type['node_location'][1]
569        if (tile_list == []):
570            uv_node.location = node.location
571            uv_node.location[0] -= 300
572        act_material.links.new(node.outputs[0], notegroup.inputs[type['input']])
573        if (input_color != -1):
574            main_material.links.new(applink_tree.outputs[type['input']], main_mat.inputs[input_color])
575
576
577def createExtraNodes(act_material, node, type):
578
579    curvenode = act_material.nodes.new('ShaderNodeRGBCurve')
580    curvenode.name = '3DC_RGBCurve'
581    curvenode.use_custom_color = True
582    curvenode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
583
584    if(type['huenode'] == 'yes'):
585        huenode = act_material.nodes.new('ShaderNodeHueSaturation')
586        huenode.name = '3DC_HueSaturation'
587        huenode.use_custom_color = True
588        huenode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
589    else:
590        huenode = act_material.nodes.new('ShaderNodeMath')
591        huenode.name = '3DC_HueSaturation'
592        huenode.operation = 'MULTIPLY'
593        huenode.inputs[1].default_value = 1
594        huenode.use_custom_color = True
595        huenode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
596
597
598    if(type['rampnode'] == 'yes'):
599        rampnode = act_material.nodes.new('ShaderNodeValToRGB')
600        rampnode.name = '3DC_ColorRamp'
601        rampnode.use_custom_color = True
602        rampnode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
603
604    if (type['rampnode'] == 'yes'):
605        act_material.links.new(node.outputs[0], curvenode.inputs[1])
606        act_material.links.new(curvenode.outputs[0], rampnode.inputs[0])
607        if(type['huenode'] == 'yes'):
608            act_material.links.new(rampnode.outputs[0], huenode.inputs[4])
609        else:
610            act_material.links.new(rampnode.outputs[0], huenode.inputs[0])
611    else:
612        act_material.links.new(node.outputs[0], curvenode.inputs[1])
613        if (type['huenode'] == 'yes'):
614            act_material.links.new(curvenode.outputs[0], huenode.inputs[4])
615        else:
616            act_material.links.new(curvenode.outputs[0], huenode.inputs[0])
617
618    if type['name'] == 'metalness':
619        node.location = -1300, 119
620        curvenode.location = -1000, 113
621        rampnode.location = -670, 115
622        huenode.location = -345, 118
623
624    elif type['name'] == 'rough':
625        node.location = -1300, -276
626        curvenode.location = -1000, -245
627        rampnode.location = -670, -200
628        huenode.location = -340, -100
629
630    elif type['name'] == 'color':
631        node.location = -990, 530
632        curvenode.location = -660, 480
633        huenode.location = -337, 335
634
635    elif type['name'] == 'emissive':
636        node.location = -1200, -900
637        curvenode.location = -900, -900
638        huenode.location = -340, -700
639
640    elif type['name'] == 'alpha':
641        node.location = -1200, -1200
642        curvenode.location = -900, -1250
643        rampnode.location = -600, -1200
644        huenode.location = -300, -1200
645
646    return huenode
647
648def matlab(objekti,mat_list,texturelist,is_new):
649
650    # FBX Materials: remove all nodes and create princibles node
651
652    if(is_new):
653        RemoveFbxNodes(objekti)
654
655    updatetextures(objekti)
656
657    # Count udim tiles
658
659    if(texturelist != []):
660
661        udim_textures = False
662        udim_count = 0
663        udim_indexs = []
664
665        if texturelist[0][0].startswith('10') and  len(texturelist[0][0]) == 4:
666            udim_textures = True
667
668            udim_target = ''
669            udim_target  = texturelist[0][2]
670            for texture in texturelist:
671                if texture[2] == udim_target:
672                    udim_indexs.append(int(texture[0]))
673
674            udim_indexs.sort() # sort tiles list -> 1001, 1002, 1003...
675
676        # Main loop for creating nodes
677
678        readtexturefolder(objekti, mat_list, texturelist, is_new, udim_textures, udim_indexs)
679
680
681    return('FINISHED')
682