diff options
author | Kalle-Samuli Riihikoski <haikalle@gmail.com> | 2019-01-24 16:36:51 +0300 |
---|---|---|
committer | Kalle-Samuli Riihikoski <haikalle@gmail.com> | 2019-01-24 16:52:49 +0300 |
commit | 0f1e494c5fd0a8015d826a68a0be6597455529a0 (patch) | |
tree | 743c00a125a30b16d00e2495b6a9b35f61334c8e /io_coat3D | |
parent | 4cc64c86b5949991392866628e1a48027563260c (diff) |
restructure texture part. Added json file
Diffstat (limited to 'io_coat3D')
-rw-r--r-- | io_coat3D/__init__.py | 57 | ||||
-rw-r--r-- | io_coat3D/data.json | 83 | ||||
-rw-r--r-- | io_coat3D/tex.py | 339 |
3 files changed, 245 insertions, 234 deletions
diff --git a/io_coat3D/__init__.py b/io_coat3D/__init__.py index fa7011ed..e52682df 100644 --- a/io_coat3D/__init__.py +++ b/io_coat3D/__init__.py @@ -991,8 +991,6 @@ class SCENE_PT_Settings_Update(ObjectButtonsPanel, bpy.types.Panel): col = flow.column() col.prop(coat3D, "importtextures", text="Update Textures") col = flow.column() - col.prop(coat3D, "creategroup", text="Group Nodes") - col = flow.column() col.prop(coat3D, "exportmod", text="Export with modifiers") class SCENE_PT_Settings_Folders(ObjectButtonsPanel, bpy.types.Panel): @@ -1174,46 +1172,6 @@ class SceneCoat3D(PropertyGroup): description="Alert if Exchange folder is not found", default=True ) - export_box: BoolProperty( - name="Export window", - description="Allows to skip export dialog", - default=True - ) - export_color: BoolProperty( - name="Export color", - description="Export color texture", - default=True - ) - export_spec: BoolProperty( - name="Export specular", - description="Export specular texture", - default=True - ) - export_normal: BoolProperty( - name="Export Normal", - description="Export normal texture", - default=True - ) - export_disp: BoolProperty( - name="Export Displacement", - description="Export displacement texture", - default=True - ) - export_position: BoolProperty( - name="Export Source Position", - description="Export source position", - default=True - ) - export_zero_layer: BoolProperty( - name="Export from Layer 0", - description="Export mesh from Layer 0", - default=True - ) - export_coarse: BoolProperty( - name="Export Coarse", - description="Export Coarse", - default=True - ) exportfile: BoolProperty( name="No Import File", description="Add Modifiers and export", @@ -1229,11 +1187,6 @@ class SceneCoat3D(PropertyGroup): description="Export modifiers", default=False ) - export_pos: BoolProperty( - name="Remember Position", - description="Remember position", - default=True - ) importtextures: BoolProperty( name="Bring Textures", description="Import Textures", @@ -1244,21 +1197,11 @@ class SceneCoat3D(PropertyGroup): description="Import Textures", default=True ) - creategroup: BoolProperty( - name="Bring Textures", - description="Import Textures", - default=True - ) importlevel: BoolProperty( name="Multires. Level", description="Bring Specific Multires Level", default=False ) - exportover: BoolProperty( - name="Export Obj", - description="Import Textures", - default=False - ) importmesh: BoolProperty( name="Mesh", description="Import Mesh", diff --git a/io_coat3D/data.json b/io_coat3D/data.json new file mode 100644 index 00000000..2a3020e1 --- /dev/null +++ b/io_coat3D/data.json @@ -0,0 +1,83 @@ +{ + + "color": { + "name": "color", + "colorspace": "color", + "normal": "no", + "displacement": "no", + "find_input": [ "Color", "Base Color" ], + "input": 0, + "rampnode": "no", + "node_location": [ -400, 400 ] + }, + + "metalness": { + "name": "metalness", + "colorspace": "color", + "normal": "no", + "displacement": "no", + "find_input": [ "Metallic" ], + "input": 1, + "rampnode": "yes", + "node_location": [ -830, 160 ] + }, + + "rough": { + "name": "rough", + "colorspace": "color", + "normal": "no", + "displacement": "no", + "find_input": [ "Roughness" ], + "input": 2, + "rampnode": "yes", + "node_location": [ -550, 0 ] + }, + + "nmap": { + "name": "nmap", + "colorspace": "noncolor", + "normal": "yes", + "displacement": "no", + "find_input": [ "Normal" ], + "input": 3, + "rampnode": "no", + "node_location": [ -650, -500 ], + "normal_node_location": [ -350, -350 ] + }, + + "displacement": { + "name": "displacement", + "colorspace": "color", + "normal": "no", + "displacement": "yes", + "rampnode": "yes", + "input": 4 + }, + + "emissive": { + "name": "emissive", + "colorspace": "color", + "normal": "no", + "displacement": "no", + "find_input": [ "Emissive" ], + "rampnode": "no", + "input": 5 + }, + + "emissive_power": { + "name": "emissive_power", + "colorspace": "color", + "normal": "no", + "displacement": "no", + "rampnode": "no" + }, + + "ao": { + "name": "ao", + "colorspace": "color", + "normal": "no", + "displacement": "no", + "rampnode": "yes" + } + +} diff --git a/io_coat3D/tex.py b/io_coat3D/tex.py index 4c8ea57e..9b0bcfbe 100644 --- a/io_coat3D/tex.py +++ b/io_coat3D/tex.py @@ -20,6 +20,8 @@ import bpy import os import re +import json + def find_index(objekti): luku = 0 @@ -29,6 +31,7 @@ def find_index(objekti): luku = luku +1 return luku + def RemoveFbxNodes(objekti): Node_Tree = objekti.active_material.node_tree for node in Node_Tree.nodes: @@ -42,6 +45,7 @@ def RemoveFbxNodes(objekti): Node_Tree.links.new(Prin_mat.outputs[0], output.inputs[0]) + def UVTiling(objekti, texturelist): """ Checks what materials are linked into UV """ @@ -70,6 +74,7 @@ def UVTiling(objekti, texturelist): print('uvtiles_index', uvtiles_index) return texturelist + def readtexturefolder(objekti, mat_list, texturelist, is_new): #read textures from texture file create_nodes = False @@ -87,7 +92,6 @@ def readtexturefolder(objekti, mat_list, texturelist, is_new): #read textures fr texcoat['rough'] = [] texcoat['metalness'] = [] texcoat['nmap'] = [] - texcoat['disp'] = [] texcoat['emissive'] = [] texcoat['emissive_power'] = [] texcoat['displacement'] = [] @@ -98,25 +102,25 @@ def readtexturefolder(objekti, mat_list, texturelist, is_new): #read textures fr 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': + elif 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': + elif 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' or texture_info[2] == 'normal': + elif texture_info[2] == 'nmap' or texture_info[2] == 'normalmap' or texture_info[2] == 'normal_map' or texture_info[2] == 'normal': texcoat['nmap'].append(texture_info[3]) create_nodes = True - if texture_info[2] == 'emissive': + elif texture_info[2] == 'emissive': texcoat['emissive'].append(texture_info[3]) create_nodes = True - if texture_info[2] == 'emissive_power': + elif texture_info[2] == 'emissive_power': texcoat['emissive_power'].append(texture_info[3]) create_nodes = True - if texture_info[2] == 'ao': + elif texture_info[2] == 'ao': texcoat['ao'].append(texture_info[3]) create_nodes = True - if texture_info[2].startswith('displacement'): + elif texture_info[2].startswith('displacement'): texcoat['displacement'].append(texture_info[3]) create_nodes = True create_group_node = True @@ -133,6 +137,7 @@ def readtexturefolder(objekti, mat_list, texturelist, is_new): #read textures fr coat3D.remove_path = True createnodes(index_mat, texcoat, create_group_node) + def checkmaterial(mat_list, objekti): #check how many materials object has mat_list = [] @@ -140,12 +145,14 @@ def checkmaterial(mat_list, objekti): #check how many materials object has if(obj_mate.material.use_nodes == False): obj_mate.material.use_nodes = True + def createnodes(active_mat,texcoat, create_group_node): # 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_emissive = True bring_AO = True coat3D = bpy.context.scene.coat3D @@ -180,22 +187,32 @@ def createnodes(active_mat,texcoat, create_group_node): # Cretes new nodes and l elif(node.name == '3DC_metalness'): bring_metalness = False node.image.reload() - elif(node.name == '3DC_roughness'): + elif(node.name == '3DC_rough'): bring_roughness = False node.image.reload() - elif(node.name == '3DC_normal'): + elif(node.name == '3DC_nmap'): bring_normal = False node.image.reload() elif(node.name == '3DC_displacement'): bring_displacement = False node.image.reload() + elif (node.name == '3DC_emissive'): + bring_emissive = 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(create_group_node): - if(applink_group_node == False and coat3D.creategroup): + if(applink_group_node == False): + main_mat2 = out_mat.inputs['Surface'].links[0].from_node + for input_ind in main_mat2.inputs: + if(input_ind.is_linked): + main_mat3 = input_ind.links[0].from_node + if(main_mat3.type == 'BSDF_PRINCIPLED'): + main_mat = main_mat3 + group_tree = bpy.data.node_groups.new( type="ShaderNodeTree", name="3DC_Applink") group_tree.outputs.new("NodeSocketColor", "Color") group_tree.outputs.new("NodeSocketColor", "Metallic") @@ -208,11 +225,32 @@ def createnodes(active_mat,texcoat, create_group_node): # Cretes new nodes and l applink_tree = act_material.nodes.new('ShaderNodeGroup') applink_tree.name = '3DC_Applink' applink_tree.node_tree = group_tree - applink_tree.location = -400, 300 + applink_tree.location = -400, -100 act_material = group_tree notegroup = act_material.nodes.new('NodeGroupOutput') notegroup.location = 220, -260 + + if(texcoat['emissive'] != []): + from_output = out_mat.inputs['Surface'].links[0].from_node + if(from_output.type == 'BSDF_PRINCIPLED'): + add_shader = main_material.nodes.new('ShaderNodeAddShader') + emission_shader = main_material.nodes.new('ShaderNodeEmission') + + emission_shader.name = '3DC_Emission' + + add_shader.location = 420, 110 + emission_shader.location = 40, -330 + out_mat.location = 670, 130 + + main_material.links.new(from_output.outputs[0], add_shader.inputs[0]) + main_material.links.new(add_shader.outputs[0], out_mat.inputs[0]) + main_material.links.new(emission_shader.outputs[0], add_shader.inputs[1]) + main_mat = from_output + else: + main_mat = out_mat.inputs['Surface'].links[0].from_node + else: + main_mat = out_mat.inputs['Surface'].links[0].from_node index = 0 for node in coatMat.node_tree.nodes: if (node.type == 'GROUP' and node.name =='3DC_Applink'): @@ -224,170 +262,27 @@ def createnodes(active_mat,texcoat, create_group_node): # Cretes new nodes and l 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 ''' + # READ DATA.JSON FILE + json_address = os.path.dirname(bpy.app.binary_path) + os.sep + '2.80' + os.sep + 'scripts' + os.sep + 'addons' + os.sep + 'io_coat3D' + os.sep + 'data.json' + with open(json_address, encoding='utf-8') as data_file: + data = json.loads(data_file.read()) + if(out_mat.inputs['Surface'].is_linked == True): 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 ''' + CreateTextureLine(data['color'], act_material, main_mat, texcoat, coat3D, notegroup, main_material, applink_tree) 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' - rampnode = act_material.nodes.new('ShaderNodeValToRGB') - rampnode.name = '3DC_ColorRamp' - - act_material.links.new(node.outputs[0], curvenode.inputs[1]) - act_material.links.new(curvenode.outputs[0], rampnode.inputs[0]) - act_material.links.new(rampnode.outputs[0], huenode.inputs[4]) - - - 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 = -1300, 119 - curvenode.location = -1000, 113 - rampnode.location = -670, 115 - 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 ''' + CreateTextureLine(data['metalness'], act_material, main_mat, texcoat, coat3D, notegroup, main_material, applink_tree) 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' - rampnode = act_material.nodes.new('ShaderNodeValToRGB') - rampnode.name = '3DC_ColorRamp' - - act_material.links.new(node.outputs[0], curvenode.inputs[1]) - act_material.links.new(curvenode.outputs[0], rampnode.inputs[0]) - act_material.links.new(rampnode.outputs[0], huenode.inputs[4]) - - 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 = -1300, -276 - curvenode.location = -1000, -245 - rampnode.location = -670, -200 - 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''' + CreateTextureLine(data['rough'], act_material, main_mat, texcoat, coat3D, notegroup, main_material, applink_tree) if(bring_normal == True and texcoat['nmap'] != []): - node = act_material.nodes.new('ShaderNodeTexImage') - normal_node = act_material.nodes.new('ShaderNodeNormalMap') + CreateTextureLine(data['nmap'], act_material, main_mat, texcoat, coat3D, notegroup, main_material, applink_tree) - node.location = -600,-670 - normal_node.location = -250,-350 + if (bring_normal == True and texcoat['emissive'] != []): + CreateTextureLine(data['emissive'], act_material, main_mat, texcoat, coat3D, notegroup, main_material, applink_tree) - 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 ''' @@ -413,25 +308,115 @@ def createnodes(active_mat,texcoat, create_group_node): # Cretes new nodes and l act_material.links.new(curvenode.outputs[0], rampnode.inputs[0]) act_material.links.new(rampnode.outputs[0], huenode.inputs[4]) ''' + act_material.links.new(node.outputs[0], notegroup.inputs[4]) - 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]) - + #if (main_mat.type == 'BSDF_PRINCIPLED'): + #main_material.links.new(applink_tree.outputs[2], 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]) + node.location = -550, 0 + act_material.links.new(node.outputs[0], notegroup.inputs[4]) +def CreateTextureLine(type, act_material, main_mat, texcoat, coat3D, notegroup, main_material, applink_tree): + node = act_material.nodes.new('ShaderNodeTexImage') + if type['name'] == 'nmap': + normal_node = act_material.nodes.new('ShaderNodeNormalMap') + node.location = -650, -500 + normal_node.location = -350, -350 + normal_node.name = '3DC_normalnode' + node.name = '3DC_' + type['name'] + node.label = type['name'] + + for input_index in type['find_input']: + input_color = main_mat.inputs.find(input_index) + if(input_color != -1): + break + + node.image = bpy.data.images.load(texcoat[type['name']][0]) + if(type['colorspace'] == 'noncolor'): + node.color_space = 'NONE' + + if (coat3D.createnodes): + if(type['name'] == 'nmap'): + act_material.links.new(node.outputs[0], normal_node.inputs[1]) + if(input_color != -1): + act_material.links.new(normal_node.outputs[0], main_mat.inputs[input_color]) + + act_material.links.new(normal_node.outputs[0], notegroup.inputs[type['input']]) + if (main_mat.inputs[input_color].name == 'Normal' and input_color != -1): + main_material.links.new(applink_tree.outputs[type['input']], main_mat.inputs[input_color]) + else: + + huenode = createExtraNodes(act_material, node, type) + + act_material.links.new(huenode.outputs[0], notegroup.inputs[type['input']]) + if (main_mat.type != 'MIX_SHADER' and input_color != -1): + main_material.links.new(applink_tree.outputs[type['input']], main_mat.inputs[input_color]) + else: + location = main_mat.location + #applink_tree.location = main_mat.location[0], main_mat.location[1] + 200 + + if(type['name'] == 'emissive'): + for material in main_material.nodes: + if(material.name == '3DC_Emission'): + main_material.links.new(applink_tree.outputs[type['input']], material.inputs[0]) + break + + + else: + node.location = type['node_location'][0], type['node_location'][1] + act_material.links.new(node.outputs[0], notegroup.inputs[type['input']]) + if (input_color != -1): + main_material.links.new(applink_tree.outputs[type['input']], main_mat.inputs[input_color]) + + +def createExtraNodes(act_material, node, type): + + curvenode = act_material.nodes.new('ShaderNodeRGBCurve') + curvenode.name = '3DC_RGBCurve' + huenode = act_material.nodes.new('ShaderNodeHueSaturation') + huenode.name = '3DC_HueSaturation' + print('tieto:', type) + print('ttoto: ', type['rampnode']) + if(type['rampnode'] == 'yes'): + rampnode = act_material.nodes.new('ShaderNodeValToRGB') + rampnode.name = '3DC_ColorRamp' + + if (type['rampnode'] == 'yes'): + act_material.links.new(node.outputs[0], curvenode.inputs[1]) + act_material.links.new(curvenode.outputs[0], rampnode.inputs[0]) + act_material.links.new(rampnode.outputs[0], huenode.inputs[4]) + else: + act_material.links.new(node.outputs[0], curvenode.inputs[1]) + act_material.links.new(curvenode.outputs[0], huenode.inputs[4]) + + if type['name'] == 'metalness': + node.location = -1300, 119 + curvenode.location = -1000, 113 + rampnode.location = -670, 115 + huenode.location = -345, 118 + + elif type['name'] == 'rough': + node.location = -1300, -276 + curvenode.location = -1000, -245 + rampnode.location = -670, -200 + huenode.location = -340, -100 + + elif type['name'] == 'color': + node.location = -990, 530 + curvenode.location = -660, 480 + huenode.location = -337, 335 + + elif type['name'] == 'emissive': + node.location = -1200, -900 + curvenode.location = -900, -900 + huenode.location = -340, -700 + + return huenode def matlab(objekti,mat_list,texturelist,is_new): |