#!BPY """ Name: 'PLY...' Blender: 232 Group: 'Import' Tooltip: 'Import PLY Polygon File Format (*.ply)' """ ##################################################################### # # # PLY - Polygon file import # # aka the Stanford Triangle Format # # as found on http://astronomy.swin.edu/~pbourke/geomformats/ply # # # # (C) Februari 2004 Wim Van Hoydonck # # email: tuinbels@hotmail.com # # # # Released under the Blender Artistic Licence (BAL) # # see www.blender.org # # # # Works in Blender 2.32 and higher # # # # this script can be found online at: # # http://users.pandora.be/tuinbels/scripts/PLY_import.py # # # ##################################################################### import Blender from Blender import * import string#, time def read(filename): # start = time.clock() Window.DrawProgressBar(0, "start processing file") file = open(filename, "rb") lines = file.readlines() # determine if file is a valid ascii 1.0 PLY file line0 = lines[0].split() line10, line11, line12 = lines[1].split() if line0[0] == 'ply' and line10 == 'format': if line11 == "binary_big_endian" or line11 == "binary_small_endian": print "this is a binary ply file, not an ascii file" elif line11 == "ascii" and line12 == "1.0": print "valid ascii 1.0 PLY file" # find info about the verts and faces in file header for i in range(len(lines)): spl = lines[i].split() if spl[0] == "element" and spl[1] == "vertex": totverts, indxV = int(spl[-1]), i if spl[0] == "element" and spl[1] == "face": totfaces, indxF = int(spl[-1]), i if spl[0] == "end_header": endhead = int(i) break xcoo, ycoo, zcoo, vert_indx = 0, 0, 0, 0 xcooi, ycooi, zcooi, vert_indxi = 0, 0, 0, 0 for i in range(indxV+1,indxF+1): prop = lines[i].split() if prop[-1] == "x": xcoo += 1 if prop[-1] == "y": ycoo += 1 if prop[-1] == "z": zcoo += 1 if prop[-1] == "vertex_indices": vert_indx +=1 break # for the moment only the vertex coordinates are supported ... vertcoords = [] for i in range((endhead+1), (endhead+1+totverts)): v_coor = lines[i].split() coor = [float(v_coor[0]), float(v_coor[1]), float(v_coor[2])] vertcoords.append(coor) lenfa, fac = [], [] for i in range((endhead+totverts+1), endhead+totverts+totfaces+1): f_ind = lines[i].split() # length of the current face lenfa.append(int(f_ind[0])) # triangles if int(f_ind[0]) == 3: fa = [int(f_ind[1]), int(f_ind[2]), int(f_ind[3])] fac.append(fa) # quads if int(f_ind[0]) == 4: fa = [int(f_ind[1]), int(f_ind[2]), int(f_ind[3]), int(f_ind[4])] fac.append(fa) file.close() Window.DrawProgressBar(0.4, "file processed") objname = Blender.sys.splitext(Blender.sys.basename(filename))[0] m = NMesh.GetRaw() # add vertices to mesh for i in range(0,totverts): v = NMesh.Vert(vertcoords[i][0], vertcoords[i][1], vertcoords[i][2]) m.verts.append(v) Window.DrawProgressBar(0.6, "verts created") # make faces from verts for i in range(0, totfaces): f = NMesh.Face() for j in range(0, len(fac[i])): f.append(m.verts[fac[i][j]]) f.smooth = 1 m.faces.append(f) obj = NMesh.PutRaw(m, objname, 1) # end = time.clock() print "" print objname, "with", totfaces, "faces and", totverts, "verts successfully imported"# in", end-start, "seconds" print "" Window.DrawProgressBar(1, "done") def fs_callback(filename): read(filename) Blender.Window.FileSelector(fs_callback, "PLY import")