From cd598b76e5fc943b4cfb4754a89cfdf79c73002f Mon Sep 17 00:00:00 2001
From: Kalle-Samuli Riihikoski <haikalle@gmail.com>
Date: Mon, 4 Mar 2019 16:42:43 +0200
Subject: [PATCH] Adding feature for multi-uvs

---
 io_coat3D/__init__.py | 47 ++++++++++++++----------
 io_coat3D/data.json   | 34 ++++++++++++-----
 io_coat3D/tex.py      | 85 ++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 131 insertions(+), 35 deletions(-)

diff --git a/io_coat3D/__init__.py b/io_coat3D/__init__.py
index a9896b6d3..315de8935 100644
--- a/io_coat3D/__init__.py
+++ b/io_coat3D/__init__.py
@@ -29,7 +29,6 @@ bl_info = {
     "category": "3D View",
 }
 
-
 if "bpy" in locals():
     import importlib
     importlib.reload(coat)
@@ -55,7 +54,6 @@ from bpy.props import (
         PointerProperty,
         )
 
-
 bpy.coat3D = dict()
 bpy.coat3D['active_coat'] = ''
 bpy.coat3D['status'] = 0
@@ -248,20 +246,31 @@ def updatemesh(objekti, proxy):
     proxy.select_set(True)
     objekti.select_set(True)
 
-    for poly in objekti.data.polygons:
-        for indi in poly.loop_indices:
-            objekti.data.uv_layers[0].data[indi].uv[0] = proxy.data.uv_layers[0].data[indi].uv[0]
-            objekti.data.uv_layers[0].data[indi].uv[1] = proxy.data.uv_layers[0].data[indi].uv[1]
-
+    Create_uv = True
+    if(len(proxy.data.uv_layers) > 0):
+        for proxy_layer in proxy.data.uv_layers:
+            for objekti_layer in objekti.data.uv_layers:
+                uv_new_name = '3DC_' + proxy_layer.name
+                if(objekti_layer.name == uv_new_name):
+                    for poly in objekti.data.polygons:
+                        for indi in poly.loop_indices:
+                            objekti_layer.data[indi].uv[0] = proxy_layer.data[indi].uv[0]
+                            objekti_layer.data[indi].uv[1] = proxy_layer.data[indi].uv[1]
+                    Create_uv = False
+                    break
+            if(Create_uv):
+                name = '3DC_' + proxy_layer.name
+                objekti.data.uv_layers.new(name=name)
+                for poly in objekti.data.polygons:
+                    for indi in poly.loop_indices:
+                        objekti.data.uv_layers[-1].data[indi].uv[0] = proxy_layer.data[indi].uv[0]
+                        objekti.data.uv_layers[-1].data[indi].uv[1] = proxy_layer.data[indi].uv[1]
 
     # Mesh Copy
 
     for ind, v in enumerate(objekti.data.vertices):
         v.co = proxy.data.vertices[ind].co
 
