diff options
author | Kalle-Samuli Riihikoski <haikalle@gmail.com> | 2020-03-29 19:57:23 +0300 |
---|---|---|
committer | Kalle-Samuli Riihikoski <haikalle@gmail.com> | 2020-03-29 19:57:23 +0300 |
commit | bbd5fd92a3d95389d6489d78cc062539a6f1e0e4 (patch) | |
tree | 4012e000c1a8a09012b7f5fc6f8a2431f76fcc3b /io_coat3D/__init__.py | |
parent | af6e605a120ba58b0e5822b060bedffe590fcd2b (diff) |
Better uvmap painting support + redesing of code
Diffstat (limited to 'io_coat3D/__init__.py')
-rw-r--r-- | io_coat3D/__init__.py | 1057 |
1 files changed, 556 insertions, 501 deletions
diff --git a/io_coat3D/__init__.py b/io_coat3D/__init__.py index 7275e158..189f28d1 100644 --- a/io_coat3D/__init__.py +++ b/io_coat3D/__init__.py @@ -19,12 +19,13 @@ bl_info = { "name": "3D-Coat Applink", "author": "Kalle-Samuli Riihikoski (haikalle)", - "version": (5, 0, 00), + "version": (4, 9, 34), "blender": (2, 80, 0), "location": "Scene > 3D-Coat Applink", "description": "Transfer data between 3D-Coat/Blender", "warning": "", "doc_url": "{BLENDER_MANUAL_URL}/addons/import_export/coat3D.html", + "category": "Import-Export", } @@ -33,16 +34,21 @@ if "bpy" in locals(): importlib.reload(tex) else: from . import tex +from bpy.app.handlers import persistent from io_coat3D import tex +#from io_coat3D import texVR + import os import ntpath import re import shutil import pathlib + import time import bpy + import subprocess from bpy.types import PropertyGroup from bpy.props import ( @@ -57,8 +63,66 @@ bpy.coat3D = dict() bpy.coat3D['active_coat'] = '' bpy.coat3D['status'] = 0 -run_background_update = False +initial_settings = True time_interval = 2.0 +global_exchange_folder = '' + + + + +@persistent +def every_3_seconds(): + global global_exchange_folder + global initial_settings + path_ex = '' + + if(initial_settings): + global_exchange_folder = set_exchange_folder() + initial_settings = False + + coat3D = bpy.context.scene.coat3D + + Export_folder = global_exchange_folder + Blender_folder = os.path.join(Export_folder, 'Blender') + + BlenderFolder = Blender_folder + ExportFolder = Export_folder + + Blender_folder += ('%sexport.txt' % (os.sep)) + Export_folder += ('%sexport.txt' % (os.sep)) + + + if os.path.isfile(Export_folder): + + print('BLENDER -> 3DC -> BLENDER WORKFLLOW') + DeleteExtra3DC() + workflow1(ExportFolder) + removeFile(Export_folder) + removeFile(Blender_folder) + + + + elif os.path.isfile(Blender_folder): + + print('3DC -> BLENDER WORKFLLOW') + DeleteExtra3DC() + workflow2(BlenderFolder) + removeFile(Blender_folder) + + + + + return 3.0 + +@persistent +def load_handler(dummy): + bpy.app.timers.register(every_3_seconds) + + +def removeFile(exportfile): + if (os.path.isfile(exportfile)): + os.remove(exportfile) + def folder_size(path): @@ -493,15 +557,7 @@ class SCENE_OT_export(bpy.types.Operator): def invoke(self, context, event): bpy.ops.export_applink.pilgway_3d_coat() - global run_background_update - if run_background_update: - if bpy.app.timers.is_registered(run_import_periodically): - bpy.app.timers.unregister(run_import_periodically) - run_background_update = False - else: - if not bpy.app.timers.is_registered(run_import_periodically): - bpy.app.timers.register(run_import_periodically, persistent=True) - run_background_update = True + return {'FINISHED'} def execute(self, context): @@ -756,575 +812,575 @@ class SCENE_OT_export(bpy.types.Operator): bpy.context.scene.render.engine = active_render return {'FINISHED'} -class SCENE_OT_import(bpy.types.Operator): - bl_idname = "import_applink.pilgway_3d_coat" - bl_label = "import your custom property" - bl_description = "import your custom property" - bl_options = {'UNDO'} - def invoke(self, context, event): - bpy.ops.import_applink.pilgway_3d_coat() - global run_background_update - if run_background_update: - if bpy.app.timers.is_registered(run_import_periodically): - bpy.app.timers.unregister(run_import_periodically) - run_background_update = False +def DeleteExtra3DC(): - else: - if not bpy.app.timers.is_registered(run_import_periodically): - bpy.app.timers.register(run_import_periodically, persistent=True) - run_background_update = True + for node_group in bpy.data.node_groups: + if(node_group.users == 0): + bpy.data.node_groups.remove(node_group) - return {'FINISHED'} + for mesh in bpy.data.meshes: + if(mesh.users == 0 and mesh.coat3D.name == '3DC'): + bpy.data.meshes.remove(mesh) - def execute(self, context): + for material in bpy.data.materials: + img_list = [] + if (material.users == 1 and material.coat3D.name == '3DC'): + if material.use_nodes == True: + for node in material.node_tree.nodes: + if node.type == 'TEX_IMAGE' and node.name.startswith('3DC'): + img_list.append(node.image) + if img_list != []: + for del_img in img_list: + bpy.data.images.remove(del_img) + + bpy.data.materials.remove(material) + + image_del_list = [] + for image in bpy.data.images: + if (image.name.startswith('3DC')): + if image.users == 0: + image_del_list.append(image.name) + + if (image_del_list != []): + for image in image_del_list: + bpy.data.images.remove(bpy.data.images[image]) - global run_background_update - run_background_update = True +def new_ref_function(new_applink_address, nimi): - for node_group in bpy.data.node_groups: - if(node_group.users == 0): - bpy.data.node_groups.remove(node_group) + create_collection = True + for collection in bpy.data.collections: + if collection.name == 'Applink_Objects': + create_collection = False - for mesh in bpy.data.meshes: - if(mesh.users == 0 and mesh.coat3D.name == '3DC'): - bpy.data.meshes.remove(mesh) + if create_collection: + bpy.data.collections.new('Applink_Objects') - for material in bpy.data.materials: - img_list = [] - if (material.users == 1 and material.coat3D.name == '3DC'): - if material.use_nodes == True: - for node in material.node_tree.nodes: - if node.type == 'TEX_IMAGE' and node.name.startswith('3DC'): - img_list.append(node.image) - if img_list != []: - for del_img in img_list: - bpy.data.images.remove(del_img) + coll_items = bpy.context.scene.collection.children.items() - bpy.data.materials.remove(material) + add_applink_collection = True + for coll in coll_items: + if coll[0] == 'Applink_Objects': + add_applink_collection = False - image_del_list = [] - for image in bpy.data.images: - if (image.name.startswith('3DC')): - if image.users == 0: - image_del_list.append(image.name) + if add_applink_collection: + bpy.context.scene.collection.children.link(bpy.data.collections['Applink_Objects']) - if (image_del_list != []): - for image in image_del_list: - bpy.data.images.remove(bpy.data.images[image]) + bpy.context.view_layer.active_layer_collection = bpy.context.view_layer.layer_collection.children['Applink_Objects'] - coat3D = bpy.context.scene.coat3D - coat = bpy.coat3D - coat3D.exchangedir = set_exchange_folder() + old_objects = bpy.data.objects.keys() + object_list = [] - texturelist = make_texture_list(coat3D.exchangedir) - for texturepath in texturelist: - for image in bpy.data.images: - if(image.filepath == texturepath[3] and image.users == 0): - bpy.data.images.remove(image) + bpy.ops.import_scene.fbx(filepath=new_applink_address, global_scale = 0.01,axis_forward='X', axis_up='Y',use_custom_normals=False) + new_objects = bpy.data.objects.keys() + diff_objects = [i for i in new_objects if i not in old_objects] + texturelist = [] - kokeilu = coat3D.exchangedir - Blender_folder = ("%s%sBlender"%(kokeilu,os.sep)) - Blender_export = Blender_folder - path3b_now = coat3D.exchangedir - path3b_now += ('last_saved_3b_file.txt') - Blender_export += ('%sexport.txt'%(os.sep)) - new_applink_address = 'False' - new_object = False - new_ref_object = False - - exportfile3 = coat3D.exchangedir - exportfile3 += ('%sexport.txt' % (os.sep)) - if(os.path.isfile(Blender_export)): - obj_pathh = open(Blender_export) - new_object = True - for line in obj_pathh: - new_applink_address = line - break - obj_pathh.close() + for diff_object in diff_objects: - for scene_objects in bpy.context.collection.all_objects: - if(scene_objects.type == 'MESH'): - if(scene_objects.coat3D.applink_address == new_applink_address): - new_object = False + refmesh = bpy.data.objects[nimi] + copymesh = bpy.data.objects[nimi].copy() + copymesh.data = bpy.data.objects[diff_object].data + copymesh.coat3D.applink_name = bpy.data.objects[diff_object].data.name + copymesh.coat3D.applink_address = refmesh.coat3D.applink_address + ne_name = bpy.data.objects[diff_object].data.name - elif(os.path.isfile(exportfile3)): - obj_pathh = open(exportfile3) + copymesh.coat3D.type = 'ppp' + copymesh.coat3D.retopo = True - for line in obj_pathh: - new_applink_address = line - break - obj_pathh.close() + bpy.data.collections['Applink_Objects'].objects.link(copymesh) - for scene_objects in bpy.context.collection.all_objects: - if(scene_objects.type == 'MESH'): - if(scene_objects.coat3D.applink_address == new_applink_address and scene_objects.coat3D.type == 'ref'): - scene_objects.coat3D.type == '' - new_ref_object = True - nimi = scene_objects.name + bpy.data.objects.remove(bpy.data.objects[diff_object]) + bpy.ops.object.select_all(action='DESELECT') + copymesh.select_set(True) + copymesh.delta_rotation_euler[0] = 1.5708 + copymesh.name = ne_name + normal_node = copymesh.material_slots[0].material.node_tree.nodes['Normal Map'] + copymesh.material_slots[0].material.node_tree.nodes.remove(normal_node) + copymesh.material_slots[0].material.node_tree.nodes['Principled BSDF'].inputs['Metallic'].default_value = 0 + copymesh.material_slots[0].material.node_tree.nodes['Principled BSDF'].inputs['Specular'].default_value = 0.5 - exportfile = coat3D.exchangedir - exportfile += ('%sBlender' % (os.sep)) - exportfile += ('%sexport.txt' % (os.sep)) - if (os.path.isfile(exportfile)): - os.remove(exportfile) + refmesh.coat3D.applink_name = '' + refmesh.coat3D.applink_address = '' + refmesh.coat3D.type = '' - if(new_ref_object): - create_collection = True - for collection in bpy.data.collections: - if collection.name == 'Applink_Objects': - create_collection = False +def blender_3DC_blender(texturelist): - if create_collection: - bpy.data.collections.new('Applink_Objects') + coat3D = bpy.context.scene.coat3D - coll_items = bpy.context.scene.collection.children.items() + old_materials = bpy.data.materials.keys() + old_objects = bpy.data.objects.keys() + cache_base = bpy.data.objects.keys() - add_applink_collection = True - for coll in coll_items: - if coll[0] == 'Applink_Objects': - add_applink_collection = False + object_list = [] + import_list = [] + import_type = [] - if add_applink_collection: - bpy.context.scene.collection.children.link(bpy.data.collections['Applink_Objects']) + for objekti in bpy.data.objects: + if objekti.type == 'MESH': + obj_coat = objekti.coat3D + if(obj_coat.applink_mesh == True): + object_list.append(objekti.name) + if(os.path.isfile(obj_coat.applink_address)): + if (obj_coat.objecttime != str(os.path.getmtime(obj_coat.applink_address))): + obj_coat.dime = objekti.dimensions + obj_coat.import_mesh = True + obj_coat.objecttime = str(os.path.getmtime(obj_coat.applink_address)) + if(obj_coat.applink_address not in import_list): + import_list.append(obj_coat.applink_address) + import_type.append(coat3D.type) - bpy.context.view_layer.active_layer_collection = bpy.context.view_layer.layer_collection.children['Applink_Objects'] + if(import_list or coat3D.importmesh): + for idx, list in enumerate(import_list): - old_objects = bpy.data.objects.keys() - object_list = [] + bpy.ops.import_scene.fbx(filepath=list, global_scale = 0.01,axis_forward='X',use_custom_normals=False) + cache_objects = bpy.data.objects.keys() + cache_objects = [i for i in cache_objects if i not in cache_base] + for cache_object in cache_objects: + bpy.data.objects[cache_object].coat3D.type = import_type[idx] + bpy.data.objects[cache_object].coat3D.applink_address = list + cache_base.append(cache_object) - bpy.ops.import_scene.fbx(filepath=new_applink_address, global_scale = 0.01,axis_forward='X', axis_up='Y',use_custom_normals=False) - new_objects = bpy.data.objects.keys() - diff_objects = [i for i in new_objects if i not in old_objects] - texturelist = [] + bpy.ops.object.select_all(action='DESELECT') + new_materials = bpy.data.materials.keys() + new_objects = bpy.data.objects.keys() - for diff_object in diff_objects: - refmesh = bpy.data.objects[nimi] - copymesh = bpy.data.objects[nimi].copy() + diff_mat = [i for i in new_materials if i not in old_materials] + diff_objects = [i for i in new_objects if i not in old_objects] - copymesh.data = bpy.data.objects[diff_object].data - copymesh.coat3D.applink_name = bpy.data.objects[diff_object].data.name - copymesh.coat3D.applink_address = refmesh.coat3D.applink_address - ne_name = bpy.data.objects[diff_object].data.name + for mark_mesh in diff_objects: + bpy.data.objects[mark_mesh].data.coat3D.name = '3DC' - copymesh.coat3D.type = 'ppp' - copymesh.coat3D.retopo = True + for c_index in diff_mat: + bpy.data.materials.remove(bpy.data.materials[c_index]) - bpy.data.collections['Applink_Objects'].objects.link(copymesh) + '''The main Applink Object Loop''' - bpy.data.objects.remove(bpy.data.objects[diff_object]) - bpy.ops.object.select_all(action='DESELECT') - copymesh.select_set(True) - copymesh.delta_rotation_euler[0] = 1.5708 - copymesh.name = ne_name + for oname in object_list: - normal_node = copymesh.material_slots[0].material.node_tree.nodes['Normal Map'] - copymesh.material_slots[0].material.node_tree.nodes.remove(normal_node) - copymesh.material_slots[0].material.node_tree.nodes['Principled BSDF'].inputs['Metallic'].default_value = 0 - copymesh.material_slots[0].material.node_tree.nodes['Principled BSDF'].inputs['Specular'].default_value = 0.5 + objekti = bpy.data.objects[oname] + if(objekti.coat3D.applink_mesh == True): - refmesh.coat3D.applink_name = '' - refmesh.coat3D.applink_address = '' - refmesh.coat3D.type = '' + path3b_n = coat3D.exchangedir + path3b_n += ('%slast_saved_3b_file.txt' % (os.sep)) - else: - if(new_object == False): - - ''' - #Blender -> 3DC -> Blender workflow - #First check if objects needs to be imported, if imported it will then delete extra mat and objs. - ''' - - old_materials = bpy.data.materials.keys() - old_objects = bpy.data.objects.keys() - cache_base = bpy.data.objects.keys() - - object_list = [] - import_list = [] - import_type = [] - - for objekti in bpy.data.objects: - if objekti.type == 'MESH': - obj_coat = objekti.coat3D - if(obj_coat.applink_mesh == True): - object_list.append(objekti.name) - if(os.path.isfile(obj_coat.applink_address)): - if (obj_coat.objecttime != str(os.path.getmtime(obj_coat.applink_address))): - obj_coat.dime = objekti.dimensions - obj_coat.import_mesh = True - obj_coat.objecttime = str(os.path.getmtime(obj_coat.applink_address)) - if(obj_coat.applink_address not in import_list): - import_list.append(obj_coat.applink_address) - import_type.append(coat3D.type) - - if(import_list or coat3D.importmesh): - for idx, list in enumerate(import_list): - bpy.ops.import_scene.fbx(filepath=list, global_scale = 0.01,axis_forward='X',use_custom_normals=False) - cache_objects = bpy.data.objects.keys() - cache_objects = [i for i in cache_objects if i not in cache_base] - for cache_object in cache_objects: - bpy.data.objects[cache_object].coat3D.type = import_type[idx] - bpy.data.objects[cache_object].coat3D.applink_address = list - cache_base.append(cache_object) + if(objekti.coat3D.import_mesh and coat3D.importmesh == True): - bpy.ops.object.select_all(action='DESELECT') - new_materials = bpy.data.materials.keys() - new_objects = bpy.data.objects.keys() - - - diff_mat = [i for i in new_materials if i not in old_materials] - diff_objects = [i for i in new_objects if i not in old_objects] - - for mark_mesh in diff_objects: - bpy.data.objects[mark_mesh].data.coat3D.name = '3DC' - for c_index in diff_mat: - bpy.data.materials.remove(bpy.data.materials[c_index]) - - '''The main Applink Object Loop''' - - for oname in object_list: - print('ONAME: ', oname) - objekti = bpy.data.objects[oname] - if(objekti.coat3D.applink_mesh == True): - path3b_n = coat3D.exchangedir - path3b_n += ('%slast_saved_3b_file.txt' % (os.sep)) - if(objekti.coat3D.import_mesh and coat3D.importmesh == True): - - objekti.coat3D.import_mesh = False - objekti.select_set(True) - - use_smooth = objekti.data.polygons[0].use_smooth - found_obj = False - - '''Changes objects mesh into proxy mesh''' - if(objekti.coat3D.type != 'ref'): - - for proxy_objects in diff_objects: - if(objekti.coat3D.retopo == False): - if (proxy_objects == objekti.coat3D.applink_name): - obj_proxy = bpy.data.objects[proxy_objects] - obj_proxy.coat3D.delete_proxy_mesh = True - found_obj = True - else: - if (proxy_objects == objekti.coat3D.applink_name + '.001'): - obj_proxy = bpy.data.objects[proxy_objects] - obj_proxy.coat3D.delete_proxy_mesh = True - found_obj = True - - - mat_list = [] - if (objekti.material_slots): - for obj_mat in objekti.material_slots: - mat_list.append(obj_mat.material) - - if(found_obj == True): - exportfile = coat3D.exchangedir - path3b_n = coat3D.exchangedir - path3b_n += ('%slast_saved_3b_file.txt' % (os.sep)) - exportfile += ('%sBlender' % (os.sep)) - exportfile += ('%sexport.txt'%(os.sep)) - if(os.path.isfile(exportfile)): - export_file = open(exportfile) - for line in export_file: - if line.rfind('.3b'): - coat['active_coat'] = line - export_file.close() - os.remove(exportfile) - if(os.path.isfile(path3b_n)): - - mesh_time = os.path.getmtime(objekti.coat3D.applink_address) - b_time = os.path.getmtime(path3b_n) - if (abs(mesh_time - b_time) < 240): - export_file = open(path3b_n) - for line in export_file: - objekti.coat3D.applink_3b_path = line - head, tail = os.path.split(line) - just_3b_name = tail - objekti.coat3D.applink_3b_just_name = just_3b_name - export_file.close() - coat3D.remove_path = True - - bpy.ops.object.select_all(action='DESELECT') - obj_proxy.select_set(True) - - bpy.ops.object.select_all(action='TOGGLE') - - if objekti.coat3D.applink_firsttime == True and objekti.coat3D.type == 'vox': - objekti.select_set(True) - - objekti.rotation_euler[0] = 1.5708 - objekti.rotation_euler[2] = 1.5708 - bpy.ops.object.transforms_to_deltas(mode='ROT') - bpy.ops.object.transforms_to_deltas(mode='SCALE') - objekti.coat3D.applink_firsttime = False - objekti.select_set(False) - - elif objekti.coat3D.applink_firsttime == True: - objekti.scale = (objekti.scale[0]/objekti.coat3D.applink_scale[0],objekti.scale[1]/objekti.coat3D.applink_scale[1],objekti.scale[2]/objekti.coat3D.applink_scale[2]) - #bpy.ops.object.transforms_to_deltas(mode='SCALE') - if(objekti.coat3D.applink_onlyone == False): - objekti.rotation_euler = (0,0,0) - objekti.coat3D.applink_firsttime = False - - if(coat3D.importlevel): - obj_proxy.select = True - obj_proxy.modifiers.new(name='temp',type='MULTIRES') - objekti.select = True - bpy.ops.object.multires_reshape(modifier=multires_name) - bpy.ops.object.select_all(action='TOGGLE') - else: - - bpy.context.view_layer.objects.active = obj_proxy - keep_materials_count = len(obj_proxy.material_slots) - len(objekti.material_slots) - - #delete_materials_from_end(keep_materials_count, obj_proxy) - - for index, material in enumerate(objekti.material_slots): - obj_proxy.material_slots[index-1].material = material.material - updatemesh(objekti,obj_proxy, texturelist) - bpy.context.view_layer.objects.active = objekti - - - - #tärkee että saadaan oikein käännettyä objekt - - objekti.select_set(True) - - if (use_smooth): - for data_mesh in objekti.data.polygons: - data_mesh.use_smooth = True - else: - for data_mesh in objekti.data.polygons: - data_mesh.use_smooth = False - - bpy.ops.object.select_all(action='DESELECT') - - if(coat3D.importmesh and not(os.path.isfile(objekti.coat3D.applink_address))): - coat3D.importmesh = False - - objekti.select_set(True) - if(coat3D.importtextures): - is_new = False - if(objekti.coat3D.retopo == False): - tex.matlab(objekti,mat_list,texturelist,is_new) - objekti.select_set(False) + objekti.coat3D.import_mesh = False + objekti.select_set(True) + + use_smooth = objekti.data.polygons[0].use_smooth + found_obj = False + + '''Changes objects mesh into proxy mesh''' + if(objekti.coat3D.type != 'ref'): + + for proxy_objects in diff_objects: + if(objekti.coat3D.retopo == False): + if (proxy_objects == objekti.coat3D.applink_name): + obj_proxy = bpy.data.objects[proxy_objects] + obj_proxy.coat3D.delete_proxy_mesh = True + found_obj = True else: - mat_list = [] - if (objekti.material_slots): - for obj_mat in objekti.material_slots: - mat_list.append(obj_mat.material) + if (proxy_objects == objekti.coat3D.applink_name + '.001'): + obj_proxy = bpy.data.objects[proxy_objects] + obj_proxy.coat3D.delete_proxy_mesh = True + found_obj = True - if (coat3D.importtextures): - is_new = False - if(objekti.coat3D.retopo == False): - tex.matlab(objekti,mat_list,texturelist, is_new) - objekti.select_set(False) - if(coat3D.remove_path == True): - os.remove(path3b_n) - coat3D.remove_path = False + mat_list = [] + if (objekti.material_slots): + for obj_mat in objekti.material_slots: + mat_list.append(obj_mat.material) + + if(found_obj == True): + exportfile = coat3D.exchangedir + path3b_n = coat3D.exchangedir + path3b_n += ('%slast_saved_3b_file.txt' % (os.sep)) + exportfile += ('%sBlender' % (os.sep)) + exportfile += ('%sexport.txt'%(os.sep)) + if(os.path.isfile(exportfile)): + export_file = open(exportfile) + for line in export_file: + if line.rfind('.3b'): + coat['active_coat'] = line + export_file.close() + os.remove(exportfile) + if(os.path.isfile(path3b_n)): + + mesh_time = os.path.getmtime(objekti.coat3D.applink_address) + b_time = os.path.getmtime(path3b_n) + if (abs(mesh_time - b_time) < 240): + export_file = open(path3b_n) + for line in export_file: + objekti.coat3D.applink_3b_path = line + head, tail = os.path.split(line) + just_3b_name = tail + objekti.coat3D.applink_3b_just_name = just_3b_name + export_file.close() + coat3D.remove_path = True - bpy.ops.object.select_all(action='DESELECT') - if(import_list): - for del_obj in diff_objects: + bpy.ops.object.select_all(action='DESELECT') + obj_proxy.select_set(True) + bpy.ops.object.select_all(action='TOGGLE') - if(bpy.context.collection.all_objects[del_obj].coat3D.type == 'vox' and bpy.context.collection.all_objects[del_obj].coat3D.delete_proxy_mesh == False): - bpy.context.collection.all_objects[del_obj].select_set(True) - objekti = bpy.context.collection.all_objects[del_obj] - #bpy.ops.object.transforms_to_deltas(mode='ROT') - objekti.scale = (0.01, 0.01, 0.01) - bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY') + if objekti.coat3D.applink_firsttime == True and objekti.coat3D.type == 'vox': + objekti.select_set(True) + objekti.scale = (1, 1, 1) + objekti.rotation_euler[0] = 1.5708 + objekti.rotation_euler[2] = 1.5708 + bpy.ops.object.transforms_to_deltas(mode='ROT') + bpy.ops.object.transforms_to_deltas(mode='SCALE') + objekti.coat3D.applink_firsttime = False + objekti.select_set(False) - objekti.data.coat3D.name = '3DC' + elif objekti.coat3D.applink_firsttime == True: + objekti.scale = (objekti.scale[0]/objekti.coat3D.applink_scale[0],objekti.scale[1]/objekti.coat3D.applink_scale[1],objekti.scale[2]/objekti.coat3D.applink_scale[2]) + #bpy.ops.object.transforms_to_deltas(mode='SCALE') + if(objekti.coat3D.applink_onlyone == False): + objekti.rotation_euler = (0,0,0) + objekti.coat3D.applink_firsttime = False - objekti.coat3D.objecttime = str(os.path.getmtime(objekti.coat3D.applink_address)) - objekti.coat3D.applink_name = objekti.name - objekti.coat3D.applink_mesh = True - objekti.coat3D.import_mesh = False + if(coat3D.importlevel): + obj_proxy.select = True + obj_proxy.modifiers.new(name='temp',type='MULTIRES') + objekti.select = True + bpy.ops.object.multires_reshape(modifier=multires_name) + bpy.ops.object.select_all(action='TOGGLE') + else: - bpy.ops.object.transforms_to_deltas(mode='SCALE') - objekti.coat3D.applink_firsttime = False - bpy.context.collection.all_objects[del_obj].select_set(False) + bpy.context.view_layer.objects.active = obj_proxy + keep_materials_count = len(obj_proxy.material_slots) - len(objekti.material_slots) - else: - bpy.context.collection.all_objects[del_obj].select_set(True) - bpy.data.objects.remove(bpy.data.objects[del_obj]) + #delete_materials_from_end(keep_materials_count, obj_proxy) - if (coat3D.bring_retopo or coat3D.bring_retopo_path): - if(os.path.isfile(coat3D.bring_retopo_path)): - 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) + for index, material in enumerate(objekti.material_slots): + obj_proxy.material_slots[index-1].material = material.material + updatemesh(objekti,obj_proxy, texturelist) + bpy.context.view_layer.objects.active = objekti - kokeilu = coat3D.exchangedir[:-9] - Blender_folder2 = ("%s%sExchange" % (kokeilu, os.sep)) - Blender_folder2 += ('%sexport.txt' % (os.sep)) - if (os.path.isfile(Blender_folder2)): - os.remove(Blender_folder2) - else: - ''' - 3DC -> Blender workflow - ''' - bpy.ops.object.select_all(action='DESELECT') - for old_obj in bpy.context.collection.objects: - old_obj.coat3D.applink_old = True - - coat3D = bpy.context.scene.coat3D - Blender_folder = ("%s%sBlender"%(coat3D.exchangedir,os.sep)) - Blender_export = Blender_folder - path3b_now = coat3D.exchangedir + os.sep - path3b_now += ('last_saved_3b_file.txt') - Blender_export += ('%sexport.txt'%(os.sep)) - mat_list = [] - osoite_3b = '' - if (os.path.isfile(path3b_now)): - path3b_fil = open(path3b_now) - for lin in path3b_fil: - osoite_3b = lin - path3b_fil.close() - head, tail = os.path.split(osoite_3b) - just_3b_name = tail - os.remove(path3b_now) + #tärkee että saadaan oikein käännettyä objekt - create_collection = True - for collection in bpy.data.collections: - if collection.name == 'Applink_Objects': - create_collection = False + objekti.select_set(True) - if create_collection: - bpy.data.collections.new('Applink_Objects') + if (use_smooth): + for data_mesh in objekti.data.polygons: + data_mesh.use_smooth = True + else: + for data_mesh in objekti.data.polygons: + data_mesh.use_smooth = False - coll_items = bpy.context.scene.collection.children.items() + bpy.ops.object.select_all(action='DESELECT') - add_applink_collection = True - for coll in coll_items: - if coll[0] == 'Applink_Objects': - add_applink_collection = False + if(coat3D.importmesh and not(os.path.isfile(objekti.coat3D.applink_address))): + coat3D.importmesh = False - if add_applink_collection: - bpy.context.scene.collection.children.link(bpy.data.collections['Applink_Objects']) + objekti.select_set(True) + if(coat3D.importtextures): + is_new = False + if(objekti.coat3D.retopo == False): + tex.matlab(objekti,mat_list,texturelist,is_new) + objekti.select_set(False) + else: + mat_list = [] + if (objekti.material_slots): + for obj_mat in objekti.material_slots: + mat_list.append(obj_mat.material) - bpy.context.view_layer.active_layer_collection = bpy.context.view_layer.layer_collection.children['Applink_Objects'] + if (coat3D.importtextures): + is_new = False + if(objekti.coat3D.retopo == False): + tex.matlab(objekti,mat_list,texturelist, is_new) + objekti.select_set(False) - old_materials = bpy.data.materials.keys() - old_objects = bpy.data.objects.keys() + if(coat3D.remove_path == True): + os.remove(path3b_n) + coat3D.remove_path = False - bpy.ops.import_scene.fbx(filepath=new_applink_address, global_scale = 1, axis_forward='-Z', axis_up='Y') + bpy.ops.object.select_all(action='DESELECT') + if(import_list): - new_materials = bpy.data.materials.keys() - new_objects = bpy.data.objects.keys() + for del_obj in diff_objects: - diff_mat = [i for i in new_materials if i not in old_materials] - diff_objects = [i for i in new_objects if i not in old_objects] + if(bpy.context.collection.all_objects[del_obj].coat3D.type == 'vox' and bpy.context.collection.all_objects[del_obj].coat3D.delete_proxy_mesh == False): + bpy.context.collection.all_objects[del_obj].select_set(True) + objekti = bpy.context.collection.all_objects[del_obj] + #bpy.ops.object.transforms_to_deltas(mode='ROT') + objekti.scale = (1, 1, 1) + bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY') + objekti.data.coat3D.name = '3DC' - for mark_mesh in diff_mat: - bpy.data.materials[mark_mesh].coat3D.name = '3DC' - bpy.data.materials[mark_mesh].use_fake_user = True - laskuri = 0 - index = 0 - for c_index in diff_objects: - bpy.data.objects[c_index].data.coat3D.name = '3DC' - laskuri += 1 + objekti.coat3D.objecttime = str(os.path.getmtime(objekti.coat3D.applink_address)) + objekti.coat3D.applink_name = objekti.name + objekti.coat3D.applink_mesh = True + objekti.coat3D.import_mesh = False #bpy.ops.object.transforms_to_deltas(mode='SCALE') - bpy.ops.object.select_all(action='DESELECT') - for new_obj in bpy.context.collection.objects: - - if(new_obj.coat3D.applink_old == False): - new_obj.select_set(True) - new_obj.coat3D.applink_firsttime = False - new_obj.select_set(False) - new_obj.coat3D.type = 'ppp' - new_obj.coat3D.applink_address = new_applink_address - new_obj.coat3D.applink_mesh = True - new_obj.coat3D.objecttime = str(os.path.getmtime(new_obj.coat3D.applink_address)) - - new_obj.coat3D.applink_name = new_obj.name - index = index + 1 - - bpy.context.view_layer.objects.active = new_obj - - new_obj.coat3D.applink_export = True - - if (os.path.isfile(osoite_3b)): - mesh_time = os.path.getmtime(new_obj.coat3D.applink_address) - b_time = os.path.getmtime(osoite_3b) - if (abs(mesh_time-b_time) < 240): - new_obj.coat3D.applink_3b_path = osoite_3b - new_obj.coat3D.applink_3b_just_name = just_3b_name - - mat_list.append(new_obj.material_slots[0].material) - is_new = True - tex.matlab(new_obj, mat_list, texturelist, is_new) - mat_list.pop() - - for new_obj in bpy.context.collection.objects: - if(new_obj.coat3D.applink_old == False): - new_obj.coat3D.applink_old = True - - kokeilu = coat3D.exchangedir[:-10] - Blender_folder2 = ("%s%sExchange%sBlender" % (kokeilu, os.sep, os.sep)) - Blender_folder2 += ('%sexport.txt' % (os.sep)) - - if (os.path.isfile(Blender_export)): - os.remove(Blender_export) - if (os.path.isfile(Blender_folder2)): - os.remove(Blender_folder2) - for material in bpy.data.materials: - if material.use_nodes == True: - for node in material.node_tree.nodes: - if (node.name).startswith('3DC'): - node.location = node.location - - if(bpy.context.scene.render.engine == 'CYCLES'): # HACK textures are updated in cycles render - bpy.context.scene.render.engine = 'BLENDER_EEVEE' - bpy.context.scene.render.engine = 'CYCLES' - - exportfile = coat3D.exchangedir - exportfile += ('%sexport.txt' % (os.sep)) - if (os.path.isfile(exportfile)): - os.remove(exportfile) + objekti.coat3D.applink_firsttime = False + bpy.context.collection.all_objects[del_obj].select_set(False) - return {'FINISHED'} + else: + bpy.context.collection.all_objects[del_obj].select_set(True) + bpy.data.objects.remove(bpy.data.objects[del_obj]) + + if (coat3D.bring_retopo or coat3D.bring_retopo_path): + if(os.path.isfile(coat3D.bring_retopo_path)): + 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) -def run_import_periodically(): - coat3D = bpy.context.scene.coat3D kokeilu = coat3D.exchangedir[:-9] Blender_folder2 = ("%s%sExchange" % (kokeilu, os.sep)) Blender_folder2 += ('%sexport.txt' % (os.sep)) - global run_background_update + if (os.path.isfile(Blender_folder2)): + os.remove(Blender_folder2) + +def blender_3DC(texturelist, new_applink_address): + + bpy.ops.object.select_all(action='DESELECT') + for old_obj in bpy.context.collection.objects: + old_obj.coat3D.applink_old = True + + coat3D = bpy.context.scene.coat3D + Blender_folder = ("%s%sBlender"%(coat3D.exchangedir,os.sep)) + Blender_export = Blender_folder + path3b_now = coat3D.exchangedir + os.sep + path3b_now += ('last_saved_3b_file.txt') + Blender_export += ('%sexport.txt'%(os.sep)) + mat_list = [] + osoite_3b = '' + if (os.path.isfile(path3b_now)): + path3b_fil = open(path3b_now) + for lin in path3b_fil: + osoite_3b = lin + path3b_fil.close() + head, tail = os.path.split(osoite_3b) + just_3b_name = tail + os.remove(path3b_now) + + create_collection = True + for collection in bpy.data.collections: + if collection.name == 'Applink_Objects': + create_collection = False + + if create_collection: + bpy.data.collections.new('Applink_Objects') + + coll_items = bpy.context.scene.collection.children.items() + + add_applink_collection = True + for coll in coll_items: + if coll[0] == 'Applink_Objects': + add_applink_collection = False + + if add_applink_collection: + bpy.context.scene.collection.children.link(bpy.data.collections['Applink_Objects']) + + bpy.context.view_layer.active_layer_collection = bpy.context.view_layer.layer_collection.children['Applink_Objects'] + + old_materials = bpy.data.materials.keys() + old_objects = bpy.data.objects.keys() + + bpy.ops.import_scene.fbx(filepath=new_applink_address, global_scale = 1, axis_forward='-Z', axis_up='Y') + + new_materials = bpy.data.materials.keys() + new_objects = bpy.data.objects.keys() + + diff_mat = [i for i in new_materials if i not in old_materials] + diff_objects = [i for i in new_objects if i not in old_objects] + + + for mark_mesh in diff_mat: + bpy.data.materials[mark_mesh].coat3D.name = '3DC' + bpy.data.materials[mark_mesh].use_fake_user = True + laskuri = 0 + index = 0 + + facture_object = False + + for c_index in diff_objects: + bpy.data.objects[c_index].data.coat3D.name = '3DC' + laskuri += 1 + if(laskuri == 2 and c_index == ('vt_' + diff_objects[0])): + facture_object = True + print('Facture object founded!!') + + #bpy.ops.object.transforms_to_deltas(mode='SCALE') + bpy.ops.object.select_all(action='DESELECT') + for new_obj in bpy.context.collection.objects: + + if(new_obj.coat3D.applink_old == False): + new_obj.select_set(True) + new_obj.coat3D.applink_firsttime = False + new_obj.select_set(False) + new_obj.coat3D.type = 'ppp' + new_obj.coat3D.applink_address = new_applink_address + new_obj.coat3D.applink_mesh = True + new_obj.coat3D.objecttime = str(os.path.getmtime(new_obj.coat3D.applink_address)) + + new_obj.coat3D.applink_name = new_obj.name + index = index + 1 + + bpy.context.view_layer.objects.active = new_obj + + new_obj.coat3D.applink_export = True + + if (os.path.isfile(osoite_3b)): + mesh_time = os.path.getmtime(new_obj.coat3D.applink_address) + b_time = os.path.getmtime(osoite_3b) + if (abs(mesh_time-b_time) < 240): + new_obj.coat3D.applink_3b_path = osoite_3b + new_obj.coat3D.applink_3b_just_name = just_3b_name + + mat_list.append(new_obj.material_slots[0].material) + is_new = True + + if(facture_object): + texVR.matlab(new_obj, mat_list, texturelist, is_new) + else: + tex.matlab(new_obj, mat_list, texturelist, is_new) + + mat_list.pop() + + for new_obj in bpy.context.collection.objects: + if(new_obj.coat3D.applink_old == False): + new_obj.coat3D.applink_old = True + + kokeilu = coat3D.exchangedir[:-10] + Blender_folder2 = ("%s%sExchange%sBlender" % (kokeilu, os.sep, os.sep)) + Blender_folder2 += ('%sexport.txt' % (os.sep)) + + if (os.path.isfile(Blender_export)): + os.remove(Blender_export) + if (os.path.isfile(Blender_folder2)): + os.remove(Blender_folder2) + + for material in bpy.data.materials: + if material.use_nodes == True: + for node in material.node_tree.nodes: + if (node.name).startswith('3DC'): + node.location = node.location + + +def workflow1(ExportFolder): + + coat3D = bpy.context.scene.coat3D + coat = bpy.coat3D + coat3D.exchangedir = set_exchange_folder() + + + texturelist = make_texture_list(ExportFolder) + + for texturepath in texturelist: + for image in bpy.data.images: + if(image.filepath == texturepath[3] and image.users == 0): + bpy.data.images.remove(image) + + path3b_now = coat3D.exchangedir + + path3b_now += ('last_saved_3b_file.txt') + new_applink_address = 'False' + new_object = False + new_ref_object = False + + exportfile3 = coat3D.exchangedir + exportfile3 += ('%sexport.txt' % (os.sep)) + + if(os.path.isfile(exportfile3)): + + obj_pathh = open(exportfile3) + + for line in obj_pathh: + new_applink_address = line + break + obj_pathh.close() + for scene_objects in bpy.context.collection.all_objects: + if(scene_objects.type == 'MESH'): + if(scene_objects.coat3D.applink_address == new_applink_address and scene_objects.coat3D.type == 'ref'): + scene_objects.coat3D.type == '' + new_ref_object = True + nimi = scene_objects.name + + + + + exportfile = coat3D.exchangedir + exportfile += ('%sBlender' % (os.sep)) + exportfile += ('%sexport.txt' % (os.sep)) + if (os.path.isfile(exportfile)): + os.remove(exportfile) + + if(new_ref_object): + + new_ref_function(new_applink_address, nimi) - try: - os.path.isfile(Blender_folder2) - except Exception as e: - run_background_update = False - if bpy.app.timers.is_registered(run_import_periodically): - bpy.app.timers.unregister(run_import_periodically) - return time_interval - - if os.path.isfile(Blender_folder2): - window = bpy.context.window_manager.windows[0] - ctx = {'window': window, 'screen': window.screen, 'workspace': window.workspace} - bpy.ops.import_applink.pilgway_3d_coat() else: - return time_interval + blender_3DC_blender(texturelist) + +def workflow2(BlenderFolder): - if not run_background_update and bpy.app.timers.is_registered(run_import_periodically): - bpy.app.timers.unregister(run_import_periodically) - return time_interval + coat3D = bpy.context.scene.coat3D + coat = bpy.coat3D + coat3D.exchangedir = set_exchange_folder() + texturelist = make_texture_list(BlenderFolder) + + for texturepath in texturelist: + for image in bpy.data.images: + if(image.filepath == texturepath[3] and image.users == 0): + bpy.data.images.remove(image) + + kokeilu = coat3D.exchangedir + + Blender_export = os.path.join(kokeilu, 'Blender') + + path3b_now = coat3D.exchangedir + + path3b_now += ('last_saved_3b_file.txt') + Blender_export += ('%sexport.txt'%(os.sep)) + new_applink_address = 'False' + new_object = False + new_ref_object = False + + if(os.path.isfile(Blender_export)): + print('blender') + obj_pathh = open(Blender_export) + new_object = True + for line in obj_pathh: + new_applink_address = line + break + obj_pathh.close() + + for scene_objects in bpy.context.collection.all_objects: + if(scene_objects.type == 'MESH'): + if(scene_objects.coat3D.applink_address == new_applink_address): + new_object = False + + exportfile = coat3D.exchangedir + exportfile += ('%sBlender' % (os.sep)) + exportfile += ('%sexport.txt' % (os.sep)) + if (os.path.isfile(exportfile)): + os.remove(exportfile) + + if(new_ref_object): + + new_ref_function(new_applink_address, nimi) + + else: + + blender_3DC(texturelist, new_applink_address) from bpy import * from mathutils import Vector, Matrix @@ -1368,8 +1424,6 @@ class SCENE_PT_Main(bpy.types.Panel): col = flow.column() col.operator("export_applink.pilgway_3d_coat", text="Send") - col = flow.column() - col.operator("import_applink.pilgway_3d_coat", text="GetBack") class ObjectButtonsPanel(): @@ -1904,7 +1958,6 @@ classes = ( SCENE_OT_folder, SCENE_OT_opencoat, SCENE_OT_export, - SCENE_OT_import, SCENE_OT_delete_material_nodes, SCENE_OT_delete_object_nodes, SCENE_OT_delete_collection_nodes, @@ -1972,6 +2025,8 @@ def register(): bpy.types.Scene.coat3D = PointerProperty(type=SceneCoat3D) bpy.types.Mesh.coat3D = PointerProperty(type=MeshCoat3D) bpy.types.Material.coat3D = PointerProperty(type=MaterialCoat3D) + bpy.app.handlers.load_post.append(load_handler) + kc = bpy.context.window_manager.keyconfigs.addon |