Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tex.py « io_coat3D - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7af4d68e01b7a9e6bcdd6bbe3d95c3970422a59a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****

import bpy
import os
import re
def find_index(objekti):

    luku = 0
    for tex in objekti.active_material.texture_slots:
        if(not(hasattr(tex,'texture'))):
            break
        luku = luku +1
    return luku

def RemoveFbxNodes(objekti):
    Node_Tree = objekti.active_material.node_tree
    for node in Node_Tree.nodes:
        if node.type != 'OUTPUT_MATERIAL':
            Node_Tree.nodes.remove(node)
        else:
            output = node
            output.location = 340,400
    Prin_mat = Node_Tree.nodes.new(type="ShaderNodeBsdfPrincipled")
    Prin_mat.location = 13, 375

    Node_Tree.links.new(Prin_mat.outputs[0], output.inputs[0])

def readtexturefolder(objekti, mat_list, texturelist, is_new): #read textures from texture file

    create_nodes = False
    for index_mat in objekti.material_slots:

        texcoat = {}
        texcoat['color'] = []
        texcoat['ao'] = []
        texcoat['rough'] = []
        texcoat['metalness'] = []
        texcoat['nmap'] = []
        texcoat['disp'] = []
        texcoat['emissive'] = []
        texcoat['emissive_power'] = []
        texcoat['displacement'] = []


        for texture_info in texturelist:
            if texture_info[0] == index_mat.name:
                if texture_info[2] == 'color' or texture_info[2] == 'diffuse':
                    texcoat['color'].append(texture_info[3])
                    create_nodes = True
                if texture_info[2] == 'metalness' or texture_info[2] == 'specular' or texture_info[2] == 'reflection':
                    texcoat['metalness'].append(texture_info[3])
                    create_nodes = True
                if texture_info[2] == 'rough' or texture_info[2] == 'roughness':
                    texcoat['rough'].append(texture_info[3])
                    create_nodes = True
                if texture_info[2] == 'nmap' or texture_info[2] == 'normalmap' or texture_info[2] == 'normal_map':
                    texcoat['nmap'].append(texture_info[3])
                    create_nodes = True
                if texture_info[2] == 'emissive':
                    texcoat['emissive'].append(texture_info[3])
                    create_nodes = True
                if texture_info[2] == 'emissive_power':
                    texcoat['emissive_power'].append(texture_info[3])
                    create_nodes = True
                if texture_info[2] == 'ao':
                    texcoat['ao'].append(texture_info[3])
                    create_nodes = True
                if texture_info[2].startswith('displacement'):
                    texcoat['displacement'].append(texture_info[3])
                    create_nodes = True
        if(create_nodes):
            coat3D = bpy.context.scene.coat3D
            path3b_n = coat3D.exchangedir
            path3b_n += ('%slast_saved_3b_file.txt' % (os.sep))

            if (os.path.isfile(path3b_n)):
                export_file = open(path3b_n)
                for line in export_file:
                    objekti.coat3D.applink_3b_path = line
                export_file.close()
                coat3D.remove_path = True
            createnodes(index_mat, texcoat)

def checkmaterial(mat_list, objekti): #check how many materials object has
    mat_list = []

    for obj_mate in objekti.material_slots:
        if(obj_mate.material.use_nodes == False):
            obj_mate.material.use_nodes = True

