From 6efe8ccf551e9c3dd7354387da3cf6256dac5bf8 Mon Sep 17 00:00:00 2001
From: Kalle-Samuli Riihikoski <haikalle@gmail.com>
Date: Mon, 5 Sep 2011 14:08:00 +0000
Subject: [PATCH] Cleaning the code. Also added support for multres objects.

---
 io_coat3D/__init__.py |  11 ++-
 io_coat3D/coat.py     |  83 ++++++++++++----------
 io_coat3D/tex.py      | 156 ++++++++++--------------------------------
 3 files changed, 88 insertions(+), 162 deletions(-)

diff --git a/io_coat3D/__init__.py b/io_coat3D/__init__.py
index 268bab8be..5041034c7 100644
--- a/io_coat3D/__init__.py
+++ b/io_coat3D/__init__.py
@@ -121,12 +121,6 @@ def register():
             description="Export Coarse.",
             default= True
         )
-   
-        smooth_on = BoolProperty(
-            name="Auto Smooth",
-            description="Add Modifiers and export.",
-            default= True
-        )
         exportfile = BoolProperty(
             name="No Import File",
             description="Add Modifiers and export.",
@@ -147,6 +141,11 @@ def register():
             description="Remember position.",
             default= True
         )
+        multires = BoolProperty(
+            name="Multires. Levels",
+            description="Saves Multiresolution data.",
+            default= True
+        )
         importtextures = BoolProperty(
             name="Bring Textures",
             description="Import Textures.",
diff --git a/io_coat3D/coat.py b/io_coat3D/coat.py
index 930efb3f8..67571353b 100644
--- a/io_coat3D/coat.py
+++ b/io_coat3D/coat.py
@@ -27,7 +27,6 @@ bpy.coat3D = dict()
 bpy.coat3D['active_coat'] = ''
 bpy.coat3D['status'] = 0
 
-
 class ObjectButtonsPanel():
     bl_space_type = 'PROPERTIES'
     bl_region_type = 'WINDOW'
@@ -106,8 +105,8 @@ class SCENE_PT_Main(ObjectButtonsPanel,bpy.types.Panel):
             colR.label(text="Import Settings:")
             colR.prop(coat3D,"importmesh")
             colR.prop(coat3D,"importmod")
-            colR.prop(coat3D,"smooth_on")
             colR.prop(coat3D,"importtextures")
+            colR.prop(coat3D,"multires")
             row = layout.row()
         
         if(bpy.context.selected_objects):
@@ -199,7 +198,6 @@ class SCENE_OT_export(bpy.types.Operator):
     bl_description = "Export your custom property"
     bl_options = {'UNDO'}
 
-    
     def invoke(self, context, event):
         checkname = ''
         coat3D = bpy.context.scene.coat3D
@@ -266,7 +264,6 @@ class SCENE_OT_export(bpy.types.Operator):
 
         return {'FINISHED'}
 
-
 class SCENE_OT_import(bpy.types.Operator):
     bl_idname = "import_applink.pilgway_3d_coat"
     bl_label = "import your custom property"
@@ -281,6 +278,7 @@ class SCENE_OT_import(bpy.types.Operator):
         act_first = bpy.context.scene.objects.active
         for act_name in test:
             if act_name.type == 'MESH' and os.path.isfile(act_name.coat3D.objectdir):
+                multires_on = False
                 activeobj = act_name.name
                 mat_list = []
                 scene.objects[activeobj].select = True
@@ -289,6 +287,18 @@ class SCENE_OT_import(bpy.types.Operator):
                 coat3D.rota = objekti.rotation_euler
                 coa = act_name.coat3D
 
+                if(coat3D.multires):
+                    for modifiers in objekti.modifiers:
+                        if modifiers.type == 'MULTIRES' and (modifiers.total_levels > 0):
+                            bpy.ops.object.multires_external_pack()
+                            multires = coat3D.exchangedir
+                            multires += ('%stemp.btx'%(os.sep))
+                            bpy.ops.object.multires_external_save(filepath=multires)
+                            #bpy.ops.object.multires_external_pack()
+                            multires_on = True
+                            multires_name = modifiers.name
+                            break
+                        
                 exportfile = coat3D.exchangedir
                 path3b_n = coat3D.exchangedir
                 path3b_n += ('last_saved_3b_file.txt')
@@ -303,11 +313,19 @@ class SCENE_OT_import(bpy.types.Operator):
                     os.remove(exportfile)
                     
                 if(objekti.material_slots):
+                    act_mat_index = objekti.active_material_index
+                    if(not(hasattr(objekti.active_material,'texture_slots'))):
+                        materials_old = bpy.data.materials.keys()
+                        bpy.ops.material.new()
+                        materials_new = bpy.data.materials.keys()
+                        new_ma = list(set(materials_new).difference(set(materials_old)))
+                        new_mat = new_ma[0]
+                        ki = bpy.data.materials[new_mat]
+                        objekti.material_slots[act_mat_index].material = ki
                     for obj_mat in objekti.material_slots:
                         mat_list.append(obj_mat.material)
-                    act_mat_index = objekti.active_material_index
-
 
+                 
                 if(coat3D.importmesh and os.path.isfile(coa.objectdir)):
                     mtl = coa.objectdir
                     mtl = mtl.replace('.obj','.mtl')
@@ -342,11 +360,6 @@ class SCENE_OT_import(bpy.types.Operator):
                     objekti.select = True
                     bpy.context.scene.objects.active = objekti
 
-                    if(coat3D.smooth_on):
-                        bpy.ops.object.shade_smooth()
-                    else:
-                        bpy.ops.object.shade_flat()
-
                 if(os.path.isfile(path3b_n)):
                     path3b_fil = open(path3b_n)
                     for lin in path3b_fil:
@@ -364,23 +377,36 @@ class SCENE_OT_import(bpy.types.Operator):
                     
                 if(mat_list):
                     for obj_mate in objekti.material_slots:
-                        for tex_slot in obj_mate.material.texture_slots:
-                            if(hasattr(tex_slot,'texture')):
-                                if(tex_slot.texture.type == 'IMAGE'):
-                                    if tex_slot.texture.image is not None:
-                                        tex_slot.texture.image.reload()
+                        if(hasattr(obj_mate.material,'texture_slots')):
+                            for tex_slot in obj_mate.material.texture_slots:
+                                if(hasattr(tex_slot,'texture')):
+                                    if(tex_slot.texture.type == 'IMAGE'):
+                                        if tex_slot.texture.image is not None:
+                                            tex_slot.texture.image.reload()
                                                                 
-
                 if(coat3D.importmod):
                     for mod_index in objekti.modifiers[:]:
                         objekti.modifiers.remove(mod_index)
                         
-                
-                        
                 if(coat3D.importtextures):
                                 export = ''
                                 tex.gettex(mat_list,objekti,scene,export)
-        
+
+                if(multires_on):
+                    temp_file = coat3D.exchangedir
+                    temp_file += ('%stemp2.btx'%(os.sep))
+                    if(objekti.modifiers[multires_name].levels == 0):
+                        objekti.modifiers[multires_name].levels = 1
+                        bpy.ops.object.multires_external_save(filepath=temp_file)
+                        objekti.modifiers[multires_name].filepath = multires
+                        objekti.modifiers[multires_name].levels = 0
+
+                    else:
+                        bpy.ops.object.multires_external_save(filepath=temp_file)
+                        objekti.modifiers[multires_name].filepath = multires
+                    #bpy.ops.object.multires_external_pack()
+                bpy.ops.object.shade_smooth()
+                
         for act_name in test:
             act_name.select = True
         bpy.context.scene.objects.active = act_first
@@ -393,7 +419,6 @@ class SCENE_OT_import3b(bpy.types.Operator):
     bl_description = "Bring 3D-Coat Mesh"
     bl_options = {'UNDO'}
 
-    
     def invoke(self, context, event):
 
         coat3D = bpy.context.scene.coat3D
@@ -452,16 +477,12 @@ class SCENE_OT_import3b(bpy.types.Operator):
         
         bpy.context.scene.objects.active = new_obj
 
-        if(coat3D.smooth_on):
-            bpy.ops.object.shade_smooth()
-        else:
-            bpy.ops.object.shade_flat()
-
+        bpy.ops.object.shade_smooth()
+       
         Blender_tex = ("%s%stextures.txt"%(coat3D.exchangedir,os.sep))
         mat_list.append(new_obj.material_slots[0].material)
         tex.gettex(mat_list, new_obj, scene,export)
 
-
         return {'FINISHED'}
 
 class SCENE_OT_load3b(bpy.types.Operator):
@@ -487,7 +508,6 @@ class SCENE_OT_load3b(bpy.types.Operator):
             file.write("\n[3B]")
             file.close()
 
-
         return {'FINISHED'}
 
 class SCENE_OT_deltex(bpy.types.Operator):
@@ -495,7 +515,6 @@ class SCENE_OT_deltex(bpy.types.Operator):
     bl_label = "Picks Object's name into path"
     bl_description = "Loads 3b linked into object"
 
-    
     def invoke(self, context, event):
         if(bpy.context.selected_objects):
             if(context.selected_objects[0].type == 'MESH'):
@@ -518,11 +537,9 @@ class SCENE_OT_deltex(bpy.types.Operator):
     
         return {'FINISHED'}
 
-
 from bpy import *
 from mathutils import Vector, Matrix
 
-
 # 3D-Coat Dynamic Menu
 class VIEW3D_MT_Coat_Dynamic_Menu(bpy.types.Menu):
     bl_label = "3D-Coat Applink Menu"
@@ -584,9 +601,6 @@ class VIEW3D_MT_Coat_Dynamic_Menu(bpy.types.Menu):
                     layout.operator("import3b_applink.pilgway_3d_coat", text="Bring from 3D-Coat")
                     layout.separator()
                 
-          
-            
-        
 class VIEW3D_MT_ImportMenu(bpy.types.Menu):
     bl_label = "Import Settings"
 
@@ -634,7 +648,6 @@ def register():
     kmi = km.keymap_items.new('wm.call_menu2', 'Q', 'PRESS')
     kmi.properties.name = "VIEW3D_MT_Coat_Dynamic_Menu"
 
-
 def unregister():
     bpy.utils.unregister_module(__name__)
 
diff --git a/io_coat3D/tex.py b/io_coat3D/tex.py
index d3b5f56d0..816dacd32 100644
--- a/io_coat3D/tex.py
+++ b/io_coat3D/tex.py
@@ -20,26 +20,6 @@
 import bpy
 import os
 
-def objname(path):
-
-    path2 = os.path.dirname(path) + os.sep
-    pituus = len(path2)
-    nimi = path[pituus:]
-    return nimi
-
-def justname(name):
-    monesko = name.rfind('.')
-    justname = name[:monesko]
-    return justname
-
-def setgallery():
-    newname =''
-    tex_name =[]
-    index_tex = 0
-    for tt in bpy.data.textures:
-        tex_name.append(tt.name)
-    return tex_name
-
 def find_index(objekti):
         luku = 0
         for tex in objekti.active_material.texture_slots:
@@ -52,7 +32,6 @@ def gettex(mat_list, objekti, scene,export):
 
     coat3D = bpy.context.scene.coat3D
     
-    
     if(bpy.context.scene.render.engine == 'VRAY_RENDER' or bpy.context.scene.render.engine == 'VRAY_RENDER_PREVIEW'):
         vray = True
     else:
@@ -77,7 +56,7 @@ def gettex(mat_list, objekti, scene,export):
 
     if(export):
         objekti.coat3D.objpath = export
-        nimi = objname(export)
+        nimi = os.path.split(export)[1]
         osoite = os.path.dirname(export) + os.sep
         for mate in objekti.material_slots:
             for tex_slot in mate.material.texture_slots:
@@ -87,13 +66,12 @@ def gettex(mat_list, objekti, scene,export):
                             tex_slot.texture.image.reload()
     else:
         coa = objekti.coat3D
-        nimi = objname(coa.objectdir)
+        nimi = os.path.split(coa.objectdir)[1]
         if(coa.texturefolder):
             osoite = os.path.dirname(coa.texturefolder) + os.sep
         else:
             osoite = os.path.dirname(coa.objectdir) + os.sep
-    just_nimi = justname(nimi)
-    just_nimi += '_'
+    just_nimi = os.path.splitext(nimi)[0] + '_'
     just_nimi_len = len(just_nimi)
         
     if(len(objekti.material_slots) != 0):
@@ -123,69 +101,40 @@ def gettex(mat_list, objekti, scene,export):
         koko += osoite
         koko += yy
         texcoat[tex_name].append(koko)
-        #date = os.path.getmtime(texcoat[tex_name][0])
 
     if((texcoat['color'] or texcoat['nmap'] or texcoat['disp'] or texcoat['specular']) and (len(objekti.material_slots)) == 0):
-        index = 0
-        tuli = False
-        lasku = False
-        while(lasku == False):
-            tuli = False
-            new_mat = ("Material.%03d"%(index))
-            for i in bpy.data.materials:
-                if(i.name == new_mat):
-                    tuli = True
-                    break
-            if(tuli):
-                index += 1
-            else:
-                lasku = True
-        bpy.data.materials.new(new_mat)
+        materials_old = bpy.data.materials.keys()
+        bpy.ops.material.new()
+        materials_new = bpy.data.materials.keys()
+        new_ma = list(set(materials_new).difference(set(materials_old)))
+        new_mat = new_ma[0]
         ki = bpy.data.materials[new_mat]
         objekti.data.materials.append(ki)
         
     if(bring_color == 1 and texcoat['color']):
-        name_tex ='Color_'
-        num = []
-
         index = find_index(objekti)
-        
-
         tex = bpy.ops.Texture
         objekti.active_material.texture_slots.create(index)
         total_mat = len(objekti.active_material.texture_slots.items())
         useold = ''
         
         for seekco in bpy.data.textures:
-            if((seekco.name[:6] == 'Color_') and (seekco.users_material == ())):
+            if((seekco.name[:5] == 'Color') and (seekco.users_material == ())):
                 useold = seekco
 
 
         if(useold == ''):
 
-            indexx = 0
-            tuli = False
-            lasku = False
-            while(lasku == False):
-                tuli = False
-                name_tex = ("Color_%s"%(indexx))
-                for i in bpy.data.textures:
-                    if(i.name == name_tex):
-                        tuli = True
-                        break
-                if(tuli):
-                    indexx += 1
-                else:
-                    lasku = True
-                
+            textures_old = bpy.data.textures.keys()
+            bpy.data.textures.new('Color',type='IMAGE')
+            textures_new = bpy.data.textures.keys()
+            name_te = list(set(textures_new).difference(set(textures_old)))
+            name_tex = name_te[0]
 
             bpy.ops.image.new(name=name_tex)
             bpy.data.images[name_tex].filepath = texcoat['color'][0]
             bpy.data.images[name_tex].source = 'FILE'
             
-            
-
-            bpy.data.textures.new(name_tex,type='IMAGE')
             objekti.active_material.texture_slots[index].texture = bpy.data.textures[name_tex]
             objekti.active_material.texture_slots[index].texture.image = bpy.data.images[name_tex]
         
@@ -207,9 +156,6 @@ def gettex(mat_list, objekti, scene,export):
 
     
     if(bring_normal == 1 and texcoat['nmap']):
-        name_tex ='Normal_'
-        num = []
-        
         index = find_index(objekti)
         tex = bpy.ops.Texture
         objekti.active_material.texture_slots.create(index)
@@ -217,38 +163,28 @@ def gettex(mat_list, objekti, scene,export):
         useold = ''
         
         for seekco in bpy.data.textures:
-            if((seekco.name[:7] == 'Normal_') and (seekco.users_material == ())):
+            if((seekco.name[:6] == 'Normal') and (seekco.users_material == ())):
                 useold = seekco
 
         if(useold == ''):
 
-            indexx = 0
-            tuli = False
-            lasku = False
-            while(lasku == False):
-                tuli = False
-                name_tex = ("Normal_%s"%(indexx))
-                for i in bpy.data.textures:
-                    if(i.name == name_tex):
-                        tuli = True
-                        break
-                if(tuli):
-                    indexx += 1
-                else:
-                    lasku = True
+            textures_old = bpy.data.textures.keys()
+            bpy.data.textures.new('Normal',type='IMAGE')
+            textures_new = bpy.data.textures.keys()
+            name_te = list(set(textures_new).difference(set(textures_old)))
+            name_tex = name_te[0]
 
             bpy.ops.image.new(name=name_tex)
             bpy.data.images[name_tex].filepath = texcoat['nmap'][0]
             bpy.data.images[name_tex].source = 'FILE'
             
-
-            bpy.data.textures.new(name_tex,type='IMAGE')
             objekti.active_material.texture_slots[index].texture = bpy.data.textures[name_tex]
             objekti.active_material.texture_slots[index].texture.image = bpy.data.images[name_tex]
         
             if(objekti.data.uv_textures.active):
                 objekti.active_material.texture_slots[index].texture_coords = 'UV'
                 objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
+
             objekti.active_material.texture_slots[index].use_map_color_diffuse = False
             objekti.active_material.texture_slots[index].use_map_normal = True
 
@@ -277,7 +213,6 @@ def gettex(mat_list, objekti, scene,export):
 
 
     if(bring_spec == 1 and texcoat['specular']):
-        name_tex ='Specular_'
 
         index = find_index(objekti)
         
@@ -285,38 +220,28 @@ def gettex(mat_list, objekti, scene,export):
         useold = ''
         
         for seekco in bpy.data.textures:
-            if((seekco.name[:9] == 'Specular_') and (seekco.users_material == ())):
+            if((seekco.name[:8] == 'Specular') and (seekco.users_material == ())):
                 useold = seekco
 
         if(useold == ''):
 
-            indexx = 0
-            tuli = False
-            lasku = False
-            while(lasku == False):
-                tuli = False
-                name_tex = ("Specular_%s"%(indexx))
-                for i in bpy.data.textures:
-                    if(i.name == name_tex):
-                        tuli = True
-                        break
-                if(tuli):
-                    indexx += 1
-                else:
-                    lasku = True
+            textures_old = bpy.data.textures.keys()
+            bpy.data.textures.new('Specular',type='IMAGE')
+            textures_new = bpy.data.textures.keys()
+            name_te = list(set(textures_new).difference(set(textures_old)))
+            name_tex = name_te[0]
 
             bpy.ops.image.new(name=name_tex)
             bpy.data.images[name_tex].filepath = texcoat['specular'][0]
             bpy.data.images[name_tex].source = 'FILE'
-            
 
-            bpy.data.textures.new(name_tex,type='IMAGE')
             objekti.active_material.texture_slots[index].texture = bpy.data.textures[name_tex]
             objekti.active_material.texture_slots[index].texture.image = bpy.data.images[name_tex]
         
             if(objekti.data.uv_textures.active):
                 objekti.active_material.texture_slots[index].texture_coords = 'UV'
                 objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
+
             objekti.active_material.texture_slots[index].use_map_color_diffuse = False
             objekti.active_material.texture_slots[index].use_map_specular = True
 
@@ -335,7 +260,6 @@ def gettex(mat_list, objekti, scene,export):
             objekti.active_material.texture_slots[index].use_map_specular = True
 
     if(bring_disp == 1 and texcoat['disp']):
-        name_tex ='Displacement_'
 
         index = find_index(objekti)
         
@@ -344,38 +268,28 @@ def gettex(mat_list, objekti, scene,export):
         useold = ''
         
         for seekco in bpy.data.textures:
-            if((seekco.name[:13] == 'Displacement_') and (seekco.users_material == ())):
+            if((seekco.name[:12] == 'Displacement') and (seekco.users_material == ())):
                 useold = seekco
 
         if useold == "":
 
-            indexx = 0
-            tuli = False
-            lasku = False
-            while(lasku == False):
-                tuli = False
-                name_tex = ("Displacement_%s"%(indexx))
-                for i in bpy.data.textures:
-                    if(i.name == name_tex):
-                        tuli = True
-                        break
-                if(tuli):
-                    indexx += 1
-                else:
-                    lasku = True
+            textures_old = bpy.data.textures.keys()
+            bpy.data.textures.new('Displacement',type='IMAGE')
+            textures_new = bpy.data.textures.keys()
+            name_te = list(set(textures_new).difference(set(textures_old)))
+            name_tex = name_te[0]
 
             bpy.ops.image.new(name=name_tex)
             bpy.data.images[name_tex].filepath = texcoat['disp'][0]
             bpy.data.images[name_tex].source = 'FILE'
             
-
-            bpy.data.textures.new(name_tex,type='IMAGE')
             objekti.active_material.texture_slots[index].texture = bpy.data.textures[name_tex]
             objekti.active_material.texture_slots[index].texture.image = bpy.data.images[name_tex]
         
             if(objekti.data.uv_textures.active):
                 objekti.active_material.texture_slots[index].texture_coords = 'UV'
                 objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
+
             objekti.active_material.texture_slots[index].use_map_color_diffuse = False
             objekti.active_material.texture_slots[index].use_map_displacement = True
 
-- 
GitLab