34
34
self.file = open(filename, "r")
35
35
my_path = Blender.sys.dirname(filename)
36
self.lines = self.file.readlines()
38
self.lines = [l_split for l in self.file.readlines() for l_split in (' '.join(l.split()),) if l_split]
40
42
print "importing into Blender ..."
41
scene = Blender.Scene.getCurrent()
43
scene = bpy.data.scenes.active
45
mesh_indicies = {} # the index of each 'Mesh' is used as the key for those meshes indicies
46
context_indicies = None # will raise an error if used!
43
49
#Get the line of Texture Coords
47
words = line_uv.split()
48
if l and words[0] == "MeshTextureCoords" :
49
nr_uv_ind = lines.index(line_uv)
58
for line_mat in lines:
61
words = line_mat.split()
62
if l and words[0] == "Material" :
64
self.writeMaterials(i, idx, mat_list, tex_list)
72
words = line_m.split()
73
if l and words[0] == "MeshMaterialList" :
74
nr_fac_mat = lines.index(line_m) + 2
57
mesh_line_indicies = []
58
for j, line in enumerate(lines):
80
if l and words[0] == "Mesh" :
81
nr_vr_ind = lines.index(line)
82
self.writeVertices(nr_vr_ind, mesh, nr_uv_ind, nr_fac_mat, tex_list)
85
NMesh.PutRaw(mesh,"Mesh",1)
86
mesh.setMaterials(mat_list)
90
self.writeMeshMaterials(nr_fac_mat, mesh)
61
if words[0] == "Material" :
62
#context_indicies["Material"] = j
63
self.loadMaterials(j, mat_list, tex_list)
64
elif words[0] == "MeshTextureCoords" :
65
context_indicies["MeshTextureCoords"] = j
67
elif words[0] == "MeshMaterialList" :
68
context_indicies["MeshMaterialList"] = j+2
70
elif words[0] == "Mesh": # Avoid a second loop
71
context_indicies = mesh_indicies[j] = {'MeshTextureCoords':0, 'MeshMaterialList':0}
73
for mesh_index, value in mesh_indicies.iteritems():
75
self.loadVertices(mesh_index, mesh, value['MeshTextureCoords'], value['MeshMaterialList'], tex_list)
77
mesh.materials = mat_list[:16]
78
if value['MeshMaterialList']:
79
self.loadMeshMaterials(value['MeshMaterialList'], mesh)
80
scene.objects.new(mesh)
93
83
print "... finished"
95
85
#------------------------------------------------------------------------------
97
87
#------------------------------------------------------------------------------
98
def writeVertices(self, nr_vr_ind, mesh, nr_uv, nr_fac_mat, tex_list):
100
lin = self.lines[nr_vr_ind + 1]
88
def loadVertices(self, nr_vr_ind, mesh, nr_uv, nr_fac_mat, tex_list):
90
lin = self.lines[v_ind]
102
lin_c = self.CleanLine(lin)
92
lin_c = self.CleanLine(lin)
103
93
nr_vert = int((lin_c.split()[0]))
104
v_ind = self.lines.index(lin)
106
lin = self.lines.index(nr_vr_ind + 2)
107
lin_c = self.CleanLine(lin)
96
lin = self.lines[v_ind]
97
lin_c = self.CleanLine(lin)
108
98
nr_vert = int((lin_c.split()[0]))
109
v_ind = self.lines.index(lin)
111
vx_array = range(v_ind + 1, (v_ind + nr_vert +1))
112
100
#--------------------------------------------------
113
lin_f = self.lines[v_ind + nr_vert +1]
101
nr_fac_li = v_ind + nr_vert +1
102
lin_f = self.lines[nr_fac_li]
115
lin_fc = self.CleanLine(lin_f)
104
lin_fc = self.CleanLine(lin_f)
116
105
nr_face = int((lin_fc.split()[0]))
117
nr_fac_li = self.lines.index(lin_f)
119
lin_f = self.lines[v_ind + nr_vert +1]
120
lin_fc = self.CleanLine(lin_f)
107
nr_fac_li = v_ind + nr_vert +1
108
lin_f = self.lines[nr_fac_li]
109
lin_fc = self.CleanLine(lin_f)
121
110
nr_face = int((lin_fc.split()[0]))
122
nr_fac_li = self.lines.index(lin_f)
125
fac_array = range(nr_fac_li + 1, (nr_fac_li + nr_face + 1))
113
verts_list = [(0,0,0)] # WARNING - DUMMY VERT - solves EEKADOODLE ERROR
114
for l in xrange(v_ind + 1, (v_ind + nr_vert +1)):
128
115
line_v = self.lines[l]
129
116
lin_v = self.CleanLine(line_v)
130
words = lin_v.split()
117
words = lin_v.split()
131
118
if len(words)==3:
132
co_vert_x = float(words[0])
133
co_vert_y = float(words[1])
134
co_vert_z = float(words[2])
135
v=NMesh.Vert(co_vert_x,co_vert_y,co_vert_z)
119
verts_list.append((float(words[0]),float(words[1]),float(words[2])))
121
mesh.verts.extend(verts_list)
127
mesh_verts = mesh.verts
128
for f in xrange(nr_fac_li + 1, (nr_fac_li + nr_face + 1)):
144
130
line_f = self.lines[f]
145
131
lin_f = self.CleanLine(line_f)
146
words = lin_f.split()
149
f.v.append(mesh.verts[int(words[1])])
150
f.v.append(mesh.verts[int(words[2])])
151
f.v.append(mesh.verts[int(words[3])])
152
f.v.append(mesh.verts[int(words[4])])
156
#---------------------------------
157
l1_uv = self.lines[nr_uv + 2 + int(words[1])]
158
fixed_l1_uv = self.CleanLine(l1_uv)
159
w1 = fixed_l1_uv.split()
160
uv_1 = (float(w1[0]), -float(w1[1]))
162
#---------------------------------
163
l2_uv = self.lines[nr_uv + 2 + int(words[2])]
164
fixed_l2_uv = self.CleanLine(l2_uv)
165
w2 = fixed_l2_uv.split()
166
uv_2 = (float(w2[0]), -float(w2[1]))
168
#---------------------------------
169
l3_uv = self.lines[nr_uv + 2 + int(words[3])]
170
fixed_l3_uv = self.CleanLine(l3_uv)
171
w3 = fixed_l3_uv.split()
172
uv_3 = (float(w3[0]), -float(w3[1]))
174
#---------------------------------
175
l4_uv = self.lines[nr_uv + 2 + int(words[4])]
176
fixed_l4_uv = self.CleanLine(l4_uv)
177
w4 = fixed_l4_uv.split()
178
uv_4 = (float(w4[0]), -float(w4[1]))
180
#---------------------------------
183
fac_line = self.lines[nr_fac_mat + i]
184
fixed_fac = self.CleanLine(fac_line)
185
w_tex = int(fixed_fac.split()[0])
186
name_tex = tex_list[w_tex]
188
name_file = Blender.sys.join(my_path,name_tex)
190
img = Image.Load(name_file)
193
#Draw.PupMenu("No image to load")
194
#print "No image " + name_tex + " to load"
197
elif len(words) == 4:
199
f.v.append(mesh.verts[int(words[1])])
200
f.v.append(mesh.verts[int(words[2])])
201
f.v.append(mesh.verts[int(words[3])])
205
#---------------------------------
206
l1_uv = self.lines[nr_uv + 2 + int(words[1])]
207
fixed_l1_uv = self.CleanLine(l1_uv)
208
w1 = fixed_l1_uv.split()
209
uv_1 = (float(w1[0]), -float(w1[1]))
211
#---------------------------------
212
l2_uv = self.lines[nr_uv + 2 + int(words[2])]
213
fixed_l2_uv = self.CleanLine(l2_uv)
214
w2 = fixed_l2_uv.split()
215
uv_2 = (float(w2[0]), -float(w2[1]))
217
#---------------------------------
218
l3_uv = self.lines[nr_uv + 2 + int(words[3])]
219
fixed_l3_uv = self.CleanLine(l3_uv)
220
w3 = fixed_l3_uv.split()
221
uv_3 = (float(w3[0]), -float(w3[1]))
223
#---------------------------------
226
fac_line = self.lines[nr_fac_mat + i]
227
fixed_fac = self.CleanLine(fac_line)
228
w_tex = int(fixed_fac.split()[0])
229
name_tex = tex_list[w_tex]
231
name_file = Blender.sys.join(my_path ,name_tex)
233
img = Image.Load(name_file)
236
#Draw.PupMenu("No image to load")
237
#print "No image " + name_tex + " to load"
133
# +1 for dummy vert only!
134
words = lin_f.split()
136
face_list.append((1+int(words[1]), 1+int(words[2]), 1+int(words[3]), 1+int(words[4])))
137
elif len(words) == 4:
138
face_list.append((1+int(words[1]), 1+int(words[2]), 1+int(words[3])))
140
mesh.faces.extend(face_list)
147
for ii, v in enumerate(f):
148
# _u, _v = self.CleanLine(self.lines[nr_uv + 2 + v.index]).split()
151
_u, _v = self.CleanLine(self.lines[nr_uv + 1 + v.index]).split()
153
fuv[ii].x = float(_u)
154
fuv[ii].y = float(_v)
157
fac_line = self.lines[nr_fac_mat + i]
158
fixed_fac = self.CleanLine(fac_line)
159
w_tex = int(fixed_fac.split()[0])
160
f.image = tex_list[w_tex]
163
mesh.verts.delete([0,])
243
165
def CleanLine(self,line):
244
fix_line = line.replace(";", " ")
245
fix_1_line = fix_line.replace('"', ' ')
246
fix_2_line = fix_1_line.replace("{", " ")
247
fix_3_line = fix_2_line.replace("}", " ")
248
fix_4_line = fix_3_line.replace(",", " ")
249
fix_5_line = fix_4_line.replace("'", " ")
166
return line.replace(\
252
174
#------------------------------------------------------------------
253
175
# CREATE MATERIALS
254
176
#------------------------------------------------------------------
255
def writeMaterials(self, nr_mat, idx, mat_list, tex_list):
256
name = "Material_" + str(idx)
257
mat = Material.New(name)
177
def loadMaterials(self, nr_mat, mat_list, tex_list):
179
def load_image(name):
181
return Image.Load(Blender.sys.join(my_path,name))
185
mat = bpy.data.materials.new()
258
186
line = self.lines[nr_mat + 1]
259
187
fixed_line = self.CleanLine(line)
260
words = fixed_line.split()
188
words = fixed_line.split()
261
189
mat.rgbCol = [float(words[0]),float(words[1]),float(words[2])]
262
190
mat.setAlpha(float(words[3]))
263
191
mat_list.append(mat)
266
194
tex_n = fix_3_line.split()
268
196
if tex_n and tex_n[0] == "TextureFilename" :
270
198
if len(tex_n) > 1:
271
tex_list.append(tex_n[1])
199
tex_list.append(load_image(tex_n[1]))
273
201
if len(tex_n) <= 1 :
275
203
l_succ = self.lines[nr_mat + 6]
276
204
fix_3_succ = self.CleanLine(l_succ)
277
205
tex_n_succ = fix_3_succ.split()
278
tex_list.append(tex_n_succ[0])
206
tex_list.append(load_image(tex_n_succ[0]))
281
tex_list.append(tex_name)
208
tex_list.append(None) # no texture for this index
283
210
return mat_list, tex_list
284
211
#------------------------------------------------------------------
286
213
#------------------------------------------------------------------
287
def writeMeshMaterials(self, nr_fc_mat, mesh):
214
def loadMeshMaterials(self, nr_fc_mat, mesh):
288
215
for face in mesh.faces:
290
217
line = self.lines[nr_fc_mat]
291
218
fixed_line = self.CleanLine(line)
292
219
wrd = fixed_line.split()
293
220
mat_idx = int(wrd[0])
294
face.materialIndex = mat_idx
299
223
#------------------------------------------------------------------
301
225
#------------------------------------------------------------------
302
226
def my_callback(filename):
303
if not filename.find('.x', -2): print "Not an .x file"
227
if not filename.lower().endswith('.x'): print "Not an .x file"
304
228
ximport = xImport(filename)
307
231
arg = __script__['arg']
308
Blender.Window.FileSelector(my_callback, "Import DirectX")
b'\\ No newline at end of file'
233
if __name__ == '__main__':
234
Blender.Window.FileSelector(my_callback, "Import DirectX", "*.x")
236
#my_callback('/fe/x/directxterrain.x')
237
#my_callback('/fe/x/Male_Normal_MAX.X')
238
#my_callback('/fe/x/male_ms3d.x')