-
-
-
     '''
     proxy.select_set(True)
     obj_data = objekti.data.id_data
@@ -362,8 +371,6 @@ class SCENE_OT_export(bpy.types.Operator):
 
     def invoke(self, context, event):
 
-
-
         for mesh in bpy.data.meshes:
             if (mesh.users == 0 and mesh.coat3D.name == '3DC'):
                 bpy.data.meshes.remove(mesh)
@@ -379,11 +386,18 @@ class SCENE_OT_export(bpy.types.Operator):
             return {'FINISHED'}
         else:
             for objec in bpy.context.selected_objects:
+                delete_uvmaps = []
                 if objec.type == 'MESH':
+                    for uv_layer in objec.data.uv_layers:
+                        if(uv_layer.name.startswith('3DC_')):
+                            delete_uvmaps.append(uv_layer.name)
+                    for uv_name in delete_uvmaps:
+                        objec.data.uv_layers.remove(objec.data.uv_layers[uv_name])
                     export_ok = True
             if (export_ok == False):
                 return {'FINISHED'}
 
+
         activeobj = bpy.context.active_object.name
         checkname = ''
         coa = bpy.context.active_object.coat3D
@@ -395,7 +409,6 @@ class SCENE_OT_export(bpy.types.Operator):
             coat3D.exchange_found = False
             return {'FINISHED'}
 
-
         folder_objects = set_working_folders()
         folder_size(folder_objects)
 
@@ -455,8 +468,6 @@ class SCENE_OT_export(bpy.types.Operator):
                 objekti.data.name = nimi2
                 objekti.name = nimi2
 
-
-
             else:
                 objekti.name = name_boxs[0]
                 objekti.data.name = name_boxs[0]
@@ -498,7 +509,6 @@ class SCENE_OT_export(bpy.types.Operator):
                             mod_mat_list[objekti.name].append([material_index, temp_mat])
                         material_index = material_index + 1
 
-
                 bake_list = []
                 if(coat3D.bake_diffuse):
                     bake_list.append(['DIFFUSE', '$LOADTEX'])
@@ -528,7 +538,6 @@ class SCENE_OT_export(bpy.types.Operator):
                 elif (coat3D.bake_resolution == 'res_8192'):
                     res_size = 8192
 
-
                 if(len(bake_list) > 0):
                     index_bake_tex = 0
                     while(index_bake_tex < len(bake_list)):
@@ -913,7 +922,6 @@ class SCENE_OT_import(bpy.types.Operator):
                     bpy.ops.import_scene.fbx(filepath=coat3D.bring_retopo_path, global_scale=1, axis_forward='X', use_custom_normals=False)
                     os.remove(coat3D.bring_retopo_path)
 
-
         else:
 
             '''
@@ -961,7 +969,6 @@ class SCENE_OT_import(bpy.types.Operator):
                 bpy.data.objects[c_index].material_slots[0].material = bpy.data.materials[diff_mat[laskuri]]
                 laskuri += 1
 
-
             bpy.ops.object.select_all(action='DESELECT')
             for new_obj in bpy.context.collection.objects:
 
@@ -1036,7 +1043,7 @@ class SCENE_PT_Main(bpy.types.Panel):
             coa = bpy.context.active_object.coat3D
         if(coat['status'] == 0):
             row = layout.row()
-            row.label(text="Applink didn't find your 3d-Coat/Excahnge folder.")
+            row.label(text="Applink didn't find your 3d-Coat/Exchange folder.")
             row = layout.row()
             row.label("Please select it before using Applink.")
             row = layout.row()
diff --git a/io_coat3D/data.json b/io_coat3D/data.json
index 29d34356a..0e3b846f5 100644
--- a/io_coat3D/data.json
+++ b/io_coat3D/data.json
@@ -7,8 +7,10 @@
     "displacement": "no",
     "find_input": [ "Color", "Base Color" ],
     "input": 0,