def createnodes(active_mat,texcoat): # Cretes new nodes and link textures into them
    bring_color = True # Meaning of these is to check if we can only update textures or do we need to create new nodes
    bring_metalness = True
    bring_roughness = True
    bring_normal = True
    bring_displacement = True
    bring_AO = True

    coat3D = bpy.context.scene.coat3D
    coatMat = active_mat.material

    if(coatMat.use_nodes == False):
        coatMat.use_nodes = True
    act_material = coatMat.node_tree
    main_material = coatMat.node_tree
    applink_group_node = False
    #ensimmaiseksi kaydaan kaikki image nodet lapi ja tarkistetaan onko nimi 3DC alkunen jos on niin reload

    for node in coatMat.node_tree.nodes:
        if (node.type == 'OUTPUT_MATERIAL'):
            out_mat = node
            break

    for node in act_material.nodes:
        if(node.name == '3DC_Applink' and node.type == 'GROUP'):
            applink_group_node = True
            act_material = node.node_tree
            group_tree = node.node_tree
            applink_tree = node
            break

    print('TeXture UPDATE happens')
    for node in act_material.nodes:
        if(node.type == 'TEX_IMAGE'):
            if(node.name == '3DC_color'):
                bring_color = False
                node.image.reload()
            elif(node.name == '3DC_metalness'):
                bring_metalness = False
                node.image.reload()
            elif(node.name == '3DC_roughness'):
                bring_roughness = False
                node.image.reload()
            elif(node.name == '3DC_normal'):
                bring_normal = False
                node.image.reload()
            elif(node.name == '3DC_displacement'):
                bring_displacement = False
                node.image.reload()
            elif (node.name == '3DC_AO'):
                bring_AO = False
                node.image.reload()

    #seuraavaksi lahdemme rakentamaan node tree. Lahdetaan Material Outputista rakentaa

    if(applink_group_node == False and coat3D.creategroup):
        group_tree = bpy.data.node_groups.new( type="ShaderNodeTree", name="3DC_Applink")
        group_tree.outputs.new("NodeSocketColor", "Color")
        group_tree.outputs.new("NodeSocketColor", "Metallic")
        group_tree.outputs.new("NodeSocketColor", "Roughness")
        group_tree.outputs.new("NodeSocketVector", "Normal map")
        group_tree.outputs.new("NodeSocketColor", "Displacement")
        group_tree.outputs.new("NodeSocketColor", "Emissive")
        group_tree.outputs.new("NodeSocketColor", "Emissive Power")
        group_tree.outputs.new("NodeSocketColor", "AO")
        applink_tree = act_material.nodes.new('ShaderNodeGroup')
        applink_tree.name = '3DC_Applink'
        applink_tree.node_tree = group_tree
        applink_tree.location = -400, 300
        act_material = group_tree
        notegroup = act_material.nodes.new('NodeGroupOutput')
        notegroup.location = 220, -260
    else:
        index = 0
        for node in coatMat.node_tree.nodes:
            if (node.type == 'GROUP' and node.name =='3DC_Applink'):
                for in_node in node.node_tree.nodes:
                    if(in_node.type == 'GROUP_OUTPUT'):
                        notegroup = in_node
                        index = 1
                        break
            if(index == 1):
                break

    if(out_mat.inputs['Surface'].is_linked == True):
        main_mat = out_mat.inputs['Surface'].links[0].from_node
        if(main_mat.inputs.find('Base Color') == -1):
            input_color = main_mat.inputs.find('Color')
        else:
            input_color = main_mat.inputs.find('Base Color')

        ''' COLOR '''

        if(bring_color == True and texcoat['color'] != []):
            print('Color:', texcoat['color'][0])
            node = act_material.nodes.new('ShaderNodeTexImage')
            node.name = '3DC_color'
            node.label = 'Color'
            if (texcoat['color']):
               node.image = bpy.data.images.load(texcoat['color'][0])

            if(coat3D.createnodes):
                curvenode = act_material.nodes.new('ShaderNodeRGBCurve')
                curvenode.name = '3DC_RGBCurve'
                huenode = act_material.nodes.new('ShaderNodeHueSaturation')
                huenode.name = '3DC_HueSaturation'

                act_material.links.new(curvenode.outputs[0], huenode.inputs[4])
                act_material.links.new(node.outputs[0], curvenode.inputs[1])
                if(coat3D.creategroup):
                    act_material.links.new(huenode.outputs[0], notegroup.inputs[0])
                    if(main_mat.type != 'MIX_SHADER'):
                        main_material.links.new(applink_tree.outputs[0],main_mat.inputs[input_color])
                    else:
                        location = main_mat.location
                        applink_tree.location = main_mat.location[0], main_mat.location[1] + 200
                else:
                    act_material.links.new(huenode.outputs[0], main_mat.inputs[input_color])
                node.location = -990, 530
                curvenode.location = -660, 480
                huenode.location = -337, 335
            else:
                if (coat3D.creategroup):
                    node.location = -400, 400
                    act_material.links.new(node.outputs[0], notegroup.inputs[len(notegroup.inputs)-1])
                    if (input_color != -1):
                        main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], main_mat.inputs[input_color])

                else:
                    node.location = -400,400
                    if (input_color != -1):
                        act_material.links.new(node.outputs[0], main_mat.inputs[input_color])

        ''' METALNESS '''

        if(bring_metalness == True and texcoat['metalness'] != []):
            node = act_material.nodes.new('ShaderNodeTexImage')
            node.name='3DC_metalness'
            node.label = 'Metalness'
            input_color = main_mat.inputs.find('Metallic')
            if(texcoat['metalness']):
                node.image = bpy.data.images.load(texcoat['metalness'][0])
                node.color_space = 'NONE'
            if (coat3D.createnodes):
                curvenode = act_material.nodes.new('ShaderNodeRGBCurve')
                curvenode.name = '3DC_RGBCurve'
                huenode = act_material.nodes.new('ShaderNodeHueSaturation')
                huenode.name = '3DC_HueSaturation'

                act_material.links.new(curvenode.outputs[0], huenode.inputs[4])
                act_material.links.new(node.outputs[0], curvenode.inputs[1])

                if (coat3D.creategroup):
                    act_material.links.new(huenode.outputs[0], notegroup.inputs[1])
                    if (main_mat.type == 'BSDF_PRINCIPLED'):
                        main_material.links.new(applink_tree.outputs[1], main_mat.inputs[input_color])
                else:
                    act_material.links.new(huenode.outputs[0], main_mat.inputs[input_color])

                node.location = -994, 119
                curvenode.location = -668, 113
                huenode.location = -345, 118

            else:
                if (coat3D.creategroup):
                    node.location = -830, 160
                    act_material.links.new(node.outputs[0], notegroup.inputs[len(notegroup.inputs)-1])
                    if (input_color != -1):
                        main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], main_mat.inputs[input_color])
                else:
                    node.location = -830, 160
                    if (input_color != -1):
                        act_material.links.new(node.outputs[0], main_mat.inputs[input_color])

        ''' ROUGHNESS '''

        if(bring_roughness == True and texcoat['rough'] != []):
            node = act_material.nodes.new('ShaderNodeTexImage')
            node.name='3DC_roughness'
            node.label = 'Roughness'
            input_color = main_mat.inputs.find('Roughness')
            if(texcoat['rough']):
                node.image = bpy.data.images.load(texcoat['rough'][0])
                node.color_space = 'NONE'

            if (coat3D.createnodes):
                curvenode = act_material.nodes.new('ShaderNodeRGBCurve')
                curvenode.name = '3DC_RGBCurve'
                huenode = act_material.nodes.new('ShaderNodeHueSaturation')
                huenode.name = '3DC_HueSaturation'

                act_material.links.new(curvenode.outputs[0], huenode.inputs[4])
                act_material.links.new(node.outputs[0], curvenode.inputs[1])

                if (coat3D.creategroup):
                    act_material.links.new(huenode.outputs[0], notegroup.inputs[2])
                    if(main_mat.type == 'BSDF_PRINCIPLED'):
                        main_material.links.new(applink_tree.outputs[2], main_mat.inputs[input_color])
                else:
                    act_material.links.new(huenode.outputs[0], main_mat.inputs[input_color])

                node.location = -1000, -276
                curvenode.location = -670, -245
                huenode.location = -340, -100

            else:
                if (coat3D.creategroup):
                    node.location = -550, 0
                    act_material.links.new(node.outputs[0],notegroup.inputs[len(notegroup.inputs)-1])
                    if (input_color != -1):
                        main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], main_mat.inputs[input_color])

                else:
                    node.location = -550, 0
                    if (input_color != -1):
                        act_material.links.new(node.outputs[0], main_mat.inputs[input_color])

        ''' NORMAL MAP'''

        if(bring_normal == True and texcoat['nmap'] != []):
            node = act_material.nodes.new('ShaderNodeTexImage')
            normal_node = act_material.nodes.new('ShaderNodeNormalMap')

            node.location = -600,-670
            normal_node.location = -300,-300

            node.name='3DC_normal'
            node.label = 'Normal Map'
            normal_node.name='3DC_normalnode'
            if(texcoat['nmap']):
                node.image = bpy.data.images.load(texcoat['nmap'][0])
                node.color_space = 'NONE'
            input_color = main_mat.inputs.find('Normal')
            act_material.links.new(node.outputs[0], normal_node.inputs[1])
            act_material.links.new(normal_node.outputs[0], main_mat.inputs[input_color])
            if (coat3D.creategroup):
                act_material.links.new(normal_node.outputs[0], notegroup.inputs[3])
                if(main_mat.inputs[input_color].name == 'Normal'):
                    main_material.links.new(applink_tree.outputs[3], main_mat.inputs[input_color])

        ''' DISPLACEMENT '''

        if (bring_displacement == True and texcoat['displacement'] != []):
            node = act_material.nodes.new('ShaderNodeTexImage')
            node.name = '3DC_displacement'
            node.label = 'Displacement'
            # input_color = main_mat.inputs.find('Roughness') Blender 2.8 Does not support Displacement yet.
            if (texcoat['displacement']):
                node.image = bpy.data.images.load(texcoat['displacement'][0])
                node.color_space = 'NONE'

            if (coat3D.createnodes):
                '''
                curvenode = act_material.nodes.new('ShaderNodeRGBCurve')
                curvenode.name = '3DC_RGBCurve'
                huenode = act_material.nodes.new('ShaderNodeHueSaturation')
                huenode.name = '3DC_HueSaturation'

                act_material.links.new(curvenode.outputs[0], huenode.inputs[4])
                act_material.links.new(node.outputs[0], curvenode.inputs[1])
                '''

                if (coat3D.creategroup):
                    act_material.links.new(node.outputs[0], notegroup.inputs[4])

                    #if (main_mat.type == 'BSDF_PRINCIPLED'):
                        #main_material.links.new(applink_tree.outputs[2], main_mat.inputs[input_color])
                #else:
                    #act_material.links.new(huenode.outputs[0], main_mat.inputs[input_color])

                node.location = -276, -579

            else:
                if (coat3D.creategroup):
                    node.location = -550, 0
                    act_material.links.new(node.outputs[0], notegroup.inputs[len(notegroup.inputs) - 1])





def matlab(objekti,mat_list,texturelist,is_new):

    ''' FBX Materials: remove all nodes and create princibles node'''
    if(is_new):
        RemoveFbxNodes(objekti)

    '''Main Loop for Texture Update'''
    #checkmaterial(mat_list, objekti)
    readtexturefolder(objekti,mat_list,texturelist,is_new)

    return('FINISHED')