#!BPY
"""
Name: 'Exporter selection faces'
Blender: 246
Group: 'Mesh'
Tooltip: 'Ecrit les faces du maillage sélectionné dans un fichier lstf'
"""

__author__ = "VVPix"
#__url__ = ("www.vvpix.com/bld_Index.htm")
__version__ = "1.00"

# v1.00 : première version

__bpydoc__ = """\
Ce script écrit la liste des numéros de sommets dans un fichier.
"""

# ***** 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 *****

import Blender
from Blender import Draw, Window, Scene, Mesh, sys, Object, Scene


def write( filename ):
	"""Ecrit les numeros de faces selectionnes dans un fichier 'lstf'"""
	print "--- debut du script ----"	
	bEditMode = Window.EditMode()
	if bEditMode: Window.EditMode(0)

	# Récupération de la liste des maillages
	print "Liste des maillages de la scene :"
	listeMaillages = Mesh.Get()
	for maillage in listeMaillages:
		print type(maillage)
		print "- %s" % maillage
	
	# Récupération de l'objet actif
	scene = Scene.GetCurrent()
	act_obj = scene.objects.active
	maillage = act_obj.getData( mesh = True )
	
	print "\nLe maillage actif est \"%s\" " % maillage.name
	
	faces = maillage.faces
	# Ouverture du fichier de sortie
	fic = open( filename, "w") 
	# Parcours de la liste des faces
	print "\nListe des faces selectionnees"
	for f in faces:
		if f.sel:
			print "index : %d" %(f.index)
			fic.write('%d\n'%(f.index))
	# Fermeture
	fic.close()
	
	Window.EditMode( bEditMode )# rétablissement du mode initial
	
	strM = "Information %t|"
	strM += "Consultez le fichier \"%s\" pour lire la liste des faces"%(filename) 
	Draw.PupMenu( strM )


def fs_callback( filename ):
	"""Sortie du dialogue de choix du fichier"""
	if not filename.lower().endswith('.lstf'): filename += '.lstf'
	write( filename )

Blender.Window.FileSelector(fs_callback, "Export lstf", Blender.sys.makename(ext='.lstf'))

