~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to release/scripts/DirectX8Importer.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
""" Registration info for Blender menus:
4
4
Name: 'DirectX(.x)...'
5
 
Blender: 240
 
5
Blender: 244
6
6
Group: 'Import'
7
7
 
8
8
Tip: 'Import from DirectX text file format format.'
23
23
# This script import meshes from DirectX text file format
24
24
 
25
25
# Grab the latest version here :www.omariben.too.it
26
 
 
 
26
import bpy
27
27
import Blender
28
 
from Blender import NMesh,Object,Material,Texture,Image,Draw
 
28
from Blender import Mesh,Object,Material,Texture,Image,Draw
29
29
 
30
30
 
31
31
class xImport:
33
33
                global my_path
34
34
                self.file = open(filename, "r")
35
35
                my_path = Blender.sys.dirname(filename)
36
 
                self.lines = self.file.readlines()
 
36
 
 
37
                # 
 
38
                self.lines = [l_split for l in self.file.readlines() for l_split in (' '.join(l.split()),) if l_split]
37
39
 
38
40
        def Import(self):
39
41
                lines = self.lines
40
42
                print "importing into Blender ..."
41
 
                scene  = Blender.Scene.getCurrent()
42
 
                mesh = NMesh.GetRaw()
 
43
                scene  = bpy.data.scenes.active
 
44
                
 
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!
 
47
                
 
48
                
43
49
                #Get the line of Texture Coords
44
50
                nr_uv_ind = 0
45
 
                for line_uv in lines:
46
 
                        l = line_uv.strip()
47
 
                        words = line_uv.split()                                                                                                                                                                                                                                                                                                                                                                                                 
48
 
                        if l and words[0] == "MeshTextureCoords" :
49
 
                                nr_uv_ind = lines.index(line_uv)        
50
 
                 
51
 
                
52
 
                                
53
 
                #Get Materials          
54
 
                idx = 0
 
51
 
 
52
                #Get Materials
 
53
                nr_fac_mat = 0
55
54
                i = -1
56
55
                mat_list = []
57
56
                tex_list = []
58
 
                for line_mat in lines:
59
 
                        i += 1
60
 
                        l = line_mat.strip()
61
 
                        words = line_mat.split()                                                                                                                                                                                                                                                                                                                                                                                                        
62
 
                        if l and words[0] == "Material" :
63
 
                                idx += 1        
64
 
                                self.writeMaterials(i, idx, mat_list, tex_list)
65
 
                
66
 
                        
67
 
                
68
 
                nr_fac_mat = 0          
69
 
                #Assign Materials               
70
 
                for line_m in lines:
71
 
                        l = line_m.strip()
72
 
                        words = line_m.split()                                                                                                                                                                                                                                                                                                                                                                                                  
73
 
                        if l and words[0] == "MeshMaterialList" :
74
 
                                nr_fac_mat = lines.index(line_m)        + 2
75
 
                                
76
 
                #Create The Mesh                                                                
77
 
                for line in lines:
 
57
                mesh_line_indicies = []
 
58
                for j, line in enumerate(lines):
78
59
                        l = line.strip()
79
 
                        words = line.split()                                                                                                                                                                                                                                                                                                                                                                                                    
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)
83
 
                                
84
 
                                
85
 
                NMesh.PutRaw(mesh,"Mesh",1)
86
 
                mesh.setMaterials(mat_list)
87
 
                mesh.update()
88
 
                        
89
 
                if nr_fac_mat :
90
 
                        self.writeMeshMaterials(nr_fac_mat, mesh)
91
 
                                
 
60
                        words = line.split()
 
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
 
66
                                #nr_uv_ind = j
 
67
                        elif words[0] == "MeshMaterialList" :
 
68
                                context_indicies["MeshMaterialList"] = j+2
 
69
                                #nr_fac_mat = j + 2
 
70
                        elif words[0] == "Mesh": # Avoid a second loop
 
71
                                context_indicies = mesh_indicies[j] = {'MeshTextureCoords':0, 'MeshMaterialList':0}
 
72
                
 
73
                for mesh_index, value in mesh_indicies.iteritems():
 
74
                        mesh = Mesh.New()
 
75
                        self.loadVertices(mesh_index, mesh, value['MeshTextureCoords'], value['MeshMaterialList'], tex_list)
 
76
                        
 
77
                        mesh.materials = mat_list[:16]
 
78
                        if value['MeshMaterialList']:
 
79
                                self.loadMeshMaterials(value['MeshMaterialList'], mesh)
 
80
                        scene.objects.new(mesh)
 
81
                        
92
82
                self.file.close()
93
83
                print "... finished"
94
 
                
 
84
 
95
85
        #------------------------------------------------------------------------------
96
86
        #        CREATE THE MESH
97
87
        #------------------------------------------------------------------------------
98
 
        def writeVertices(self, nr_vr_ind, mesh, nr_uv, nr_fac_mat, tex_list):
99
 
                
100
 
                lin = self.lines[nr_vr_ind + 1]
 
88
        def loadVertices(self, nr_vr_ind, mesh, nr_uv, nr_fac_mat, tex_list):
 
89
                v_ind = nr_vr_ind + 1
 
90
                lin = self.lines[v_ind]
101
91
                if lin :
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)
105
94
                else :
106
 
                        lin = self.lines.index(nr_vr_ind + 2)
107
 
                        lin_c = self.CleanLine(lin)                                                                                                                                                                     
 
95
                        v_ind = nr_vr_ind + 2
 
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)
110
 
                                                                                                                                                                        
111
 
                vx_array = range(v_ind + 1, (v_ind + nr_vert +1))
 
99
 
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]
114
103
                if lin_f :
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)
118
106
                else :
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)     
123
 
                                                                                                                                                
124
 
                                                                                                                                                                                                                                                                                                                                                                                                                        
125
 
                fac_array = range(nr_fac_li + 1, (nr_fac_li + nr_face + 1))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
126
 
                #Get Coordinates                                                                                                        
127
 
                for l in vx_array:
 
111
 
 
112
                #Get Coordinates
 
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)
136
 
                                mesh.verts.append(v) 
137
 
                                                                                                                                                                                                                                                                                                                                                                
138
 
                
139
 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
 
119
                                verts_list.append((float(words[0]),float(words[1]),float(words[2])))
 
120
                
 
121
                mesh.verts.extend(verts_list)
 
122
                del verts_list
 
123
                
 
124
                face_list = []
140
125
                #Make Faces
141
126
                i = 0
142
 
                for f in fac_array:
 
127
                mesh_verts = mesh.verts
 
128
                for f in xrange(nr_fac_li + 1, (nr_fac_li + nr_face + 1)):
143
129
                        i += 1
144
130
                        line_f = self.lines[f]
145
131
                        lin_f = self.CleanLine(line_f)
146
 
                        words = lin_f.split()                                                                                                                                                                                                                                                                                                   
147
 
                        if len(words) == 5:                                                                                                                                                                     
148
 
                                f=NMesh.Face() 
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])])
153
 
                                mesh.faces.append(f)
154
 
                                if nr_uv :
155
 
                                        uv = []
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]))
161
 
                                        uv.append(uv_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]))
167
 
                                        uv.append(uv_2)
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]))
173
 
                                        uv.append(uv_3)
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]))
179
 
                                        uv.append(uv_4)
180
 
                                        #---------------------------------
181
 
                                        f.uv = uv
182
 
                                        if nr_fac_mat :
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]
187
 
                                                if name_tex :
188
 
                                                        name_file = Blender.sys.join(my_path,name_tex)
189
 
                                                        try:
190
 
                                                                img = Image.Load(name_file)
191
 
                                                                f.image = img
192
 
                                                        except:
193
 
                                                                #Draw.PupMenu("No image to load")
194
 
                                                                #print "No image " + name_tex + " to load"
195
 
                                                                pass
196
 
                                        
197
 
                        elif len(words) == 4:                                                                                                                                                   
198
 
                                f=NMesh.Face() 
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])])
202
 
                                mesh.faces.append(f)
203
 
                                if nr_uv :
204
 
                                        uv = []
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]))
210
 
                                        uv.append(uv_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]))
216
 
                                        uv.append(uv_2)
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]))
222
 
                                        uv.append(uv_3)
223
 
                                        #---------------------------------
224
 
                                        f.uv = uv
225
 
                                        if nr_fac_mat :
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]
230
 
                                                if name_tex :
231
 
                                                        name_file = Blender.sys.join(my_path ,name_tex)
232
 
                                                        try:
233
 
                                                                img = Image.Load(name_file)
234
 
                                                                f.image = img
235
 
                                                        except:
236
 
                                                                #Draw.PupMenu("No image to load")
237
 
                                                                #print "No image " + name_tex + " to load"
238
 
                                                                pass
239
 
                                                                
240
 
                                
241
 
        
 
132
                        
 
133
                        # +1 for dummy vert only!
 
134
                        words = lin_f.split()
 
135
                        if len(words) == 5:
 
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])))
 
139
                
 
140
                mesh.faces.extend(face_list)
 
141
                del face_list
 
142
                
 
143
                if nr_uv :
 
144
                        mesh.faceUV = True
 
145
                        for f in mesh.faces:
 
146
                                fuv = f.uv
 
147
                                for ii, v in enumerate(f):
 
148
                                        # _u, _v = self.CleanLine(self.lines[nr_uv + 2 + v.index]).split()
 
149
                                        
 
150
                                        # Use a dummy vert
 
151
                                        _u, _v = self.CleanLine(self.lines[nr_uv + 1 + v.index]).split()
 
152
                                        
 
153
                                        fuv[ii].x = float(_u)
 
154
                                        fuv[ii].y = float(_v)
 
155
                        
 
156
                                if nr_fac_mat :
 
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]
 
161
                                        
 
162
                # remove dummy vert
 
163
                mesh.verts.delete([0,])
242
164
                
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("'", " ")
250
 
                return fix_5_line
251
 
        
 
166
                return line.replace(\
 
167
                        ";", " ").replace(\
 
168
                        '"', ' ').replace(\
 
169
                        "{", " ").replace(\
 
170
                        "}", " ").replace(\
 
171
                        ",", " ").replace(\
 
172
                        "'", " ")
 
173
 
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):
 
178
                
 
179
                def load_image(name):
 
180
                        try:
 
181
                                return Image.Load(Blender.sys.join(my_path,name))
 
182
                        except:
 
183
                                return None
 
184
                
 
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()
267
195
                
268
196
                if tex_n and tex_n[0] == "TextureFilename" :
269
 
                        
 
197
 
270
198
                        if len(tex_n) > 1:
271
 
                                tex_list.append(tex_n[1])
272
 
                                
 
199
                                tex_list.append(load_image(tex_n[1]))
 
200
 
273
201
                        if len(tex_n) <= 1 :
274
 
                                
 
202
 
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]))
279
207
                else :
280
 
                        tex_name = None
281
 
                        tex_list.append(tex_name)
282
 
                        
 
208
                        tex_list.append(None) # no texture for this index
 
209
 
283
210
                return mat_list, tex_list
284
211
        #------------------------------------------------------------------
285
212
        # SET MATERIALS
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:
289
216
                        nr_fc_mat += 1
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
295
 
                mesh.update()   
296
 
                
297
 
                
298
 
                                
 
221
                        face.mat = mat_idx
 
222
 
299
223
#------------------------------------------------------------------
300
224
#  MAIN
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)
305
229
        ximport.Import()
306
230
 
307
231
arg = __script__['arg']
308
 
Blender.Window.FileSelector(my_callback, "Import DirectX")      
309
 
        
 
 
b'\\ No newline at end of file'
 
232
 
 
233
if __name__ == '__main__':
 
234
        Blender.Window.FileSelector(my_callback, "Import DirectX", "*.x")
 
235
 
 
236
#my_callback('/fe/x/directxterrain.x')
 
237
#my_callback('/fe/x/Male_Normal_MAX.X')
 
238
#my_callback('/fe/x/male_ms3d.x')