-    "rampnode":  "no",
-    "node_location":  [ -400, 400 ]
+    "rampnode": "no",
+    "huenode": "yes",
+    "node_location": [ -400, 400 ],
+    "node_color": [0.535, 0.608, 0.306]
   },
 
   "metalness": {
@@ -19,7 +21,9 @@
     "find_input": [ "Metallic" ],
     "input": 1,
     "rampnode": "yes",
-    "node_location": [ -830, 160 ]
+    "huenode": "no",
+    "node_location": [ -830, 160 ],
+    "node_color": [ 0.027, 0.324, 0.908 ]
   },
 
   "rough": {
@@ -30,7 +34,9 @@
     "find_input": [ "Roughness" ],
     "input": 2,
     "rampnode": "yes",
-    "node_location": [ -550, 0 ]
+    "huenode": "no",
+    "node_location": [ -550, 0 ],
+    "node_color": [ 0.608, 0.254, 0.000 ]
   },
 
   "nmap": {
@@ -41,8 +47,10 @@
     "find_input": [ "Normal" ],
     "input": 3,
     "rampnode": "no",
+    "huenode": "no",
     "node_location": [ -650, -500 ],
-    "normal_node_location": [ -350, -350 ]
+    "normal_node_location": [ -350, -350 ],
+    "node_color": [ 0.417, 0.363, 0.608 ]
   },
 
   "displacement": {
@@ -51,7 +59,9 @@
     "normal": "no",
     "displacement": "yes",
     "rampnode": "yes",
-    "input": 5
+    "huenode": "no",
+    "input": 5,
+    "node_color": [ 0.608, 0.591, 0.498 ]
   },
 
   "emissive": {
@@ -61,7 +71,9 @@
     "displacement": "no",
     "find_input": [ "Emissive" ],
     "rampnode": "no",
-    "input": 4
+    "huenode": "yes",
+    "input": 4,
+    "node_color": [ 0.0, 0.0, 0.0 ]
   },
 
   "emissive_power": {
@@ -69,7 +81,9 @@
     "colorspace": "color",
     "normal": "no",
     "displacement": "no",
-    "rampnode": "no"
+    "rampnode": "no",
+    "huenode": "no",
+    "node_color": [ 0.0, 0.0, 0.0 ]
   },
 
   "ao": {
@@ -77,7 +91,9 @@
     "colorspace": "color",
     "normal": "no",
     "displacement": "no",
-    "rampnode": "yes"
+    "rampnode": "yes",
+    "huenode": "no",
+    "node_color": [ 0.1, 0.5, 0.1 ]
   }
 
 }
diff --git a/io_coat3D/tex.py b/io_coat3D/tex.py
index 73293f51f..822f91d42 100644
--- a/io_coat3D/tex.py
+++ b/io_coat3D/tex.py
@@ -65,9 +65,11 @@ def UVTiling(objekti, texturelist):
         name = texture_info[0]
         final_name = name[1:]
         tiling_number = int(final_name)
+
         for list_tiles in uvtiles_index:
             if(list_tiles[1] >= (tiling_number - 1) and list_tiles[1] <= tiling_number ):
                 texture_info[0] = objekti.material_slots[list_tiles[0]].material.name
+                break
 
     return texturelist
 
@@ -92,9 +94,38 @@ def updatetextures(objekti): # Update 3DC textures
                 elif (node.name == '3DC_AO'):
                     node.image.reload()
 
+    for index_node_group in bpy.data.node_groups:
+
+        for node in index_node_group.nodes:
+            if (node.type == 'TEX_IMAGE'):
+                if (node.name == '3DC_color'):
+                    node.image.reload()
+                elif (node.name == '3DC_metalness'):
+                    node.image.reload()
+                elif (node.name == '3DC_rough'):
+                    node.image.reload()
+                elif (node.name == '3DC_nmap'):
+                    node.image.reload()
+                elif (node.name == '3DC_displacement'):
+                    node.image.reload()
+                elif (node.name == '3DC_emissive'):
+                    node.image.reload()
+                elif (node.name == '3DC_AO'):
+                    node.image.reload()
+
 
 def readtexturefolder(objekti, mat_list, texturelist, is_new): #read textures from texture file
 
+    # Let's check are we UVSet or MATERIAL mode
+
+    uv_name = texturelist[0][0]
+    Main_mode = ''
+    for uv_set in objekti.data.uv_layers:
+        if(uv_name == uv_set.name):
+            Main_mode = uv_set.name
+            break
+
+
     create_nodes = False
     if texturelist[0][0].startswith('100'):
         print('This is UVTiling')
@@ -183,7 +214,6 @@ def createnodes(active_mat,texcoat, create_group_node): # Cretes new nodes and l
         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
 
