Merge branch 'master' of ssh://swordfish/srv/www/htdocs/git/engrid
[engrid.git] / src / scripts / gmsh2.py
blob8852d4591498c5f15945b218569ec2a31292bb2d
1 #!BPY
3 # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 # + +
5 # + This file is part of enGrid. +
6 # + +
7 # + Copyright 2008,2009 Oliver Gloth +
8 # + +
9 # + enGrid is free software: you can redistribute it and/or modify +
10 # + it under the terms of the GNU General Public License as published by +
11 # + the Free Software Foundation, either version 3 of the License, or +
12 # + (at your option) any later version. +
13 # + +
14 # + enGrid is distributed in the hope that it will be useful, +
15 # + but WITHOUT ANY WARRANTY; without even the implied warranty of +
16 # + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
17 # + GNU General Public License for more details. +
18 # + +
19 # + You should have received a copy of the GNU General Public License +
20 # + along with enGrid. If not, see <http:#www.gnu.org/licenses/>. +
21 # + +
22 # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24 # DESCRIPTION:
25 # Export script for Blender.
27 """
28 Name: 'Gmsh2'
29 Blender: 249
30 Group: 'Export'
31 Tooltip: 'Export to Gmsh 2 ASCII format'
32 """
34 import Blender
35 import bpy
37 def writeGmsh2(filename):
38 if not filename.lower().endswith('.msh'):
39 filename += '.msh'
40 out = file(filename, "w")
41 scn = bpy.data.scenes.active
42 object = scn.objects.active
43 if not object:
44 Blender.Draw.PupMenu('Error%t|Select 1 active object')
45 return
46 if object.type != 'Mesh':
47 Blender.Draw.PupMenu('Error%t|Select a mesh object')
48 return
50 mesh = object.getData(0,1)
51 faces = mesh.faces
52 nodes = mesh.verts
54 out.write('$MeshFormat\n2 0 8\n$EndMeshFormat\n')
55 out.write('$Nodes\n%d\n' % len(nodes))
57 i = 1
58 for n in nodes:
59 out.write("%d " % i)
60 out.write("%e " % n.co[0])
61 out.write("%e " % n.co[1])
62 out.write("%e\n" % n.co[2])
63 i = i + 1
65 out.write('$EndNodes\n')
66 out.write('$Elements\n%d\n' % len(faces))
67 i = 1
68 for f in faces:
69 out.write("%d " % i)
70 N = len(f.verts)
71 if N < 3 and N > 4:
72 Blender.Draw.PupMenu('Error%t|Only triangles and quads allowed')
73 return
74 if N == 3:
75 out.write('2 ')
76 if N == 4:
77 out.write('3 ')
78 out.write('1 %d' % (f.mat + 1))
79 for v in f.verts:
80 out.write(' %d' % (v.index + 1))
81 out.write('\n')
82 i = i + 1
84 out.write('$EndElements\n')
86 Blender.Window.FileSelector(writeGmsh2, "Export", Blender.sys.makename(ext='.msh'))