.smesh converter script


Here is another short script.  This one converts Blender mesh to .smesh format.  This format is the simplest which can be read by the finite element mesh generator Tetgen.  More here:  http://tetgen.berlios.de/  .   Output from Tetgen can be easily modified for use by the finite solver CCX.  Tetgen works in windows, so the work path Blender -> Tetgen -> CCX is open.  (However, post-processing in CGX, which would usually be desirable, is still only available on linux.)  This script has been tested, but not extensively.  Comments welcome.

-l

###########################
#!BPY



"""
Name: 'SMESH file format (.smesh)...'
Blender: 234
Group: 'Export'
Tooltip: 'Export selected mesh to SMESH format (*.smesh)'
"""

# --------------------------------------------------------------------------
#  SMESH Export v0.2 by Gary Bollenbach
# --------------------------------------------------------------------------
# ***** 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 LICENSE BLOCK *****
# --------------------------------------------------------------------------





#  Object(s) for export must 
#  be SELECTED.  If multiple 
#  objects are exported, they
#  must first be JOINED.





import Blender


# ==============================
# ====== Write .smesh Format ======
# ==============================


def write(filename):
	file = open(filename, "wb")
	objects = Blender.Object.GetSelected()
	objname = objects[0].name
	meshname = objects[0].data.name
	mesh = Blender.NMesh.GetRaw(meshname)
	obj = Blender.Object.Get(objname)

	# === FBD Header ===
	file.write("# .smesh output from Blender
")

	# === number of vertices, dimensions, marker codes
	file.write("%d %d %d %d
" % (len(mesh.verts), 3, 0, 0))

	# === Vertex List ===
	for i in range(len(mesh.verts)):
		x, y, z = mesh.verts[i].co
		lnu = str(i+1)
		file.write(lnu)
		file.write(" %f %f %f
" % (x, y, z))
		
	# === Number of faces, number of boundary markers
	file.write("%d %d
" % (len(mesh.faces), 0))

	# === Face List ===
	for i in range(len(mesh.faces)):
		file.write(`len(mesh.faces[i].v)`+' ')
		for j in range(len(mesh.faces[i].v)):
			t = ((mesh.faces[i].v[j].index) +1)
			file.write(`t`+' ')
		file.write("
")

	# === Number of holes, number of regions
	file.write("0
")
	file.write("0
")
	file.close()

def fs_callback(filename):
	write(filename +'.smesh')

Blender.Window.FileSelector(fs_callback, "Export smesh")