#!BPY """ Registration info for Blender menus: Name: 'DirectX(.x)...' Blender: 236 Group: 'Import' Tip: 'Import from DirectX text file format format.' """ # DirectXImporter.py version 1.0 # Copyright (C) 2005 Arben OMARI -- omariarben@everyday.com # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # This script import meshes from DirectX text file format # Grab the latest version here :www.omariben.too.it import Blender from Blender import NMesh,Object class xImport: def __init__(self, filename): self.file = open(filename, "r") self.lines = self.file.readlines() def Import(self): lines = self.lines print "importing into Blender ..." scene = Blender.Scene.getCurrent() mesh = NMesh.GetRaw() for line in lines: l = line.strip() words = line.split() if l and words[0] == "Mesh" : #check for "Mesh" word nr_vr_ind = lines.index(line) #get it's line number index print nr_vr_ind, "Mesh" self.writeVertices(nr_vr_ind,mesh) #go to write vertices NMesh.PutRaw(mesh,"Mesh",1) self.file.close() print "... finished" def writeVertices(self, nr_vr_ind, mesh): vx_nr_line = self.checkNumVert(nr_vr_ind) print vx_nr_line nr_vert = int((vx_nr_line.split()[0])[:-1]) v_ind = self.lines.index(vx_nr_line) print v_ind ,"Numero vertici" vx_array = range(v_ind + 1, (v_ind + nr_vert +1)) print vx_array[0],vx_array[-1], "prime e ultime coordinate" nr_fac_line = self.checkNumFace(v_ind + nr_vert +1) nr_fac_li = self.lines.index(nr_fac_line) print nr_fac_li, "numero facce" nr_face = int((nr_fac_line.split()[0])[:-1]) fac_array = range(nr_fac_li + 1, (nr_fac_li + nr_face + 1)) print fac_array[0], fac_array[-1], "prime e ultime facce" print "--------------------------------------------------" #Get Coordinates for l in vx_array: line = self.lines[l] fix_line = line.replace(";", " ") fixed_line = fix_line.replace(",", " ") words = fixed_line.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) #Make Faces for f in fac_array: line = self.lines[f] fix_line = line.replace(";", " ") fixed_line = fix_line.replace(",", " ") words = fixed_line.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])]) mesh.faces.append(f) 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])]) mesh.faces.append(f) def checkNumVert(self, ind): #check the first line after "Mesh" line_1 = self.lines[ind+1] words_1 = line_1.split() l_1 = line_1.strip() #check the second line after "Mesh" line_2 = self.lines[ind+2] words_2 = line_2.split() l_2 = line_2.strip() if l_1: return line_1 if l_2: return line_2 def checkNumFace(self, indx): line_1 = self.lines[indx] words_1 = line_1.split() l_1 = line_1.strip() line_2 = self.lines[indx + 1] words_2 = line_2.split() l_2 = line_2.strip() if l_1 : return line_1 if l_2: return line_2 def my_callback(filename): if not filename.find('.x', -2): print "Not an .x file" ximport = xImport(filename) ximport.Import() arg = __script__['arg'] Blender.Window.FileSelector(my_callback, "Import DirectX8")