diff --git a/io_coat3D/__init__.py b/io_coat3D/__init__.py index 826a5fa4bae52d381445b90e9b46585c55c01336..e55daa67186be7e52948fa0644acfd5c93849ccb 100644 --- a/io_coat3D/__init__.py +++ b/io_coat3D/__init__.py @@ -59,6 +59,14 @@ bpy.coat3D = dict() bpy.coat3D['active_coat'] = '' bpy.coat3D['status'] = 0 +def update_exe_path(): + if (bpy.context.scene.coat3D.coat3D_exe != ''): + importfile = bpy.context.scene.coat3D.exchangedir + importfile += ('%scoat3D_exe.txt' % (os.sep)) + file = open(importfile, "w") + file.write("%s" % (bpy.context.scene.coat3D.coat3D_exe)) + file.close() + def folder_size(path): tosi = True @@ -79,9 +87,12 @@ def set_exchange_folder(): Blender_export = "" if(platform == 'win32'): - exchange = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + '3D-CoatV4' + os.sep +'Exchange' + exchange = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + '3D-CoatV48' + os.sep +'Exchange' if not(os.path.isdir(exchange)): - exchange = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + '3D-CoatV3' + os.sep +'Exchange' + exchange = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + '3D-CoatV4' + os.sep +'Exchange' + if not (os.path.isdir(exchange)): + exchange = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + '3D-CoatV3' + os.sep + 'Exchange' + else: exchange = os.path.expanduser("~") + os.sep + '3D-CoatV4' + os.sep + 'Exchange' if not(os.path.isdir(exchange)): @@ -209,7 +220,7 @@ def updatemesh(objekti, proxy): index = 0 for uv_layer in objekti.data.uv_layers: if (uv_layer != objekti.data.uv_layers[0]): - proxy.data.uv_layers.new(uv_layer.name) + proxy.data.uv_layers.new(name=uv_layer.name) proxy.data.uv_layers.active_index = index objekti.data.uv_layers.active_index = index bpy.ops.object.join_uvs() @@ -263,26 +274,30 @@ class SCENE_OT_opencoat(bpy.types.Operator): bl_options = {'UNDO'} def invoke(self, context, event): + + update_exe_path() + + exefile = bpy.context.scene.coat3D.exchangedir + exefile += ('%scoat3D_exe.txt' % (os.sep)) + exe_path = '' + if (os.path.isfile(exefile)): + + ex_pathh = open(exefile) + for line in ex_pathh: + exe_path = line + break + ex_pathh.close() + coat3D = bpy.context.selected_objects[0].coat3D.applink_3b_path platform = os.sys.platform prog_path = os.environ['PROGRAMFILES'] if (platform == 'win32'): - index = 0 - for file in os.listdir(prog_path): - if index == 0: - if file.startswith('3D-Coat-V4'): - modi = os.path.getmtime(prog_path + os.sep + file) - active_3dcoat = prog_path + os.sep + file - index += 1 - else: - if file.startswith('3D-Coat-V4'): - if(os.path.getmtime(prog_path + os.sep + file) > modi): - modi = os.path.getmtime(prog_path + os.sep + file) - active_3dcoat = prog_path + os.sep + file - if running() == False: + active_3dcoat = exe_path - os.popen('"' + active_3dcoat + os.sep + '3D-CoatDX64C.exe' '" ' + coat3D) + if running() == False: + print('tulele tanne') + os.popen('"' + active_3dcoat + '" ' + coat3D) else: importfile = bpy.context.scene.coat3D.exchangedir importfile += ('%simport.txt' % (os.sep)) @@ -317,6 +332,8 @@ class SCENE_OT_export(bpy.types.Operator): def invoke(self, context, event): + update_exe_path() + for mesh in bpy.data.meshes: if (mesh.users == 0 and mesh.coat3D.name == '3DC'): bpy.data.meshes.remove(mesh) @@ -456,6 +473,13 @@ class SCENE_OT_import(bpy.types.Operator): bl_options = {'UNDO'} def invoke(self, context, event): + + update_exe_path() + + for node_group in bpy.data.node_groups: + if(node_group.users == 0): + bpy.data.node_groups.remove(node_group) + for mesh in bpy.data.meshes: if(mesh.users == 0 and mesh.coat3D.name == '3DC'): bpy.data.meshes.remove(mesh) @@ -563,6 +587,9 @@ class SCENE_OT_import(bpy.types.Operator): objekti = bpy.data.objects[oname] print('BASE_OBJECT:', objekti,objekti.coat3D.applink_address) if(objekti.coat3D.applink_mesh == True): + exportfile = coat3D.exchangedir + path3b_n = coat3D.exchangedir + path3b_n += ('%slast_saved_3b_file.txt' % (os.sep)) print('totta vai tarua:',objekti.coat3D.applink_mesh) if(objekti.coat3D.import_mesh and coat3D.importmesh == True): print('ONAME:', oname) @@ -885,7 +912,7 @@ class SCENE_PT_Settings_Folders(ObjectButtonsPanel, bpy.types.Panel): def draw(self, context): layout = self.layout - layout.use_property_split = True + layout.use_property_split = False coat3D = bpy.context.scene.coat3D rd = context.scene.render @@ -1044,7 +1071,7 @@ class SceneCoat3D(PropertyGroup): ) coat3D_exe: StringProperty( name="FilePath", - subtype="DIR_PATH", + subtype="FILE_PATH", ) cursor_loc: FloatVectorProperty( name="Cursor_loc", @@ -1164,7 +1191,7 @@ class SceneCoat3D(PropertyGroup): importmesh: BoolProperty( name="Mesh", description="Import Mesh", - default=True + default=False ) # copy location diff --git a/io_coat3D/tex.py b/io_coat3D/tex.py index 6b936f0d9f3d8a0f74f2fadfe1bfbb22c3e51053..5e7db59e92f316c5b512c877bb4518cb07f86237 100644 --- a/io_coat3D/tex.py +++ b/io_coat3D/tex.py @@ -42,31 +42,50 @@ def RemoveFbxNodes(objekti): Node_Tree.links.new(Prin_mat.outputs[0], output.inputs[0]) -def readtexturefolder(mat_list,texturelist,is_new): #read textures from texture file - - texcoat = {} - texcoat['color'] = [] - texcoat['metalness'] = [] - texcoat['rough'] = [] - texcoat['nmap'] = [] - texcoat['disp'] = [] - texcoat['emissive'] = [] - - - for texture_info in texturelist: - if texture_info[0] == mat_list[0].name: - if texture_info[2] == 'color' or texture_info[2] == 'diffuse': - texcoat['color'].append(texture_info[3]) - if texture_info[2] == 'metalness' or texture_info[2] == 'specular' or texture_info[2] == 'reflection': - texcoat['metalness'].append(texture_info[3]) - if texture_info[2] == 'rough' or texture_info[2] == 'roughness': - texcoat['rough'].append(texture_info[3]) - if texture_info[2] == 'nmap' or texture_info[2] == 'normalmap' or texture_info[2] == 'normal_map': - texcoat['nmap'].append(texture_info[3]) - if texture_info[2] == 'emissive': - texcoat['emissive'].append(texture_info[3]) - - createnodes(mat_list, texcoat) +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['metalness'] = [] + texcoat['rough'] = [] + texcoat['nmap'] = [] + texcoat['disp'] = [] + texcoat['emissive'] = [] + + + 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(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 = [] @@ -75,14 +94,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(mat_list,texcoat): #luo nodes palikat ja linkittaa tekstuurit niihin +def createnodes(active_mat,texcoat): #luo nodes palikat ja linkittaa tekstuurit niihin bring_color = True #naiden tarkoitus on tsekata onko tarvetta luoda uusi node vai riittaako paivitys bring_metalness = True bring_roughness = True bring_normal = True bring_disp = True coat3D = bpy.context.scene.coat3D - coatMat = mat_list[0] + coatMat = active_mat.material if(coatMat.use_nodes == False): coatMat.use_nodes = True @@ -91,9 +110,9 @@ def createnodes(mat_list,texcoat): #luo nodes palikat ja linkittaa tekstuurit ni applink_group_node = False #ensimmaiseksi kaydaan kaikki image nodet lapi ja tarkistetaan onko nimi 3DC alkunen jos on niin reload - for node in mat_list[0].node_tree.nodes: + for node in coatMat.node_tree.nodes: if (node.type == 'OUTPUT_MATERIAL'): - main_mat = node + out_mat = node break for node in act_material.nodes: @@ -133,7 +152,7 @@ def createnodes(mat_list,texcoat): #luo nodes palikat ja linkittaa tekstuurit ni notegroup = act_material.nodes.new('NodeGroupOutput') else: index = 0 - for node in mat_list[0].node_tree.nodes: + 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'): @@ -145,12 +164,12 @@ def createnodes(mat_list,texcoat): #luo nodes palikat ja linkittaa tekstuurit ni - if(main_mat.inputs['Surface'].is_linked == True): - glue_mat = main_mat.inputs['Surface'].links[0].from_node - if(glue_mat.inputs.find('Base Color') == -1): - input_color = glue_mat.inputs.find('Color') + 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 = glue_mat.inputs.find('Base Color') + input_color = main_mat.inputs.find('Base Color') #Color if(bring_color == True and texcoat['color'] != []): @@ -175,7 +194,7 @@ def createnodes(mat_list,texcoat): #luo nodes palikat ja linkittaa tekstuurit ni curvenode.name = '3DC_RGBCurve' huenode = act_material.nodes.new('ShaderNodeHueSaturation') huenode.name = '3DC_HueSaturation' - act_material.links.new(huenode.outputs[0], glue_mat.inputs[input_color]) + act_material.links.new(huenode.outputs[0], main_mat.inputs[input_color]) act_material.links.new(curvenode.outputs[0], huenode.inputs[4]) act_material.links.new(node.outputs[0], curvenode.inputs[1]) node.location = -990, 530 @@ -185,23 +204,23 @@ def createnodes(mat_list,texcoat): #luo nodes palikat ja linkittaa tekstuurit ni if(coat3D.creategroup): act_material.links.new(huenode.outputs[0], notegroup.inputs[len(notegroup.inputs)-1]) group_tree.outputs[len(group_tree.outputs)-1].name = 'Color' - main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], glue_mat.inputs[input_color]) + main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], main_mat.inputs[input_color]) 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], glue_mat.inputs[input_color]) + 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], glue_mat.inputs[input_color]) + 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' - input_color = glue_mat.inputs.find('Metallic') + input_color = main_mat.inputs.find('Metallic') if(texcoat['metalness']): node.image = bpy.data.images.load(texcoat['metalness'][0]) node.color_space = 'NONE' @@ -210,7 +229,7 @@ def createnodes(mat_list,texcoat): #luo nodes palikat ja linkittaa tekstuurit ni curvenode.name = '3DC_RGBCurve' huenode = act_material.nodes.new('ShaderNodeHueSaturation') huenode.name = '3DC_HueSaturation' - act_material.links.new(huenode.outputs[0], glue_mat.inputs[input_color]) + act_material.links.new(huenode.outputs[0], main_mat.inputs[input_color]) act_material.links.new(curvenode.outputs[0], huenode.inputs[4]) act_material.links.new(node.outputs[0], curvenode.inputs[1]) node.location = -994, 119 @@ -219,53 +238,77 @@ def createnodes(mat_list,texcoat): #luo nodes palikat ja linkittaa tekstuurit ni if (coat3D.creategroup): act_material.links.new(huenode.outputs[0], notegroup.inputs[len(notegroup.inputs)-1]) group_tree.outputs[len(group_tree.outputs) - 1].name = 'Metalness' - main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], glue_mat.inputs[input_color]) + if (input_color != -1): + main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], main_mat.inputs[input_color]) 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], glue_mat.inputs[input_color]) + 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], glue_mat.inputs[input_color]) + 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' - input_color = glue_mat.inputs.find('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(huenode.outputs[0], glue_mat.inputs[input_color]) - act_material.links.new(curvenode.outputs[0], huenode.inputs[4]) - act_material.links.new(node.outputs[0], curvenode.inputs[1]) + + if(main_mat.type == 'EMISSION'): + + input_color = main_mat.inputs.find('Strength') + + rgbTObw = act_material.nodes.new('ShaderNodeRGBToBW') + rgbTObw.name = '3DC_RGBToBW' + mathnode = main_material.nodes.new('ShaderNodeMath') + mathnode.name = '3DC_Math' + mathnode.operation = 'MULTIPLY' + mathnode.label = 'Emission multiply' node.location = -1000, -276 - curvenode.location = -670, -245 - huenode.location = -340, -100 - if (coat3D.creategroup): - act_material.links.new(huenode.outputs[0], notegroup.inputs[len(notegroup.inputs)-1]) - group_tree.outputs[len(group_tree.outputs) - 1].name = 'Roughness' - main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], glue_mat.inputs[input_color]) + rgbTObw.location = -670, -245 + mathnode.location = -300,0 + act_material.links.new(node.outputs[0], rgbTObw.inputs[0]) + if(coat3D.creategroup): + act_material.links.new(rgbTObw.outputs[0], notegroup.inputs[len(notegroup.inputs) - 1]) + if(coat3D.creategroup): + if (input_color != -1): + main_material.links.new(applink_tree.outputs[len(applink_tree.outputs) - 1], mathnode.inputs[0]) + #main_material.links.new(mathnode.outputs[0], main_mat.inputs[1]) 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], glue_mat.inputs[input_color]) + 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(huenode.outputs[0], main_mat.inputs[input_color]) + act_material.links.new(curvenode.outputs[0], huenode.inputs[4]) + act_material.links.new(node.outputs[0], curvenode.inputs[1]) + node.location = -1000, -276 + curvenode.location = -670, -245 + huenode.location = -340, -100 + if (coat3D.creategroup): + act_material.links.new(huenode.outputs[0], notegroup.inputs[len(notegroup.inputs)-1]) + group_tree.outputs[len(group_tree.outputs) - 1].name = 'Roughness' + 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], glue_mat.inputs[input_color]) + 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') @@ -277,12 +320,13 @@ def createnodes(mat_list,texcoat): #luo nodes palikat ja linkittaa tekstuurit ni if(texcoat['nmap']): node.image = bpy.data.images.load(texcoat['nmap'][0]) node.color_space = 'NONE' - input_color = glue_mat.inputs.find('Normal') + 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], glue_mat.inputs[input_color]) + 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[len(notegroup.inputs)-1]) - main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], glue_mat.inputs[input_color]) + if(main_mat.inputs[input_color].name == 'Normal'): + main_material.links.new(applink_tree.outputs[len(applink_tree.outputs)-1], main_mat.inputs[input_color]) def matlab(objekti,mat_list,texturelist,is_new): @@ -293,6 +337,6 @@ def matlab(objekti,mat_list,texturelist,is_new): '''Main Loop for Texture Update''' #checkmaterial(mat_list, objekti) - readtexturefolder(mat_list,texturelist,is_new) + readtexturefolder(objekti,mat_list,texturelist,is_new) return('FINISHED')