#!BPY """ Name: 'GMesh file (.pos)...' Blender: 241 Group: 'Import' Tooltip: 'Load a GMesh POS File.' """ __author__= "Jiri Hnidek" __url__= ["blender", "blenderartist"] __version__= "0.2" __bpydoc__= """\ This script imports GMesh pos 1.0 files to Blender. Usage: Run this script from "File->Import" menu and then load the desired OBJ file. """ # $Id:$ # # -------------------------------------------------------------------------- # POS GMESH Import v1.0 by Jiri Hnidek # -------------------------------------------------------------------------- # ***** BEGIN GPL LICENSE BLOCK ***** # # 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. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # ***** END GPL LICENCE BLOCK ***** # -------------------------------------------------------------------------- from Blender import * import math vertList = {} # list of vertexes faceList = {} # list of faces mesh = {} # list of mesh data object = {} # list of objects min_max = {} # list of min max values name = "" def hsv_to_rgb(h, s, v): """Converts hsv color to rgb color""" f = p = q = t = 0.0 if h>1.0: h = 1.0 elif h<0.0: h = 0.0 h = 360.0*h if s==0: r = v g = v b = v else: if h==360.0: h = 0.0 h = h/60.0 i = int(h) f = h - float(i) p = v*(1.0 - s) q = v*(1.0 - (s*f)) t = v*(1.0 - (s*(1.0 - f))) if i==0: r = v g = t b = p elif i==1: r = q g = v b = p elif i==2: r = p g = v b = t elif i==3: r = p g = q b = v elif i==4: r = t g = p b = v elif i==5: r = v g = p b = q return r, g, b def add_vert(x, y, z): """Add vertex to list of vertexes""" global vertList global mesh global name try: v = vertList[x, y, z] except: v = NMesh.Vert(x, y, z) vertList[x, y, z] = v mesh[name].verts.append(v) return v def add_triangle(v1, v2, v3, min, max, col1, col2, col3): """It adds triangle to list of faces""" global faceList global mesh global name try: f = faceList[v1, v2, v3] except: f = NMesh.Face() f.v.append(v1) col1 = (float(col1)-min)/(max-min) r, g, b = hsv_to_rgb(col1, 1.0, 1.0) c1 = NMesh.Col(int(255.0*r), int(255.0*g), int(255.0*b)) f.col.append(c1) f.v.append(v2) col2 = (float(col2)-min)/(max-min) r, g, b = hsv_to_rgb(col2, 1.0, 1.0) c2 = NMesh.Col(int(255.0*r), int(255.0*g), int(255.0*b)) f.col.append(c2) f.v.append(v3) col3 = (float(col3)-min)/(max-min) r, g, b = hsv_to_rgb(col3, 1.0, 1.0) c3 = NMesh.Col(int(255.0*r), int(255.0*g), int(255.0*b)) f.col.append(c3) faceList[v1, v2, v3] = f mesh[name].faces.append(f) def add_scalar_triangle(x1, y1, z1, x2, y2, z2, x3, y3, z3, min, max, c1, c2, c3): """It adds triangle to list of faces""" v1 = add_vert(float(x1), float(y1), float(z1)) v2 = add_vert(float(x2), float(y2), float(z2)) v3 = add_vert(float(x3), float(y3), float(z3)) add_triangle(v1, v2, v3, min, max, c1, c2, c3) def add_scalar_simplex(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, min, max, c1, c2, c3, c4): """It adds all faces of simplex (tetrahedron) to list of faces""" v1 = add_vert(float(x1), float(y1), float(z1)) v2 = add_vert(float(x2), float(y2), float(z2)) v3 = add_vert(float(x3), float(y3), float(z3)) v4 = add_vert(float(x4), float(y4), float(z4)) add_triangle(v1, v2, v3, min, max, c1, c2, c3) add_triangle(v1, v2, v4, min, max, c1, c2, c4) add_triangle(v2, v3, v4, min, max, c2, c3, c4) add_triangle(v3, v1, v4, min, max, c3, c1, c4) def add_vector_point(x, y, z, vx, vy, vz, min, max): """It adds vector (line) to list of vectors""" global mesh global name nx = float(vx) ny = float(vy) nz = float(vz) n = math.sqrt(nx*nx + ny*ny + nz*nz) nx = nx/n ny = ny/n nz = nz/n n = (n-min)/(max-min) f = NMesh.Face() v1 = add_vert(float(x), float(y), float(z)) v2 = add_vert(float(x)+nx, float(y)+ny, float(z)+nz) r, g, b = hsv_to_rgb(n/360.0, 1.0, 1.0) c = NMesh.Col(int(255.0*r), int(255.0*g), int(255.0*b)) f.col.append(c) f.v.append(v1) f.col.append(c) f.v.append(v2) f.col.append(c) f.v.append(v1) mesh[name].faces.append(f) def find_min_max(min, max, list): """Find min and max value in list of floats""" for item in list: if itemmax: max = item return min, max def find_all_min_max(fileLines): """Find min and max values""" global name global min_max lIdx = 0 while lIdx < len(fileLines): l = fileLines[lIdx].split() if len(l)==0: continue elif l[0]=='View': name="" end_name = 0 start_name = 0 for string in l: if string[0]=="\"" and len(name)==0 and start_name==0: name = name + string[1:] start_name = 1 elif string[-1]=="\"" and len(name)>0 and end_name==0 and start_name==1: name = name + string[:-1] end_name = 1 elif end_name==0 and start_name==1: name = name + string elif l[0]=='ST': tmp = l[19] try: min = min_max[name+'_ST_min'] except: min = 1.7e38 try: max = min_max[name+'_ST_max'] except: max = -1.7e38 min_max[name+'_ST_min'], min_max[name+'_ST_max'] = find_min_max(min, max, [float(tmp[1:]), float(l[21]), float(l[23])]) elif l[0]=='SS': tmp = l[25] try: min = min_max[name+'_SS_min'] except: min = 1.7e38 try: max = min_max[name+'_SS_max'] except: max = -1.7e38 min_max[name+'_SS_min'], min_max[name+'_SS_max'] = find_min_max(min, max, [float(tmp[1:]), float(l[27]), float(l[29]), float(l[31])]) elif l[0]=='VP': tmp = l[7] nx = float(tmp[1:]) ny = float(l[9]) nz = float(l[11]) n = math.sqrt(nx*nx + ny*ny + nz*nz) try: max = min_max[name+'_VP_max'] except: max = -1.7e38 try: min = min_max[name+'_VP_min'] except: min = 1.7e38 min_max[name+'_VP_min'], min_max[name+'_VP_max'] = find_min_max(min, max, [n]) lIdx+=1 def load_pos(file): """load data from some simulation from file .pos""" global vertList global faceList global Mesh global object global name print file # read all lines to buffer try: tempFile = open(file, 'r') except: exit fileLines = tempFile.readlines() tempFile.close() del tempFile find_all_min_max(fileLines) scene = Scene.getCurrent() lIdx = 0 # parse all buffered lines while lIdx < len(fileLines): l= fileLines[lIdx].split() if len(l) == 0: continue elif l[0] == 'View': name = "" end_name = 0 start_name = 0 for string in l: if string[0]=="\"" and len(name)==0 and start_name==0: name = name + string[1:] start_name = 1 elif string[-1]=="\"" and len(name)>0 and end_name==0 and start_name==1: name = name + string[:-1] end_name = 1 elif end_name==0 and start_name==1: name = name + string print name mesh[name] = NMesh.New(name) mesh[name].hasVertexColours(1) vertList = {} faceList = {} elif l[0] == 'ST': tmp1 = l[1] tmp2 = l[19] add_scalar_triangle(tmp1[1:], l[3], l[5], l[7], l[9], l[11], l[13], l[15], l[17], min_max[name+'_ST_min'], min_max[name+'_ST_max'], tmp2[1:], l[21], l[23]) elif l[0] == 'SS': tmp1 = l[1] tmp2 = l[25] add_scalar_simplex(tmp1[1:], l[3], l[5], l[7], l[9], l[11], l[13], l[15], l[17], l[19], l[21], l[23], min_max[name+'_SS_min'], min_max[name+'_SS_max'],tmp2[1:], l[27], l[29], l[31]) elif l[0] == 'VP': tmp1 = l[1] tmp2 = l[7] add_vector_point(tmp1[1:], l[3], l[5], tmp2[1:], l[9], l[11], min_max[name+'_VP_min'], min_max[name+'_VP_max']) elif l[0] == '};': object[name] = Object.New('Mesh') object[name].setName(name) object[name].link(mesh[name]) scene.link(object[name]) lIdx+=1 Window.RedrawAll() def main(): Window.FileSelector(load_pos, 'Import a GMesh pos', '*.pos') if __name__ == '__main__': main()