diff options
author | Campbell Barton <ideasman42@gmail.com> | 2007-08-01 22:04:44 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2007-08-01 22:04:44 +0400 |
commit | f707382e1e960d3476c656662ad2d1f787b418b2 (patch) | |
tree | dc1241bd97ed6c76c81eb78c874c0e9abaa565b9 /release/scripts/DirectX8Importer.py | |
parent | b38e987932b7a956210875d8bb7881921490ab02 (diff) |
misc changes from stable
Diffstat (limited to 'release/scripts/DirectX8Importer.py')
-rw-r--r-- | release/scripts/DirectX8Importer.py | 231 |
1 files changed, 90 insertions, 141 deletions
diff --git a/release/scripts/DirectX8Importer.py b/release/scripts/DirectX8Importer.py index 7e6a8429759..0dda654944d 100644 --- a/release/scripts/DirectX8Importer.py +++ b/release/scripts/DirectX8Importer.py @@ -2,7 +2,7 @@ """ Registration info for Blender menus: Name: 'DirectX(.x)...' -Blender: 240 +Blender: 244 Group: 'Import' Tip: 'Import from DirectX text file format format.' @@ -25,7 +25,7 @@ Tip: 'Import from DirectX text file format format.' # Grab the latest version here :www.omariben.too.it import bpy import Blender -from Blender import NMesh,Object,Material,Texture,Image,Draw +from Blender import Mesh,Object,Material,Texture,Image,Draw class xImport: @@ -41,13 +41,16 @@ class xImport: lines = self.lines print "importing into Blender ..." scene = bpy.data.scenes.active - mesh = NMesh.GetRaw() + + mesh_indicies = {} # the index of each 'Mesh' is used as the key for those meshes indicies + context_indicies = None # will raise an error if used! + + #Get the line of Texture Coords nr_uv_ind = 0 #Get Materials nr_fac_mat = 0 - idx = 0 i = -1 mat_list = [] tex_list = [] @@ -56,31 +59,33 @@ class xImport: l = line.strip() words = line.split() if words[0] == "Material" : - idx += 1 - self.writeMaterials(j, idx, mat_list, tex_list) + #context_indicies["Material"] = j + self.loadMaterials(j, mat_list, tex_list) elif words[0] == "MeshTextureCoords" : - nr_uv_ind = j + context_indicies["MeshTextureCoords"] = j + #nr_uv_ind = j elif words[0] == "MeshMaterialList" : - nr_fac_mat = j + 2 + context_indicies["MeshMaterialList"] = j+2 + #nr_fac_mat = j + 2 elif words[0] == "Mesh": # Avoid a second loop - mesh_line_indicies.append(j) - - #Create The Mesh - for nr_vr_ind in mesh_line_indicies: - self.writeVertices(nr_vr_ind, mesh, nr_uv_ind, nr_fac_mat, tex_list) - - mesh.setMaterials(mat_list) - if nr_fac_mat: - self.writeMeshMaterials(nr_fac_mat, mesh) - NMesh.PutRaw(mesh,"Mesh",1) - + context_indicies = mesh_indicies[j] = {'MeshTextureCoords':0, 'MeshMaterialList':0} + + for mesh_index, value in mesh_indicies.iteritems(): + mesh = Mesh.New() + self.loadVertices(mesh_index, mesh, value['MeshTextureCoords'], value['MeshMaterialList'], tex_list) + + mesh.materials = mat_list[:16] + if value['MeshMaterialList']: + self.loadMeshMaterials(value['MeshMaterialList'], mesh) + scene.objects.new(mesh) + self.file.close() print "... finished" #------------------------------------------------------------------------------ # CREATE THE MESH #------------------------------------------------------------------------------ - def writeVertices(self, nr_vr_ind, mesh, nr_uv, nr_fac_mat, tex_list): + def loadVertices(self, nr_vr_ind, mesh, nr_uv, nr_fac_mat, tex_list): v_ind = nr_vr_ind + 1 lin = self.lines[v_ind] if lin : @@ -92,7 +97,6 @@ class xImport: lin_c = self.CleanLine(lin) nr_vert = int((lin_c.split()[0])) - vx_array = range(v_ind + 1, (v_ind + nr_vert +1)) #-------------------------------------------------- nr_fac_li = v_ind + nr_vert +1 lin_f = self.lines[nr_fac_li] @@ -105,136 +109,80 @@ class xImport: lin_fc = self.CleanLine(lin_f) nr_face = int((lin_fc.split()[0])) - fac_array = range(nr_fac_li + 1, (nr_fac_li + nr_face + 1)) #Get Coordinates - for l in vx_array: + verts_list = [(0,0,0)] # WARNING - DUMMY VERT - solves EEKADOODLE ERROR + for l in xrange(v_ind + 1, (v_ind + nr_vert +1)): line_v = self.lines[l] lin_v = self.CleanLine(line_v) words = lin_v.split() if len(words)==3: - mesh.verts.append(NMesh.Vert(float(words[0]),float(words[1]),float(words[2]))) - + verts_list.append((float(words[0]),float(words[1]),float(words[2]))) + + mesh.verts.extend(verts_list) + del verts_list + + face_list = [] #Make Faces i = 0 mesh_verts = mesh.verts - for f in fac_array: + for f in xrange(nr_fac_li + 1, (nr_fac_li + nr_face + 1)): i += 1 line_f = self.lines[f] lin_f = self.CleanLine(line_f) + + # +1 for dummy vert only! words = lin_f.split() if len(words) == 5: - f= NMesh.Face([\ - mesh_verts[int(words[1])], - mesh_verts[int(words[2])], - mesh_verts[int(words[3])], - mesh_verts[int(words[4])]]) - - mesh.faces.append(f) - if nr_uv : - uv = [] - #--------------------------------- - l1_uv = self.lines[nr_uv + 2 + int(words[1])] - fixed_l1_uv = self.CleanLine(l1_uv) - w1 = fixed_l1_uv.split() - uv_1 = (float(w1[0]), -float(w1[1])) - uv.append(uv_1) - #--------------------------------- - l2_uv = self.lines[nr_uv + 2 + int(words[2])] - fixed_l2_uv = self.CleanLine(l2_uv) - w2 = fixed_l2_uv.split() - uv_2 = (float(w2[0]), -float(w2[1])) - uv.append(uv_2) - #--------------------------------- - l3_uv = self.lines[nr_uv + 2 + int(words[3])] - fixed_l3_uv = self.CleanLine(l3_uv) - w3 = fixed_l3_uv.split() - uv_3 = (float(w3[0]), -float(w3[1])) - uv.append(uv_3) - #--------------------------------- - l4_uv = self.lines[nr_uv + 2 + int(words[4])] - fixed_l4_uv = self.CleanLine(l4_uv) - w4 = fixed_l4_uv.split() - uv_4 = (float(w4[0]), -float(w4[1])) - uv.append(uv_4) - #--------------------------------- - f.uv = uv - if nr_fac_mat : - fac_line = self.lines[nr_fac_mat + i] - fixed_fac = self.CleanLine(fac_line) - w_tex = int(fixed_fac.split()[0]) - name_tex = tex_list[w_tex] - if name_tex : - name_file = Blender.sys.join(my_path,name_tex) - try: - img = Image.Load(name_file) - f.image = img - except: - #Draw.PupMenu("No image to load") - #print "No image " + name_tex + " to load" - pass - + face_list.append((1+int(words[1]), 1+int(words[2]), 1+int(words[3]), 1+int(words[4]))) elif len(words) == 4: - f=NMesh.Face([\ - mesh_verts[int(words[1])],\ - mesh_verts[int(words[2])],\ - mesh_verts[int(words[3])]]) - - mesh.faces.append(f) - if nr_uv : - uv = [] - #--------------------------------- - l1_uv = self.lines[nr_uv + 2 + int(words[1])] - fixed_l1_uv = self.CleanLine(l1_uv) - w1 = fixed_l1_uv.split() - uv_1 = (float(w1[0]), -float(w1[1])) - uv.append(uv_1) - #--------------------------------- - l2_uv = self.lines[nr_uv + 2 + int(words[2])] - fixed_l2_uv = self.CleanLine(l2_uv) - w2 = fixed_l2_uv.split() - uv_2 = (float(w2[0]), -float(w2[1])) - uv.append(uv_2) - #--------------------------------- - l3_uv = self.lines[nr_uv + 2 + int(words[3])] - fixed_l3_uv = self.CleanLine(l3_uv) - w3 = fixed_l3_uv.split() - uv_3 = (float(w3[0]), -float(w3[1])) - uv.append(uv_3) - #--------------------------------- - f.uv = uv - if nr_fac_mat : - fac_line = self.lines[nr_fac_mat + i] - fixed_fac = self.CleanLine(fac_line) - w_tex = int(fixed_fac.split()[0]) - name_tex = tex_list[w_tex] - if name_tex : - name_file = Blender.sys.join(my_path ,name_tex) - try: - img = Image.Load(name_file) - f.image = img - except: - #Draw.PupMenu("No image to load") - #print "No image " + name_tex + " to load" - pass - - - - + face_list.append((1+int(words[1]), 1+int(words[2]), 1+int(words[3]))) + + mesh.faces.extend(face_list) + del face_list + + if nr_uv : + mesh.faceUV = True + for f in mesh.faces: + fuv = f.uv + for ii, v in enumerate(f): + # _u, _v = self.CleanLine(self.lines[nr_uv + 2 + v.index]).split() + + # Use a dummy vert + _u, _v = self.CleanLine(self.lines[nr_uv + 1 + v.index]).split() + + fuv[ii].x = float(_u) + fuv[ii].y = float(_v) + + if nr_fac_mat : + fac_line = self.lines[nr_fac_mat + i] + fixed_fac = self.CleanLine(fac_line) + w_tex = int(fixed_fac.split()[0]) + f.image = tex_list[w_tex] + + # remove dummy vert + mesh.verts.delete([0,]) + def CleanLine(self,line): - fix_line = line.replace(";", " ") - fix_1_line = fix_line.replace('"', ' ') - fix_2_line = fix_1_line.replace("{", " ") - fix_3_line = fix_2_line.replace("}", " ") - fix_4_line = fix_3_line.replace(",", " ") - fix_5_line = fix_4_line.replace("'", " ") - return fix_5_line + return line.replace(\ + ";", " ").replace(\ + '"', ' ').replace(\ + "{", " ").replace(\ + "}", " ").replace(\ + ",", " ").replace(\ + "'", " ") #------------------------------------------------------------------ # CREATE MATERIALS #------------------------------------------------------------------ - def writeMaterials(self, nr_mat, idx, mat_list, tex_list): - name = "Material_" + str(idx) - mat = Material.New(name) + def loadMaterials(self, nr_mat, mat_list, tex_list): + + def load_image(name): + try: + return Image.Load(Blender.sys.join(my_path,name)) + except: + return None + + mat = bpy.data.materials.new() line = self.lines[nr_mat + 1] fixed_line = self.CleanLine(line) words = fixed_line.split() @@ -244,35 +192,33 @@ class xImport: l = self.lines[nr_mat + 5] fix_3_line = self.CleanLine(l) tex_n = fix_3_line.split() - + if tex_n and tex_n[0] == "TextureFilename" : if len(tex_n) > 1: - tex_list.append(tex_n[1]) + tex_list.append(load_image(tex_n[1])) if len(tex_n) <= 1 : l_succ = self.lines[nr_mat + 6] fix_3_succ = self.CleanLine(l_succ) tex_n_succ = fix_3_succ.split() - tex_list.append(tex_n_succ[0]) + tex_list.append(load_image(tex_n_succ[0])) else : - tex_name = None - tex_list.append(tex_name) + tex_list.append(None) # no texture for this index return mat_list, tex_list #------------------------------------------------------------------ # SET MATERIALS #------------------------------------------------------------------ - def writeMeshMaterials(self, nr_fc_mat, mesh): + def loadMeshMaterials(self, nr_fc_mat, mesh): for face in mesh.faces: nr_fc_mat += 1 line = self.lines[nr_fc_mat] fixed_line = self.CleanLine(line) wrd = fixed_line.split() mat_idx = int(wrd[0]) - face.materialIndex = mat_idx - + face.mat = mat_idx #------------------------------------------------------------------ # MAIN @@ -286,4 +232,7 @@ arg = __script__['arg'] if __name__ == '__main__': Blender.Window.FileSelector(my_callback, "Import DirectX", "*.x") -# my_callback('/directxterrain.x') + +#my_callback('/fe/x/directxterrain.x') +#my_callback('/fe/x/Male_Normal_MAX.X') +#my_callback('/fe/x/male_ms3d.x') |