#!BPY """ Name: 'GMesh file (.msh)...' Blender: 241 Group: 'Import' Tooltip: 'Load a GMesh MSH File.' """ __author__= "Jiri Hnidek" __url__= ["blender", "blenderartist"] __version__= "0.2" __bpydoc__= """\ This script imports GMesh msh 1.0 and 2.0 files to Blender. Usage: Run this script from "File->Import" menu and then load the desired OBJ file. """ # $Id:$ # # -------------------------------------------------------------------------- # MSH 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 l = "" # current line vertList = {} # list of vertexes surfaceVertList = {} # list of vertexes for surface volumeVertList = {} # list of vertexes fot volume surfaceFaceList = {} # list of surface faces volumeFaceList = {} # list of volume faces mtrList = {} # list of materials Mesh = {} # list of meshes 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 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 add_surface_vert(index): """Add and return vertex to list of surface vertexes""" global vertList global surfaceVertList global Mesh try: v = surfaceVertList[index] except: v = surfaceVertList[index] = vertList[index] Mesh['surface'].verts.append(v) return v def add_volume_vert(index): """Add and return vertex to list of volume vertexes""" global vertList global volumeVertList global Mesh try: v = volumeVertList[index] except: v = volumeVertList[index] = vertList[index] Mesh['volume'].verts.append(v) return v def add_surface_face(v1, v2, v3, v4=-1): """It adds face to list of surface faces""" global vertList global surfaceFaceList global Mesh if Mesh.has_key('surface')==False: Mesh['surface'] = NMesh.New('surface') Mesh['surface'].hasVertexColours(1) f = NMesh.Face() vert = add_surface_vert(v1) f.v.append(vert) vert = add_surface_vert(v2) f.v.append(vert) vert = add_surface_vert(v3) f.v.append(vert) if v4==-1: surfaceFaceList[v1, v2, v3] = f; else: vert = add_surface_vert(v4) f.v.append(vert) surfaceFaceList[v1, v2, v3, v4] = f; Mesh['surface'].faces.append(f) def add_volume_face(tag, v1, v2, v3, v4=-1): """Adds face to list of volume faces""" global volumeFaceList global Mesh global mtrList if Mesh.has_key('volume')==False: Mesh['volume'] = NMesh.New('surface') Mesh['volume'].hasVertexColours(1) try: if v4==-1: f = volumeFaceList(v1, v2, v3) else: f = volumeFaceList(v1, v2, v3, v4) except: f=NMesh.Face() col = mtrList[tag] vert = add_volume_vert(v1) f.col.append(col) f.v.append(vert) vert = add_volume_vert(v2) f.col.append(col) f.v.append(vert) vert = add_volume_vert(v3) f.col.append(col) f.v.append(vert) if v4==-1: volumeFaceList[v1, v2, v3] = f; else: vert = add_volume_vert(v4) f.v.append(vert) f.col.append(col) volumeFaceList[v1, v2, v3, v4] = f; Mesh['volume'].faces.append(f) def load_mtr(file): """Load materials from .mtr file""" global mtrList file = file+'.mtr' # read all lines of material file to buffer try: tempFile = open(file, 'r') except: print 'not able to read file'+file+'.mtr' return False fileLines = tempFile.readlines() tempFile.close() del tempFile mode = 'none' min = 1.7e38 max = -1.7e38 lIdx = 0 #parse all buffered lines while lIdx0: if int(l[1])==21 or int(l[1])==31 or int(l[1])==-31: mtrList[int(l[0])] = [math.log10(float(l[2]))] min, max = find_min_max(min, max, mtrList[int(l[0])]) elif int(l[1])==22 or int(l[1])==-22: mtrList[int(l[0])] = [math.log10(float(l[2])), math.log10(float(l[3]))] min, max = find_min_max(min, max, mtrList[int(l[0])]) elif int(l[1])==33 or int(l[1])==-33: mtrList[int(l[0])] = [math.log10(float(l[2])), math.log10(float(l[3])), math.log10(float(l[4]))] min, max = find_min_max(min, max, mtrList[int(l[0])]) else: print 'count of materials '+l[0] mid += 1 lIdx += 1 mtrItems = mtrList.items() print mtrItems for mat in mtrItems: if len(mat[1])==1: val = 255*(mat[1][0] - min)/(max - min) r, g, b = hsv_to_rgb(mat[1][0], 1.0, 1.0) col = NMesh.Col(255*int(r), 255*int(g), 255*int(b), int(val)) mtrList[int(mat[0])] = col elif len(mat[1])==3: val1 = mat[1][0] val2 = mat[1][1] val3 = mat[1][2] val = (val1 + val2 + val3)/3 r, g, b = hsv_to_rgb(val, 1.0, 1.0) val = 255*(val - min)/(max - min) col = NMesh.Col(255*int(r), 255*int(g), 255*int(b), int(val)) mtrList[int(mat[0])] = col print mtrList return True def load_msh(file): """Loads mesh from .msh file""" global vertList print file # try to load material from .mtr file mtr = load_mtr(file[:-4]) # init mode = 'none' elem_type = 'none' # read all lines to buffer tempFile = open(file, 'r') fileLines = tempFile.readlines() tempFile.close() del tempFile lIdx = 0 # parse all buffered lines while lIdx < len(fileLines): l= fileLines[lIdx].split() if len(l) == 0: continue elif l[0] == '$NOD': print "$NOD" mode = "node" nid = 0 elif l[0] == "$ENDNOD": print "$ENDNOD" nid = 0 elif l[0] == "$ELM": print "$ELM" mode = "elem" tag1 = 2 v1 = 5 v2 = 6 v3 = 7 v4 = 8 v5 = 9 v6 = 10 v7 = 11 v8 = 12 eid = 0 elif l[0] == "$ENDELM": print "$ENDELM" eid = 0 elif l[0] == "$MeshFormat": print "$MeshFormat" elif l[0] == "$EndMeshFormat": print "$EndMeshFormar" elif l[0] == "$Nodes": print "$Nodes" mode = "nodes" nid = 0 elif l[0] == "$EndNodes": print "$EndNodes" nid = 0 elif l[0] == "$Elements": print "$Elements" mode = "elements" tag1 = 3 v1 = 6 v2 = 7 v3 = 8 v4 = 9 v5 = 10 v6 = 11 v7 = 12 v8 = 13 eid = 0 elif l[0] == "$EndElements": print "$EndElements" eid = 0 else: if mode == "node" or mode == "nodes": if nid>0: v = NMesh.Vert(float(l[1]), float(l[2]), float(l[3])) vertList[int(l[0])] = v else: print "count of vertexes:", l[0] nid+=1 elif mode == "elem" or mode == "elements": if eid>0: if l[1] == '1': elem_type = 'line' elif l[1] == '2': elem_type = 'triangle' vert1 = int(l[v1]) vert2 = int(l[v2]) vert3 = int(l[v3]) add_surface_face(vert1, vert2, vert3) elif l[1] == '3': elem_type = 'quat' vert1 = int(l[v1]) vert2 = int(l[v2]) vert3 = int(l[v3]) vert4 = int(l[v4]) add_surface_face(vert1, vert2, vert3, vert4) elif l[1] == '4': elem_type = 'tetrahedron' vert1 = int(l[v1]) vert2 = int(l[v2]) vert3 = int(l[v3]) vert4 = int(l[v4]) index = int(l[tag1]) add_volume_face(index, vert1, vert2, vert3) add_volume_face(index, vert1, vert2, vert4) add_volume_face(index, vert1, vert3, vert4) add_volume_face(index, vert2, vert3, vert4) elif l[1] == '5': elem_type = 'hexahedron' vert1 = int(l[v1]) vert2 = int(l[v2]) vert3 = int(l[v3]) vert4 = int(l[v4]) vert5 = int(l[v5]) vert6 = int(l[v6]) vert7 = int(l[v7]) vert8 = int(l[v8]) index = int(l[tag1]) add_volume_face(index, vert1, vert2, vert3, vert4) add_volume_face(index, vert1, vert2, vert6, vert5) add_volume_face(index, vert2, vert3, vert7, vert6) add_volume_face(index, vert3, vert4, vert8, vert7) add_volume_face(index, vert4, vert1, vert5, vert8) add_volume_face(index, vert5, vert6, vert7, vert8) elif l[1] == '6': elem_type = 'prism' elif l[1] == '7': elem_type = 'pyramid' else: print "count of elems:", l[0] eid+=1 lIdx+=1 scene = Scene.getCurrent() if Mesh.has_key('surface')==True: object = Object.New('Mesh') object.setName('surface') object.link(Mesh['surface']) scene.link(object) if Mesh.has_key('volume')==True: obj = Object.New('Mesh') obj.setName('volume') obj.link(Mesh['volume']) scene.link(obj) Window.RedrawAll() def main(): Window.FileSelector(load_msh, 'Import a GMesh msh', '*.msh') if __name__ == '__main__': main()