Updated this script not to choke on some blank lines (example file from Lekane)

Also removed the use of list.index() all over the place where the index was known. (saves searching the file and the possibility of duplicates messing it up)
This commit is contained in:
Campbell Barton 2007-05-31 06:12:09 +00:00
parent 6e716521f8
commit 6057ab05f0

@ -23,7 +23,7 @@ Tip: 'Import from DirectX text file format format.'
# This script import meshes from DirectX text file format
# Grab the latest version here :www.omariben.too.it
import bpy
import Blender
from Blender import NMesh,Object,Material,Texture,Image,Draw
@ -33,61 +33,46 @@ class xImport:
global my_path
self.file = open(filename, "r")
my_path = Blender.sys.dirname(filename)
self.lines = self.file.readlines()
#
self.lines = [l_split for l in self.file.readlines() for l_split in (' '.join(l.split()),) if l_split]
def Import(self):
lines = self.lines
print "importing into Blender ..."
scene = Blender.Scene.getCurrent()
scene = bpy.data.scenes.active
mesh = NMesh.GetRaw()
#Get the line of Texture Coords
nr_uv_ind = 0
for line_uv in lines:
l = line_uv.strip()
words = line_uv.split()
if l and words[0] == "MeshTextureCoords" :
nr_uv_ind = lines.index(line_uv)
#Get Materials
nr_fac_mat = 0
idx = 0
i = -1
mat_list = []
tex_list = []
for line_mat in lines:
i += 1
l = line_mat.strip()
words = line_mat.split()
if l and words[0] == "Material" :
idx += 1
self.writeMaterials(i, idx, mat_list, tex_list)
nr_fac_mat = 0
#Assign Materials
for line_m in lines:
l = line_m.strip()
words = line_m.split()
if l and words[0] == "MeshMaterialList" :
nr_fac_mat = lines.index(line_m) + 2
#Create The Mesh
for line in lines:
mesh_line_indicies = []
for j, line in enumerate(lines):
l = line.strip()
words = line.split()
if l and words[0] == "Mesh" :
nr_vr_ind = lines.index(line)
if words[0] == "Material" :
idx += 1
self.writeMaterials(j, idx, mat_list, tex_list)
elif words[0] == "MeshTextureCoords" :
nr_uv_ind = j
elif words[0] == "MeshMaterialList" :
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)
NMesh.PutRaw(mesh,"Mesh",1)
mesh.setMaterials(mat_list)
mesh.update()
if nr_fac_mat :
if nr_fac_mat:
self.writeMeshMaterials(nr_fac_mat, mesh)
NMesh.PutRaw(mesh,"Mesh",1)
self.file.close()
print "... finished"
@ -96,31 +81,29 @@ class xImport:
# CREATE THE MESH
#------------------------------------------------------------------------------
def writeVertices(self, nr_vr_ind, mesh, nr_uv, nr_fac_mat, tex_list):
lin = self.lines[nr_vr_ind + 1]
v_ind = nr_vr_ind + 1
lin = self.lines[v_ind]
if lin :
lin_c = self.CleanLine(lin)
nr_vert = int((lin_c.split()[0]))
v_ind = self.lines.index(lin)
else :
lin = self.lines.index(nr_vr_ind + 2)
v_ind = nr_vr_ind + 2
lin = self.lines[v_ind]
lin_c = self.CleanLine(lin)
nr_vert = int((lin_c.split()[0]))
v_ind = self.lines.index(lin)
vx_array = range(v_ind + 1, (v_ind + nr_vert +1))
#--------------------------------------------------
lin_f = self.lines[v_ind + nr_vert +1]
nr_fac_li = v_ind + nr_vert +1
lin_f = self.lines[nr_fac_li]
if lin_f :
lin_fc = self.CleanLine(lin_f)
nr_face = int((lin_fc.split()[0]))
nr_fac_li = self.lines.index(lin_f)
else :
lin_f = self.lines[v_ind + nr_vert +1]
nr_fac_li = v_ind + nr_vert +1
lin_f = self.lines[nr_fac_li]
lin_fc = self.CleanLine(lin_f)
nr_face = int((lin_fc.split()[0]))
nr_fac_li = self.lines.index(lin_f)
fac_array = range(nr_fac_li + 1, (nr_fac_li + nr_face + 1))
#Get Coordinates
@ -129,27 +112,23 @@ class xImport:
lin_v = self.CleanLine(line_v)
words = lin_v.split()
if len(words)==3:
co_vert_x = float(words[0])
co_vert_y = float(words[1])
co_vert_z = float(words[2])
v=NMesh.Vert(co_vert_x,co_vert_y,co_vert_z)
mesh.verts.append(v)
mesh.verts.append(NMesh.Vert(float(words[0]),float(words[1]),float(words[2])))
#Make Faces
i = 0
mesh_verts = mesh.verts
for f in fac_array:
i += 1
line_f = self.lines[f]
lin_f = self.CleanLine(line_f)
words = lin_f.split()
if len(words) == 5:
f=NMesh.Face()
f.v.append(mesh.verts[int(words[1])])
f.v.append(mesh.verts[int(words[2])])
f.v.append(mesh.verts[int(words[3])])
f.v.append(mesh.verts[int(words[4])])
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 = []
@ -195,10 +174,11 @@ class xImport:
pass
elif len(words) == 4:
f=NMesh.Face()
f.v.append(mesh.verts[int(words[1])])
f.v.append(mesh.verts[int(words[2])])
f.v.append(mesh.verts[int(words[3])])
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 = []
@ -292,8 +272,6 @@ class xImport:
wrd = fixed_line.split()
mat_idx = int(wrd[0])
face.materialIndex = mat_idx
mesh.update()
#------------------------------------------------------------------
@ -308,4 +286,4 @@ arg = __script__['arg']
if __name__ == '__main__':
Blender.Window.FileSelector(my_callback, "Import DirectX", "*.x")
# my_callback('/directxterrain.x')