@@ -295,15 +325,29 @@ def createnodes(active_mat,texcoat, create_group_node): # Cretes new nodes and l
 def CreateTextureLine(type, act_material, main_mat, texcoat, coat3D, notegroup, main_material, applink_tree, out_mat, coatMat):
 
     node = act_material.nodes.new('ShaderNodeTexImage')
+    uv_node = act_material.nodes.new('ShaderNodeUVMap')
+    uv_node.uv_map = '3DC_' + coatMat.name
+    act_material.links.new(uv_node.outputs[0], node.inputs[0])
+
+    node.use_custom_color = True
+    uv_node.use_custom_color = True
+
+    node.color = (type['node_color'][0],type['node_color'][1],type['node_color'][2])
+    uv_node.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
 
     if type['name'] == 'nmap':
         normal_node = act_material.nodes.new('ShaderNodeNormalMap')
-        node.location = -650, -500
+        normal_node.use_custom_color = True
+        normal_node.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
+
+        node.location = -671, -510
+        uv_node.location = -750, -600
         normal_node.location = -350, -350
         normal_node.name = '3DC_normalnode'
 
     elif type['name'] == 'displacement':
         disp_node = main_material.nodes.new('ShaderNodeDisplacement')
+
         node.location = -630, -1160
         disp_node.location = 90, -460
         disp_node.inputs[2].default_value = 0.1
@@ -337,6 +381,8 @@ def CreateTextureLine(type, act_material, main_mat, texcoat, coat3D, notegroup,
 
             rampnode = act_material.nodes.new('ShaderNodeValToRGB')
             rampnode.name = '3DC_ColorRamp'
+            rampnode.use_custom_color = True
+            rampnode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
             rampnode.location = -270, -956
 
             act_material.links.new(node.outputs[0], rampnode.inputs[0])
@@ -363,9 +409,14 @@ def CreateTextureLine(type, act_material, main_mat, texcoat, coat3D, notegroup,
                         main_material.links.new(applink_tree.outputs[type['input']], material.inputs[0])
                         break
 
+        uv_node.location = node.location
+        uv_node.location[0] -= 300
+        uv_node.location[1] -= 200
 
     else:
         node.location = type['node_location'][0], type['node_location'][1]
+        uv_node.location = node.location
+        uv_node.location[0] -= 300
         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])
@@ -375,19 +426,41 @@ 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'
+    curvenode.use_custom_color = True
+    curvenode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
+
+    if(type['huenode'] == 'yes'):
+        huenode = act_material.nodes.new('ShaderNodeHueSaturation')
+        huenode.name = '3DC_HueSaturation'
+        huenode.use_custom_color = True
+        huenode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
+    else:
+        huenode = act_material.nodes.new('ShaderNodeMath')
+        huenode.name = '3DC_HueSaturation'
+        huenode.operation = 'MULTIPLY'
+        huenode.use_custom_color = True
+        huenode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
+
+
     if(type['rampnode'] == 'yes'):
         rampnode = act_material.nodes.new('ShaderNodeValToRGB')
         rampnode.name = '3DC_ColorRamp'
+        rampnode.use_custom_color = True
+        rampnode.color = (type['node_color'][0], type['node_color'][1], type['node_color'][2])
 
     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])
+        if(type['huenode'] == 'yes'):
+            act_material.links.new(rampnode.outputs[0], huenode.inputs[4])
+        else:
+            act_material.links.new(rampnode.outputs[0], huenode.inputs[0])
     else:
         act_material.links.new(node.outputs[0], curvenode.inputs[1])
-        act_material.links.new(curvenode.outputs[0], huenode.inputs[4])
+        if (type['huenode'] == 'yes'):
+            act_material.links.new(curvenode.outputs[0], huenode.inputs[4])
+        else:
+            act_material.links.new(curvenode.outputs[0], huenode.inputs[0])
 
     if type['name'] == 'metalness':
         node.location = -1300, 119
-- 
GitLab