2004-06-12 20:46:24 +00:00
#!BPY
"""
2007-04-27 17:19:26 +00:00
# Name: 'DirectX (.x)...'
# Blender: 242
# Group: 'Export'
# Tooltip: 'Export to DirectX text file format format for XNA Animation Component Library.'
"""
2008-06-23 00:21:49 +00:00
__author__ = " vertex color exporting feature is added by mnemoto (original:minahito (original:Arben (Ben) Omari)) "
2008-10-21 09:08:46 +00:00
__url__ = ( " blender.org " , " blenderartists.org " , " Adjuster ' s site http://sunday-lab.blogspot.com/, Author ' s site http://www.omariben.too.it " , " Adjuster ' s site http://ex.homeunix.net/ " )
2008-06-23 00:21:49 +00:00
__version__ = " 3.1 "
2004-11-07 16:31:13 +00:00
__bpydoc__ = """ \
This script exports a Blender mesh with armature to DirectX 8 ' s text file
format .
Notes : < br >
Check author ' s site or the elYsiun forum for a new beta version of the
DX exporter .
"""
2006-03-11 10:45:48 +00:00
# DirectXExporter.py version 3.0
2006-01-29 19:17:53 +00:00
# Copyright (C) 2006 Arben OMARI -- omariarben@everyday.com
2004-06-12 20:46:24 +00:00
#
# 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.
# This script export meshes created with Blender in DirectX8 file format
# it exports meshes,armatures,materials,normals,texturecoords and animations
# Grab the latest version here :www.omariben.too.it
2007-04-27 17:19:26 +00:00
# [Notice]
# This script is the custom version of Mr.Arben Omari's great work.
# If you have a question about the adjusted part, visit http://sunday-lab.blogspot.com/.
2004-06-12 20:46:24 +00:00
import Blender
2006-03-11 10:45:48 +00:00
from Blender import Types , Object , NMesh , Material , Armature , Mesh
2004-06-12 20:46:24 +00:00
from Blender . Mathutils import *
2006-03-11 10:45:48 +00:00
from Blender import Draw , BGL
from Blender . BGL import *
2009-05-05 21:51:54 +00:00
try : import math
except : math = None
2004-06-12 20:46:24 +00:00
2006-01-29 19:17:53 +00:00
global mat_flip , index_list , space , bone_list , mat_dict
2006-03-29 06:22:43 +00:00
global anim , flip_norm , swap_zy , flip_z , speed , ticks , no_light , recalc_norm , Bl_norm
2006-01-29 19:17:53 +00:00
bone_list = [ ]
2004-07-27 03:13:45 +00:00
index_list = [ ]
2006-01-29 19:17:53 +00:00
mat_dict = { }
2006-03-11 10:45:48 +00:00
space = 0 ; flip_z = 1 ; anim = 0 ; swap_yz = 0 ; flip_norm = 0 ; speed = 0 ; ticks = 25
2006-03-29 06:22:43 +00:00
Bl_norm = 1 ; recalc_norm = 0 ; no_light = 0
2006-03-11 10:45:48 +00:00
toggle_val = 0
toggle1_val = 0
toggle2_val = 0
toggle3_val = 1
toggle4_val = 0
2006-03-29 06:22:43 +00:00
toggle5_val = 1
toggle6_val = 0
toggle7_val = 0
2006-03-11 10:45:48 +00:00
anim_tick = Draw . Create ( 25 )
2007-04-27 17:19:26 +00:00
#***********************************************
# DirectX file spec only allows letters, digits, and
# underscore in Names.
#***********************************************
def make_legal_name ( starting_name ) :
new_name = starting_name . replace ( ' . ' , ' _ ' )
new_name = new_name . replace ( ' ' , ' _ ' )
if new_name [ 0 ] . isdigit ( ) :
new_name = ' _ ' + new_name
return new_name
2006-03-11 10:45:48 +00:00
#***********************************************
# MAIN
#***********************************************
def my_callback ( filename ) :
if filename . find ( ' .x ' , - 2 ) < = 0 : filename + = ' .x '
xexport = xExport ( filename )
xexport . SelectObjs ( )
def my_callback_sel ( filename ) :
if filename . find ( ' .x ' , - 2 ) < = 0 : filename + = ' .x '
xexport = xExport ( filename )
xexport . exportSelMesh ( )
def event ( evt , val ) :
if evt == Draw . ESCKEY :
Draw . Exit ( )
return
def button_event ( evt ) :
2007-04-27 17:19:26 +00:00
global toggle_val , toggle1_val , toggle2_val , toggle3_val , toggle4_val , toggle5_val , toggle6_val , toggle7_val
2006-03-29 06:22:43 +00:00
global flip_z , swap_yz , flip_norm , anim , ticks , speed , no_light , Bl_norm , recalc_norm
2006-03-11 10:45:48 +00:00
arg = __script__ [ ' arg ' ]
2007-04-27 17:19:26 +00:00
if evt == 1 :
2006-03-11 10:45:48 +00:00
toggle_val = 1 - toggle_val
anim = toggle_val
Draw . Redraw ( 1 )
if evt == 2 :
toggle1_val = 1 - toggle1_val
flip_norm = toggle1_val
Draw . Redraw ( 1 )
if evt == 3 :
toggle2_val = 1 - toggle2_val
swap_yz = toggle2_val
Draw . Redraw ( 1 )
if evt == 4 :
toggle3_val = 1 - toggle3_val
flip_z = toggle3_val
Draw . Redraw ( 1 )
if evt == 5 :
toggle4_val = 1 - toggle4_val
speed = toggle4_val
Draw . Redraw ( 1 )
2006-03-29 06:22:43 +00:00
if evt == 10 :
toggle5_val = 1 - toggle5_val
if toggle5_val == 1 :
toggle6_val = 0
toggle7_val = 0
else :
toggle6_val = 1
toggle7_val = 1
no_light = toggle7_val
recalc_norm = toggle6_val
Bl_norm = toggle5_val
Draw . Redraw ( 1 )
if evt == 11 :
toggle6_val = 1 - toggle6_val
if toggle6_val == 1 :
toggle5_val = 0
toggle7_val = 0
else :
toggle5_val = 1
toggle7_val = 1
no_light = toggle7_val
recalc_norm = toggle6_val
Bl_norm = toggle5_val
Draw . Redraw ( 1 )
if evt == 12 :
toggle7_val = 1 - toggle7_val
if toggle7_val == 1 :
toggle6_val = 0
toggle5_val = 0
else :
toggle6_val = 1
toggle5_val = 1
no_light = toggle7_val
recalc_norm = toggle6_val
Bl_norm = toggle5_val
Draw . Redraw ( 1 )
2006-03-11 10:45:48 +00:00
if evt == 6 :
ticks = anim_tick . val
if evt == 7 :
fname = Blender . sys . makename ( ext = " .x " )
Blender . Window . FileSelector ( my_callback , " Export DirectX " , fname )
if evt == 8 :
fname = Blender . sys . makename ( ext = " .x " )
Blender . Window . FileSelector ( my_callback_sel , " Export DirectX " , fname )
if evt == 9 :
Draw . Exit ( )
def draw ( ) :
global animsg , flipmsg , swapmsg , anim_tick
2006-03-29 06:22:43 +00:00
global flip_z , swap_yz , flip_norm , anim , ticks , speed , recalc_norm , Bl_norm , no_light
2006-03-11 10:45:48 +00:00
glClearColor ( 0.55 , 0.6 , 0.6 , 1 )
glClear ( BGL . GL_COLOR_BUFFER_BIT )
#external box
glColor3f ( 0.2 , 0.3 , 0.3 )
rect ( 10 , 402 , 300 , 382 )
#--
#glColor3f(0.3,0.4,0.4)
#rect(11,399,298,398)
#--
glColor3f ( 0.5 , 0.75 , 0.65 )
rect ( 14 , 398 , 292 , 30 )
#--
glColor3f ( 0.5 , 0.75 , 0.65 )
rect ( 14 , 366 , 292 , 160 )
#--
glColor3f ( 0.5 , 0.75 , 0.65 )
rect ( 14 , 202 , 292 , 60 )
#--
glColor3f ( 0.5 , 0.75 , 0.65 )
rect ( 14 , 138 , 292 , 40 )
#--
glColor3f ( 0.5 , 0.75 , 0.65 )
rect ( 14 , 94 , 292 , 70 )
glColor3f ( 0.8 , .8 , 0.6 )
glRasterPos2i ( 20 , 380 )
Draw . Text ( " DirectX Exporter " , ' large ' )
Draw . Text ( " (for Blender 2.41) " , ' small ' )
#-------Aniamtion toggle---------------------------------------------
Draw . Toggle ( " Anim " , 1 , 20 , 330 , 55 , 20 , toggle_val , " export animations " )
if toggle_val :
anim = 1
animsg = " animation will be exported "
else :
anim = 0
animsg = " animation will be not exported "
glRasterPos2i ( 100 , 335 )
Draw . Text ( animsg )
#---Flip normals toggle-----------------------------------------------
Draw . Toggle ( " Flip norm " , 2 , 20 , 300 , 55 , 20 , toggle1_val , " invert normals " )
if toggle1_val :
flip_norm = 1
flipmsg = " flipped normals "
else :
flip_norm = 0
flipmsg = " not flipped normals "
glRasterPos2i ( 100 , 305 )
Draw . Text ( flipmsg )
#------Swap yz toggle----------------------------------------------------------------
Draw . Toggle ( " Swap zy " , 3 , 20 , 270 , 55 , 20 , toggle2_val , " swap z,y axis(y up) " )
if toggle2_val :
swap_yz = 1
swapmsg = " Y-axis up "
else :
swap_yz = 0
swapmsg = " Z-axis up "
glRasterPos2i ( 100 , 275 )
Draw . Text ( swapmsg )
#------Flip z toggle----------------------------------------------------------------
Draw . Toggle ( " Flip z " , 4 , 20 , 240 , 55 , 20 , toggle3_val , " flip z axis " )
if toggle3_val :
flip_z = 1
zmsg = " left handed system "
else :
flip_z = 0
zmsg = " right handed system "
glRasterPos2i ( 100 , 245 )
Draw . Text ( zmsg )
#------Speed toggle----------------------------------------------------------------
Draw . Toggle ( " Speed " , 5 , 20 , 210 , 55 , 20 , toggle4_val , " Animation speed " )
if toggle4_val :
speed = 1
spedmsg = " set speed "
anim_tick = Draw . Number ( " " , 6 , 200 , 210 , 85 , 20 , anim_tick . val , 1 , 100000 , " ticks per second " )
else :
speed = 0
spedmsg = " "
glRasterPos2i ( 100 , 215 )
Draw . Text ( spedmsg )
2006-03-29 06:22:43 +00:00
#------Blender Normals toggle----------------------------------------------------------------
Draw . Toggle ( " Bl.normals " , 10 , 20 , 105 , 75 , 25 , toggle5_val , " export normals as in Blender " )
if toggle5_val :
Bl_norm = 1
#------Recalculute Normals toggle----------------------------------------------------------------
Draw . Toggle ( " recalc.no " , 11 , 120 , 105 , 75 , 25 , toggle6_val , " export recalculated normals " )
if toggle6_val :
recalc_norm = 1
#------Recalculute Normals toggle----------------------------------------------------------------
Draw . Toggle ( " no smooth " , 12 , 220 , 105 , 75 , 25 , toggle7_val , " every vertex has the face normal,no smoothing " )
if toggle7_val :
no_light = 1
2006-03-11 10:45:48 +00:00
#------Draw Button export----------------------------------------------------------------
exp_butt = Draw . Button ( " Export All " , 7 , 20 , 155 , 75 , 30 , " export all the scene objects " )
sel_butt = Draw . Button ( " Export Sel " , 8 , 120 , 155 , 75 , 30 , " export the selected object " )
exit_butt = Draw . Button ( " Exit " , 9 , 220 , 155 , 75 , 30 , " exit " )
glRasterPos2i ( 20 , 75 )
Draw . Text ( " (C) 2006 Arben OMARI " )
glRasterPos2i ( 20 , 55 )
Draw . Text ( " http://www.omariben.too.it " )
glRasterPos2i ( 20 , 35 )
Draw . Text ( " aromar@tin.it " )
2007-04-27 17:19:26 +00:00
2006-03-11 10:45:48 +00:00
def rect ( x , y , width , height ) :
glBegin ( GL_LINE_LOOP )
glVertex2i ( x , y )
glVertex2i ( x + width , y )
glVertex2i ( x + width , y - height )
glVertex2i ( x , y - height )
glEnd ( )
def rectFill ( x , y , width , height ) :
glBegin ( GL_POLYGON )
glVertex2i ( x , y )
glVertex2i ( x + width , y )
glVertex2i ( x + width , y - height )
glVertex2i ( x , y - height )
glEnd ( )
Draw . Register ( draw , event , button_event )
2004-06-12 20:46:24 +00:00
#***********************************************
#***********************************************
# EXPORTER
#***********************************************
#***********************************************
class xExport :
def __init__ ( self , filename ) :
self . file = open ( filename , " w " )
#*********************************************************************************************************************************************
#***********************************************
2005-10-11 19:05:56 +00:00
#Select Scene objects
#***********************************************
2006-03-11 10:45:48 +00:00
def analyzeScene ( self ) :
parent_list = [ ]
2006-12-25 09:17:23 +00:00
for obj in Blender . Scene . GetCurrent ( ) . objects :
if obj . type in ( ' Mesh ' , ' Armature ' , ' Empty ' ) :
if obj . parent == None :
2006-03-11 10:45:48 +00:00
parent_list . append ( obj )
return parent_list
def getChildren ( self , obj ) :
2007-01-16 15:19:04 +00:00
obs = Blender . Scene . GetCurrent ( ) . objects
2007-01-31 11:34:41 +00:00
return [ ob for ob in obs if ob . parent == obj ]
2006-03-11 10:45:48 +00:00
def getArmChildren ( self , obj ) :
2007-01-16 15:19:04 +00:00
for ob in Blender . Scene . GetCurrent ( ) . objects : #Object.Get():
if ob . parent == obj :
return ob
2006-03-11 10:45:48 +00:00
def getLocMat ( self , obj ) :
2006-12-25 09:17:23 +00:00
pare = obj . parent
2006-03-11 10:45:48 +00:00
mat = obj . matrixWorld
mat_id = Matrix ( [ 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 1 ] )
if pare :
mat_p = pare . matrixWorld
mat_c = Matrix ( mat_p )
mat_c . invert ( )
mat_f = mat * mat_c
else :
mat_id . invert ( )
mat_f = mat * mat_id
return mat_f
def writeObjFrames ( self , obj ) :
global space , chld_obj , ch_list
mesh = obj . getData ( )
2006-12-25 09:17:23 +00:00
if obj . type == " Empty " :
2006-03-11 10:45:48 +00:00
mat = self . getLocMat ( obj )
mat_c = Matrix ( mat )
2007-04-27 17:19:26 +00:00
self . writeArmFrames ( mat_c , make_legal_name ( obj . name ) )
2006-03-11 10:45:48 +00:00
if type ( mesh ) == Types . ArmatureType :
Child_obj = self . getArmChildren ( obj )
chld_obj = obj
ch_list . append ( Child_obj )
self . writeRootBone ( obj , Child_obj )
2007-01-31 11:34:41 +00:00
if obj . type == ' Mesh ' and obj not in ch_list :
2006-03-11 10:45:48 +00:00
self . exportMesh ( obj )
def writeChildObj ( self , obj ) :
global space , ch_list
space + = 1
if obj :
for ob in obj :
if ob not in ch_list :
self . writeObjFrames ( ob )
ch_list . append ( ob )
ch_ob = self . getChildren ( ob )
self . writeChildObj ( ch_ob )
self . closeBrackets ( )
self . file . write ( " // End of the Object %s \n " % ( ob . name ) )
2006-03-29 06:22:43 +00:00
def writeRootFrame ( self ) :
global flip_z , swap_yz , speed
2006-03-11 10:45:48 +00:00
if speed :
self . writeAnimTicks ( )
if flip_z :
mat_flip = Matrix ( [ 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 0 , - 1 , 0 ] , [ 0 , 0 , 0 , 1 ] )
else :
mat_flip = Matrix ( [ 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 1 ] )
if swap_yz :
mat_rot = RotationMatrix ( - 90 , 4 , ' x ' )
mat_flip = mat_rot * mat_flip
2006-03-29 06:22:43 +00:00
self . writeArmFrames ( mat_flip , " RootFrame " )
2006-03-11 10:45:48 +00:00
2006-03-29 06:22:43 +00:00
##################################################################
def SelectObjs ( self ) :
global space , chld_obj , ch_list , flip_z , swap_yz , speed
print " exporting... "
self . writeHeader ( )
self . writeRootFrame ( )
2006-03-11 10:45:48 +00:00
obj_list = self . analyzeScene ( )
space + = 1
ch_list = [ ]
for obj in obj_list :
self . writeObjFrames ( obj )
ch_l = self . getChildren ( obj )
for ch in ch_l :
2007-04-27 17:19:26 +00:00
2006-12-25 09:17:23 +00:00
if ch and ch . type == " Armature " :
2006-03-11 10:45:48 +00:00
ch_list . append ( ch )
self . writeObjFrames ( ch )
else :
self . writeChildObj ( ch_l )
2006-12-25 09:17:23 +00:00
if obj . type != " Armature " :
2006-03-11 10:45:48 +00:00
self . file . write ( " } // SI End of the Object %s \n " % ( obj . name ) )
self . file . write ( " } // End of the Root Frame \n " )
if anim :
2007-04-27 17:19:26 +00:00
self . file . write ( " AnimationSet AnimationSet0 { \n " )
2006-12-25 09:17:23 +00:00
for obj in Blender . Scene . GetCurrent ( ) . objects :
if obj . type in ( ' Mesh ' , ' Empty ' ) :
ip_list = obj . ipo
if ip_list != None :
self . writeAnimationObj ( obj )
elif obj . type == ' Armature ' :
act_list = obj . getAction ( )
if act_list != None :
self . writeAnimation ( obj )
#ip_list = obj.ipo
#if ip_list != None :
# self.writeAnimationObj(obj)
2006-03-11 10:45:48 +00:00
self . file . write ( " } // End of Animation Set \n " )
2005-10-11 19:05:56 +00:00
self . writeEnd ( )
2006-03-11 10:45:48 +00:00
#######################################################
def writeAnimTicks ( self ) :
global ticks
self . file . write ( " AnimTicksPerSecond { \n " )
self . file . write ( " %d ; \n " % ( ticks ) )
self . file . write ( " } \n " )
2005-10-11 19:05:56 +00:00
#***********************************************
#Export Mesh without Armature
2004-06-12 20:46:24 +00:00
#***********************************************
2005-10-11 19:05:56 +00:00
def exportMesh ( self , obj ) :
tex = [ ]
mesh = obj . getData ( )
self . writeTextures ( obj , tex )
2006-03-11 10:45:48 +00:00
self . writeMeshcoordArm ( obj , arm_ob = None )
2005-10-11 19:05:56 +00:00
self . writeMeshMaterialList ( obj , mesh , tex )
self . writeMeshNormals ( obj , mesh )
self . writeMeshTextureCoords ( obj , mesh )
2008-06-23 00:21:49 +00:00
self . writeMeshVertexColors ( obj , mesh )
2007-04-27 17:19:26 +00:00
self . file . write ( " } // End of the Mesh %s \n " % ( obj . name ) )
2004-07-27 03:13:45 +00:00
2005-10-11 19:05:56 +00:00
#***********************************************
#Export the Selected Mesh
#***********************************************
def exportSelMesh ( self ) :
print " exporting ... "
self . writeHeader ( )
2006-03-29 06:22:43 +00:00
self . writeRootFrame ( )
2005-10-11 19:05:56 +00:00
tex = [ ]
2006-04-18 22:56:45 +00:00
objs = Object . GetSelected ( )
for obj in objs :
2007-01-31 11:34:41 +00:00
if obj . type == ' Mesh ' :
mesh = obj . data
2006-04-18 22:56:45 +00:00
self . writeTextures ( obj , tex )
self . writeMeshcoordArm ( obj , arm_ob = None )
self . writeMeshMaterialList ( obj , mesh , tex )
self . writeMeshNormals ( obj , mesh )
self . writeMeshTextureCoords ( obj , mesh )
2008-06-23 00:21:49 +00:00
self . writeMeshVertexColors ( obj , mesh )
2006-04-18 22:56:45 +00:00
self . file . write ( " } \n " )
2004-06-12 20:46:24 +00:00
self . file . write ( " } \n " )
2006-04-18 22:56:45 +00:00
ind = objs . index ( obj )
if ind == len ( objs ) - 1 :
self . file . write ( " } \n " )
2006-12-25 09:17:23 +00:00
ip_list = obj . ipo
2006-04-18 22:56:45 +00:00
if ip_list != None :
2007-04-27 17:19:26 +00:00
self . file . write ( " AnimationSet AnimationSet0 { \n " )
2006-04-18 22:56:45 +00:00
self . writeAnimationObj ( obj )
self . file . write ( " } \n " )
else :
print " The selected object is not a mesh "
2005-10-11 19:05:56 +00:00
print " ...finished "
#***********************************************
#Export Mesh with Armature
#***********************************************
def exportMeshArm ( self , arm , arm_ob , ch_obj ) :
tex = [ ]
mesh = ch_obj . getData ( )
self . writeTextures ( ch_obj , tex )
2006-03-11 10:45:48 +00:00
self . writeMeshcoordArm ( ch_obj , arm_ob )
2005-10-11 19:05:56 +00:00
self . writeMeshMaterialList ( ch_obj , mesh , tex )
self . writeMeshNormals ( ch_obj , mesh )
self . writeMeshTextureCoords ( ch_obj , mesh )
self . writeSkinWeights ( arm , mesh )
2006-03-11 10:45:48 +00:00
#self.file.write(" } // End of the Frame %s \n" % (ch_obj.name))
self . file . write ( " } // End of the Object %s \n " % ( ch_obj . name ) )
2005-10-11 19:05:56 +00:00
2004-06-12 20:46:24 +00:00
#***********************************************
#Export Root Bone
#***********************************************
2006-03-11 10:45:48 +00:00
def writeRootBone ( self , chld_obj , child_obj ) :
global space , root_bon
arms = chld_obj . getData ( )
mat_arm = self . getLocMat ( chld_obj )
2006-01-29 19:17:53 +00:00
for bon in arms . bones . values ( ) :
if bon . hasParent ( ) :
pass
else :
root_bon = bon
space + = 1
2006-03-11 10:45:48 +00:00
mat_r = self . writeAnimCombineMatrix ( root_bon , 1 )
2007-04-27 17:19:26 +00:00
self . writeArmFrames ( mat_r , make_legal_name ( root_bon . name ) )
2006-03-11 10:45:48 +00:00
bon_c = root_bon . children
2006-01-29 19:17:53 +00:00
self . writeChildren ( bon_c )
2006-03-11 10:45:48 +00:00
self . file . write ( " } // End of the Bone %s \n " % ( root_bon . name ) )
self . exportMeshArm ( arms , chld_obj , child_obj )
2004-06-12 20:46:24 +00:00
#***********************************************
2006-01-29 19:17:53 +00:00
#Create Children structure
2004-06-12 20:46:24 +00:00
#***********************************************
2006-01-29 19:17:53 +00:00
def writeBon ( self , bon ) :
global space
2006-03-11 10:45:48 +00:00
mat_r = self . writeAnimCombineMatrix ( bon , 1 )
2007-04-27 17:19:26 +00:00
self . writeArmFrames ( mat_r , make_legal_name ( bon . name ) )
2004-06-12 20:46:24 +00:00
2006-01-29 19:17:53 +00:00
def writeChildren ( self , bon_c ) :
global space , bone_list
space + = 1
if bon_c :
for bo in bon_c :
if bo . name not in bone_list :
self . writeBon ( bo )
bone_list . append ( bo . name )
bo_c = bo . children
self . writeChildren ( bo_c )
self . closeBrackets ( )
def closeBrackets ( self ) :
global space
space = space - 1
2004-06-12 20:46:24 +00:00
tab = " "
2006-01-29 19:17:53 +00:00
self . file . write ( " %s " % ( tab * space ) )
2004-06-12 20:46:24 +00:00
self . file . write ( " } \n " )
2006-01-29 19:17:53 +00:00
2004-06-12 20:46:24 +00:00
#***********************************************
#Offset Matrix
#***********************************************
def writeMatrixOffset ( self , bon ) :
2006-01-29 19:17:53 +00:00
global chld_obj
Blender . Set ( ' curframe ' , 1 )
pose = chld_obj . getPose ( )
pos_b = pose . bones [ bon . name ]
mat_b = pos_b . poseMatrix
2006-03-11 10:45:48 +00:00
mat_c = Matrix ( mat_b )
mat_c . invert ( )
return mat_c
2004-06-12 20:46:24 +00:00
#***********************************************
#Combine Matrix
#***********************************************
def writeCombineMatrix ( self , bon ) :
2006-01-29 19:17:53 +00:00
global chld_obj
2006-03-11 10:45:48 +00:00
2006-01-29 19:17:53 +00:00
Blender . Set ( ' curframe ' , 1 )
pose = chld_obj . getPose ( )
pos_b = pose . bones [ bon . name ]
mat_b = pos_b . poseMatrix
2004-06-12 20:46:24 +00:00
if bon . hasParent ( ) :
2006-01-29 19:17:53 +00:00
pare = bon . parent
pos_p = pose . bones [ pare . name ]
mat_p = pos_p . poseMatrix
else :
2004-06-12 20:46:24 +00:00
mat_p = Matrix ( [ 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 1 ] )
2006-03-11 10:45:48 +00:00
mat_c = Matrix ( mat_p )
mat_c . invert ( )
mat_f = mat_b * mat_c
2006-01-29 19:17:53 +00:00
return mat_f
2004-06-12 20:46:24 +00:00
#***********************************************
#Combine Matrix
#***********************************************
2006-01-29 19:17:53 +00:00
def writeAnimCombineMatrix ( self , bon , fre ) :
global chld_obj
Blender . Set ( ' curframe ' , fre )
pose = chld_obj . getPose ( )
pos_b = pose . bones [ bon . name ]
mat_b = pos_b . poseMatrix
2004-06-12 20:46:24 +00:00
if bon . hasParent ( ) :
2006-01-29 19:17:53 +00:00
pare = bon . parent
pos_p = pose . bones [ pare . name ]
mat_p = pos_p . poseMatrix
else :
2004-06-12 20:46:24 +00:00
mat_p = Matrix ( [ 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 1 ] )
2006-03-11 10:45:48 +00:00
mat_c = Matrix ( mat_p )
mat_c . invert ( )
mat_f = mat_b * mat_c
2006-01-29 19:17:53 +00:00
return mat_f
2004-06-12 20:46:24 +00:00
#*********************************************************************************************************************************************
#***********************************************
#Write SkinWeights
#***********************************************
2004-07-27 03:13:45 +00:00
def writeSkinWeights ( self , arm , mesh ) :
global index_list
2006-01-29 19:17:53 +00:00
v_dict = { }
2004-06-12 20:46:24 +00:00
Blender . Set ( ' curframe ' , 1 )
self . file . write ( " XSkinMeshHeader { \n " )
max_infl = 0
2006-01-29 19:17:53 +00:00
for bo in arm . bones . values ( ) :
name = bo . name
2004-06-12 20:46:24 +00:00
try :
vertx_list = mesh . getVertsFromGroup ( name , 1 )
for inde in vertx_list :
2006-03-11 10:45:48 +00:00
vert_infl = mesh . getVertexInfluences ( inde [ 0 ] )
2004-06-12 20:46:24 +00:00
ln_infl = len ( vert_infl )
if ln_infl > max_infl :
max_infl = ln_infl
except :
pass
2006-01-29 19:17:53 +00:00
self . file . write ( " %d ; \n " % ( max_infl ) )
self . file . write ( " %d ; \n " % ( max_infl * 3 ) )
self . file . write ( " %d ; \n " % ( len ( arm . bones . values ( ) ) ) )
2004-06-12 20:46:24 +00:00
self . file . write ( " } \n " )
2006-01-29 19:17:53 +00:00
for bo in arm . bones . values ( ) :
2004-07-27 03:13:45 +00:00
bo_list = [ ]
weight_list = [ ]
2006-01-29 19:17:53 +00:00
name = bo . name
2007-04-27 17:19:26 +00:00
f_name = make_legal_name ( name )
2004-06-12 20:46:24 +00:00
try :
vert_list = mesh . getVertsFromGroup ( name , 1 )
2004-07-27 03:13:45 +00:00
le = 0
for indx in vert_list :
2006-03-11 10:45:48 +00:00
ver_infl = mesh . getVertexInfluences ( indx [ 0 ] )
2006-02-12 15:37:01 +00:00
infl = 0.0
if len ( ver_infl ) != 0 :
sum = 0.0
2006-03-11 10:45:48 +00:00
for bone_n , weight in ver_infl :
if bone_n == name :
2006-02-12 15:37:01 +00:00
infl = weight
2006-03-11 10:45:48 +00:00
sum + = weight
infl / = sum
2004-07-27 03:13:45 +00:00
i = - 1
for el in index_list :
i + = 1
if el == indx [ 0 ] :
le + = 1
bo_list . append ( i )
weight_list . append ( infl )
2004-06-12 20:46:24 +00:00
self . file . write ( " SkinWeights { \n " )
2006-01-29 19:17:53 +00:00
self . file . write ( ' " %s " ; \n ' % ( f_name ) )
self . file . write ( ' %d ; \n ' % ( le ) )
2004-06-12 20:46:24 +00:00
count = 0
2004-07-27 03:13:45 +00:00
for ind in bo_list :
2004-06-12 20:46:24 +00:00
count + = 1
2004-07-27 03:13:45 +00:00
if count == len ( bo_list ) :
2006-01-29 19:17:53 +00:00
self . file . write ( " %d ; \n " % ( ind ) )
2004-06-12 20:46:24 +00:00
else :
2006-01-29 19:17:53 +00:00
self . file . write ( " %d , \n " % ( ind ) )
2004-06-12 20:46:24 +00:00
cou = 0
2004-07-27 03:13:45 +00:00
for wegh in weight_list :
2004-06-12 20:46:24 +00:00
cou + = 1
2004-07-27 03:13:45 +00:00
if cou == len ( weight_list ) :
2006-01-29 19:17:53 +00:00
self . file . write ( " %f ; \n " % ( round ( wegh , 6 ) ) )
2004-06-12 20:46:24 +00:00
else :
2006-01-29 19:17:53 +00:00
self . file . write ( " %f , \n " % ( round ( wegh , 6 ) ) )
2004-06-12 20:46:24 +00:00
matx = self . writeMatrixOffset ( bo )
2006-01-29 19:17:53 +00:00
self . writeOffsFrames ( matx , name )
2004-06-12 20:46:24 +00:00
except :
pass
2006-03-11 10:45:48 +00:00
self . file . write ( " } // End of XSkinMeshHeader \n " )
2004-06-12 20:46:24 +00:00
#***********************************************
# Write Matrices
#***********************************************
2006-01-29 19:17:53 +00:00
def writeArmFrames ( self , matx , name ) :
global space
2004-06-12 20:46:24 +00:00
tab = " "
self . file . write ( " %s " % ( tab * space ) )
self . file . write ( " Frame " )
self . file . write ( " %s { \n \n " % ( name ) )
self . file . write ( " %s " % ( tab * space ) )
self . file . write ( " FrameTransformMatrix { \n " )
2006-03-11 10:45:48 +00:00
self . writeFrame ( matx )
2004-06-12 20:46:24 +00:00
#***********************************************
2006-03-11 10:45:48 +00:00
# Write Frames
2004-06-12 20:46:24 +00:00
#***********************************************
2006-01-29 19:17:53 +00:00
def writeOffsFrames ( self , matx , name ) :
space = 1
2006-03-11 10:45:48 +00:00
self . writeFrame ( matx )
#***********************************************
# Write Frames
#***********************************************
def writeFrame ( self , matx ) :
2004-06-12 20:46:24 +00:00
tab = " "
self . file . write ( " %s " % ( tab * space ) )
2006-01-29 19:17:53 +00:00
self . file . write ( " %f , %f , %f , %f , \n " %
2004-06-12 20:46:24 +00:00
( round ( matx [ 0 ] [ 0 ] , 4 ) , round ( matx [ 0 ] [ 1 ] , 4 ) , round ( matx [ 0 ] [ 2 ] , 4 ) , round ( matx [ 0 ] [ 3 ] , 4 ) ) )
2005-10-11 19:05:56 +00:00
self . file . write ( " %s " % ( tab * space ) )
2006-01-29 19:17:53 +00:00
self . file . write ( " %f , %f , %f , %f , \n " %
2005-10-11 19:05:56 +00:00
( round ( matx [ 1 ] [ 0 ] , 4 ) , round ( matx [ 1 ] [ 1 ] , 4 ) , round ( matx [ 1 ] [ 2 ] , 4 ) , round ( matx [ 1 ] [ 3 ] , 4 ) ) )
self . file . write ( " %s " % ( tab * space ) )
2006-01-29 19:17:53 +00:00
self . file . write ( " %f , %f , %f , %f , \n " %
2004-06-12 20:46:24 +00:00
( round ( matx [ 2 ] [ 0 ] , 4 ) , round ( matx [ 2 ] [ 1 ] , 4 ) , round ( matx [ 2 ] [ 2 ] , 4 ) , round ( matx [ 2 ] [ 3 ] , 4 ) ) )
2005-10-11 19:05:56 +00:00
self . file . write ( " %s " % ( tab * space ) )
2006-01-29 19:17:53 +00:00
self . file . write ( " %f , %f , %f , %f ;; \n " %
2006-03-11 10:45:48 +00:00
( round ( matx [ 3 ] [ 0 ] , 4 ) , round ( matx [ 3 ] [ 1 ] , 4 ) , round ( matx [ 3 ] [ 2 ] , 4 ) , round ( matx [ 3 ] [ 3 ] , 4 ) ) )
2004-06-12 20:46:24 +00:00
self . file . write ( " %s " % ( tab * space ) )
self . file . write ( " } \n " )
#*********************************************************************************************************************************************
#***********************************************
#HEADER
#***********************************************
def writeHeader ( self ) :
self . file . write ( " xof 0303txt 0032 \n \n \n " )
self . file . write ( " template VertexDuplicationIndices { \n \
< b8d65549 - d7c9 - 4995 - 89 cf - 53 a9a8b031e3 > \n \
DWORD nIndices ; \n \
DWORD nOriginalVertices ; \n \
array DWORD indices [ nIndices ] ; \n \
} \n \
template XSkinMeshHeader { \n \
< 3 cf169ce - ff7c - 44 ab - 93 c0 - f78f62d172e2 > \n \
WORD nMaxSkinWeightsPerVertex ; \n \
WORD nMaxSkinWeightsPerFace ; \n \
WORD nBones ; \n \
} \n \
template SkinWeights { \n \
< 6 f0d123b - bad2 - 4167 - a0d0 - 80224 f25fabb > \n \
STRING transformNodeName ; \n \
DWORD nWeights ; \n \
array DWORD vertexIndices [ nWeights ] ; \n \
array float weights [ nWeights ] ; \n \
Matrix4x4 matrixOffset ; \n \
} \n \n " )
#***********************************************
#CLOSE FILE
#***********************************************
def writeEnd ( self ) :
self . file . close ( )
print " ... finished "
#***********************************************
#EXPORT TEXTURES
#***********************************************
def writeTextures ( self , name , tex ) :
mesh = name . data
for face in mesh . faces :
if face . image and face . image . name not in tex :
tex . append ( face . image . name )
#***********************************************
2005-10-11 19:05:56 +00:00
#EXPORT MESH DATA with Armature
2004-06-12 20:46:24 +00:00
#***********************************************
2006-03-11 10:45:48 +00:00
def writeMeshcoordArm ( self , obj , arm_ob ) :
global index_list , flip_z
#TransformMatrix
mat = self . getLocMat ( obj )
2007-04-27 17:19:26 +00:00
self . writeArmFrames ( mat , make_legal_name ( obj . name ) )
2006-03-11 10:45:48 +00:00
mesh = NMesh . GetRawFromObject ( obj . name )
2004-07-27 03:13:45 +00:00
self . file . write ( " Mesh { \n " )
2005-10-11 19:05:56 +00:00
numface = len ( mesh . faces )
2004-06-12 20:46:24 +00:00
#VERTICES NUMBER
2004-07-27 03:13:45 +00:00
numvert = 0
for face in mesh . faces :
numvert = numvert + len ( face . v )
2006-03-11 10:45:48 +00:00
self . file . write ( " %d ; \n " % ( numvert ) )
if numvert == 0 :
print " Mesh named " , mesh . name , " has no vertices.Problems may occur using the .x file "
2004-06-12 20:46:24 +00:00
#VERTICES COORDINATES
counter = 0
2004-07-27 03:13:45 +00:00
for face in mesh . faces :
2004-06-12 20:46:24 +00:00
counter + = 1
2004-07-27 03:13:45 +00:00
for n in range ( len ( face . v ) ) :
index_list . append ( face . v [ n ] . index )
2006-03-11 10:45:48 +00:00
vec_vert = Vector ( [ ( face . v [ n ] . co [ 0 ] ) , face . v [ n ] . co [ 1 ] , face . v [ n ] . co [ 2 ] , 1 ] )
if arm_ob :
f_vec_vert = vec_vert * mat
else :
f_vec_vert = vec_vert
2006-01-29 19:17:53 +00:00
self . file . write ( " %f ; %f ; %f ; " % ( round ( f_vec_vert [ 0 ] , 4 ) , round ( f_vec_vert [ 1 ] , 4 ) , round ( f_vec_vert [ 2 ] , 4 ) ) )
2005-10-11 19:05:56 +00:00
if counter == numface :
2004-07-27 03:13:45 +00:00
if n == len ( face . v ) - 1 :
self . file . write ( " ; \n " )
else :
self . file . write ( " , \n " )
else :
self . file . write ( " , \n " )
2006-03-11 10:45:48 +00:00
if flip_z :
a3 = 0 ; b3 = 2 ; c3 = 1
a4 = 0 ; b4 = 3 ; c4 = 2 ; d4 = 1
else :
a3 = 0 ; b3 = 1 ; c3 = 2
a4 = 0 ; b4 = 1 ; c4 = 2 ; d4 = 3
2004-07-27 03:13:45 +00:00
#FACES NUMBER
2005-10-11 19:05:56 +00:00
self . file . write ( " %s ; \n " % ( numface ) )
2004-07-27 03:13:45 +00:00
coun , counter = 0 , 0
for face in mesh . faces :
coun + = 1
2007-04-27 17:19:26 +00:00
separator = ' , '
2004-07-27 03:13:45 +00:00
if coun == numface :
2007-04-27 17:19:26 +00:00
separator = ' ; '
if len ( face . v ) == 3 :
self . file . write ( " 3; %d , %d , %d ; %c \n " % ( counter + a3 , counter + b3 , counter + c3 , separator ) )
counter + = 3
elif len ( face . v ) == 4 :
self . file . write ( " 4; %d , %d , %d , %d ; %c \n " % ( counter + a4 , counter + b4 , counter + c4 , counter + d4 , separator ) )
counter + = 4
elif len ( face . v ) < 3 :
print " WARNING:the mesh has faces with less then 3 vertices "
print " It my be not exported correctly. "
2004-06-12 20:46:24 +00:00
2005-10-11 19:05:56 +00:00
2004-06-12 20:46:24 +00:00
#***********************************************
#MESH MATERIAL LIST
#***********************************************
2006-03-11 10:45:48 +00:00
def writeMeshMaterialList ( self , obj , mesh , tex ) :
2004-06-12 20:46:24 +00:00
self . file . write ( " MeshMaterialList { \n " )
#HOW MANY MATERIALS ARE USED
count = 0
2006-03-11 10:45:48 +00:00
for mat in mesh . getMaterials ( ) :
2004-06-12 20:46:24 +00:00
count + = 1
2006-03-11 10:45:48 +00:00
self . file . write ( " %d ; \n " % ( len ( tex ) + count ) )
2004-06-12 20:46:24 +00:00
#HOW MANY FACES IT HAS
2006-03-11 10:45:48 +00:00
numfaces = len ( mesh . faces )
self . file . write ( " %d ; \n " % ( numfaces ) )
2004-06-12 20:46:24 +00:00
##MATERIALS INDEX FOR EVERY FACE
counter = 0
2006-03-11 10:45:48 +00:00
for face in mesh . faces :
2004-06-12 20:46:24 +00:00
counter + = 1
mater = face . materialIndex
if counter == numfaces :
if face . image and face . image . name in tex :
2006-03-11 10:45:48 +00:00
self . file . write ( " %d ;; \n " % ( tex . index ( face . image . name ) + count ) )
2004-06-12 20:46:24 +00:00
else :
2006-03-11 10:45:48 +00:00
self . file . write ( " %d ;; \n " % ( mater ) )
2004-06-12 20:46:24 +00:00
else :
if face . image and face . image . name in tex :
2006-03-11 10:45:48 +00:00
self . file . write ( " %d , \n " % ( tex . index ( face . image . name ) + count ) )
2004-06-12 20:46:24 +00:00
else :
2006-03-11 10:45:48 +00:00
self . file . write ( " %d , \n " % ( mater ) )
2004-06-12 20:46:24 +00:00
##MATERIAL NAME
2006-03-11 10:45:48 +00:00
for mat in mesh . getMaterials ( ) :
2004-06-12 20:46:24 +00:00
self . file . write ( " Material " )
2007-04-27 17:19:26 +00:00
self . file . write ( " %s " % ( make_legal_name ( mat . name ) ) )
2004-06-12 20:46:24 +00:00
self . file . write ( " { \n " )
2006-03-11 10:45:48 +00:00
self . file . write ( " %f ; %f ; %f ; " % ( mat . R , mat . G , mat . B ) )
2004-06-12 20:46:24 +00:00
self . file . write ( " %s ;; \n " % ( mat . alpha ) )
2006-03-11 10:45:48 +00:00
self . file . write ( " %f ; \n " % ( mat . spec ) )
self . file . write ( " %f ; %f ; %f ;; \n " % ( mat . specR , mat . specG , mat . specB ) )
2004-06-12 20:46:24 +00:00
self . file . write ( " 0.0; 0.0; 0.0;; \n " )
2006-03-11 10:45:48 +00:00
self . file . write ( " } //End of Material \n " )
2004-06-12 20:46:24 +00:00
for mat in tex :
self . file . write ( " Material Mat " )
self . file . write ( " %s " % ( len ( tex ) ) )
self . file . write ( " { \n " )
self . file . write ( " 1.0; 1.0; 1.0; 1.0;; \n " )
self . file . write ( " 1.0; \n " )
self . file . write ( " 1.0; 1.0; 1.0;; \n " )
self . file . write ( " 0.0; 0.0; 0.0;; \n " )
2006-01-29 19:17:53 +00:00
self . file . write ( " TextureFilename { " )
2006-03-11 10:45:48 +00:00
self . file . write ( ' " %s " ; ' % ( mat ) )
2004-06-12 20:46:24 +00:00
self . file . write ( " } \n " )
2006-03-11 10:45:48 +00:00
self . file . write ( " } // End of Material \n " )
self . file . write ( " } //End of MeshMaterialList \n " )
2004-06-12 20:46:24 +00:00
#***********************************************
#MESH NORMALS
#***********************************************
2004-07-27 03:13:45 +00:00
def writeMeshNormals ( self , name , mesh ) :
2006-03-29 06:22:43 +00:00
global flip_norm , flip_z , no_light , recalc_norm , Bl_norm
2004-06-12 20:46:24 +00:00
self . file . write ( " MeshNormals { \n " )
#VERTICES NUMBER
2004-07-27 03:13:45 +00:00
numvert = 0
for face in mesh . faces :
numvert = numvert + len ( face . v )
2006-03-11 10:45:48 +00:00
self . file . write ( " %d ; \n " % ( numvert ) )
2004-07-27 03:13:45 +00:00
numfaces = len ( mesh . faces )
2006-03-11 10:45:48 +00:00
if flip_norm :
fl = - 1
else :
fl = 1
2004-06-12 20:46:24 +00:00
#VERTICES NORMAL
2006-03-29 06:22:43 +00:00
if Bl_norm :
self . writeBlenderNormals ( mesh , fl )
if recalc_norm :
self . writeRecalcNormals ( mesh , fl )
if no_light :
self . writeNoSmothing ( mesh , fl )
2004-07-27 03:13:45 +00:00
2006-03-11 10:45:48 +00:00
if flip_z :
a3 = 0 ; b3 = 2 ; c3 = 1
a4 = 0 ; b4 = 3 ; c4 = 2 ; d4 = 1
else :
a3 = 0 ; b3 = 1 ; c3 = 2
a4 = 0 ; b4 = 1 ; c4 = 2 ; d4 = 3
2004-07-27 03:13:45 +00:00
2004-06-12 20:46:24 +00:00
#FACES NUMBER
2004-07-27 03:13:45 +00:00
self . file . write ( " %s ; \n " % ( numfaces ) )
coun , counter = 0 , 0
for face in mesh . faces :
coun + = 1
if coun == numfaces :
2004-06-12 20:46:24 +00:00
if len ( face . v ) == 3 :
2006-03-11 10:45:48 +00:00
self . file . write ( " 3; %d , %d , %d ;; \n " % ( counter + a3 , counter + b3 , counter + c3 ) )
2004-07-27 03:13:45 +00:00
counter + = 3
else :
2006-03-11 10:45:48 +00:00
self . file . write ( " 4; %d , %d , %d , %d ;; \n " % ( counter + a4 , counter + b4 , counter + c4 , counter + d4 ) )
2004-07-27 03:13:45 +00:00
counter + = 4
2004-06-12 20:46:24 +00:00
else :
2004-07-27 03:13:45 +00:00
2004-06-12 20:46:24 +00:00
if len ( face . v ) == 3 :
2006-03-11 10:45:48 +00:00
self . file . write ( " 3; %d , %d , %d ;, \n " % ( counter + a3 , counter + b3 , counter + c3 ) )
2004-07-27 03:13:45 +00:00
counter + = 3
else :
2006-03-11 10:45:48 +00:00
self . file . write ( " 4; %d , %d , %d , %d ;, \n " % ( counter + a4 , counter + b4 , counter + c4 , counter + d4 ) )
2004-07-27 03:13:45 +00:00
counter + = 4
2006-03-11 10:45:48 +00:00
self . file . write ( " } //End of MeshNormals \n " )
2006-03-29 06:22:43 +00:00
def writeBlenderNormals ( self , mesh , fl ) :
numfaces = len ( mesh . faces )
#VERTICES NORMAL
counter = 0
for face in mesh . faces :
counter + = 1
for n in range ( len ( face . v ) ) :
self . file . write ( " %f ; %f ; %f ; " % (
( round ( face . v [ n ] . no [ 0 ] , 6 ) * fl ) , ( round ( face . v [ n ] . no [ 1 ] , 6 ) * fl ) , ( round ( face . v [ n ] . no [ 2 ] , 6 ) * fl ) ) )
if counter == numfaces :
if n == len ( face . v ) - 1 :
self . file . write ( " ; \n " )
else :
self . file . write ( " , \n " )
else :
self . file . write ( " , \n " )
def writeRecalcNormals ( self , mesh , fl ) :
numfaces = len ( mesh . faces )
normal_list = { }
idx = 0
for vertex in mesh . verts :
v_norm = Vector ( [ 0 , 0 , 0 ] )
normal_list [ idx ] = v_norm
idx + = 1
for face in mesh . faces :
for verts in face . v :
if verts . index == vertex . index :
v_norm [ 0 ] + = face . no [ 0 ]
v_norm [ 1 ] + = face . no [ 1 ]
v_norm [ 2 ] + = face . no [ 2 ]
v_norm . normalize ( )
counter = 0
for face in mesh . faces :
counter + = 1
n = 0
for vert in face . v :
n + = 1
norm = normal_list [ vert . index ]
self . file . write ( " %f ; %f ; %f ; " % (
( round ( norm [ 0 ] , 6 ) * fl ) , ( round ( norm [ 1 ] , 6 ) * fl ) , ( round ( norm [ 2 ] , 6 ) * fl ) ) )
if counter == numfaces :
if n == len ( face . v ) :
self . file . write ( " ; \n " )
else :
self . file . write ( " , \n " )
else :
self . file . write ( " , \n " )
def writeNoSmothing ( self , mesh , fl ) :
numfaces = len ( mesh . faces )
counter = 0
for face in mesh . faces :
counter + = 1
n = 0
for n in range ( len ( face . v ) ) :
n + = 1
self . file . write ( " %f ; %f ; %f ; " % (
( round ( face . no [ 0 ] , 6 ) * fl ) , ( round ( face . no [ 1 ] , 6 ) * fl ) , ( round ( face . no [ 2 ] , 6 ) * fl ) ) )
if counter == numfaces :
if n == len ( face . v ) :
self . file . write ( " ; \n " )
else :
self . file . write ( " , \n " )
else :
self . file . write ( " , \n " )
2004-06-12 20:46:24 +00:00
#***********************************************
#MESH TEXTURE COORDS
#***********************************************
2004-07-27 03:13:45 +00:00
def writeMeshTextureCoords ( self , name , mesh ) :
if mesh . hasFaceUV ( ) :
self . file . write ( " MeshTextureCoords { \n " )
#VERTICES NUMBER
numvert = 0
for face in mesh . faces :
numvert + = len ( face . v )
2006-03-11 10:45:48 +00:00
self . file . write ( " %d ; \n " % ( numvert ) )
2004-07-27 03:13:45 +00:00
#UV COORDS
numfaces = len ( mesh . faces )
counter = - 1
co = 0
for face in mesh . faces :
counter + = 1
co + = 1
for n in range ( len ( face . v ) ) :
2006-03-11 10:45:48 +00:00
self . file . write ( " %f ; %f ; " % ( mesh . faces [ counter ] . uv [ n ] [ 0 ] , - mesh . faces [ counter ] . uv [ n ] [ 1 ] ) )
2004-07-27 03:13:45 +00:00
if co == numfaces :
if n == len ( face . v ) - 1 :
self . file . write ( " ; \n " )
else :
self . file . write ( " , \n " )
else :
self . file . write ( " , \n " )
2006-03-11 10:45:48 +00:00
self . file . write ( " } //End of MeshTextureCoords \n " )
2008-06-23 00:21:49 +00:00
#***********************************************
#MESH VORTEX COLORS
#***********************************************
def writeMeshVertexColors ( self , name , mesh ) :
if mesh . hasVertexColours ( ) :
self . file . write ( " MeshVertexColors { \n " )
#VERTICES NUMBER
numvert = reduce ( lambda i , f : len ( f ) + i , mesh . faces , 0 )
self . file . write ( " %d ; \n " % ( numvert ) )
#VERTEX COLORS
vcounter = 0
for f in mesh . faces :
col = f . col
for i , c in enumerate ( col ) :
# Note vcol alpha has no meaning
self . file . write ( " %d ; %f ; %f ; %f ; %f ; " % ( vcounter , c . r / 255.0 , c . g / 255.0 , c . b / 255.0 , 1.0 ) ) # c.a/255.0))
vcounter + = 1
if vcounter == numvert :
self . file . write ( " ; \n " )
else :
self . file . write ( " , \n " )
self . file . write ( " } //End of MeshVertexColors \n " )
2004-06-12 20:46:24 +00:00
#***********************************************#***********************************************#***********************************************
#***********************************************
#FRAMES
#***********************************************
def writeFrames ( self , matx ) :
2004-07-27 03:13:45 +00:00
2006-03-11 10:45:48 +00:00
self . file . write ( " %f , %f , %f , %f , " %
( round ( matx [ 0 ] [ 0 ] , 4 ) , round ( matx [ 0 ] [ 1 ] , 4 ) , round ( matx [ 0 ] [ 2 ] , 4 ) , round ( matx [ 0 ] [ 3 ] , 4 ) ) )
self . file . write ( " %f , %f , %f , %f , " %
( round ( matx [ 1 ] [ 0 ] , 4 ) , round ( matx [ 1 ] [ 1 ] , 4 ) , round ( matx [ 1 ] [ 2 ] , 4 ) , round ( matx [ 1 ] [ 3 ] , 4 ) ) )
self . file . write ( " %f , %f , %f , %f , " %
( round ( matx [ 2 ] [ 0 ] , 4 ) , round ( matx [ 2 ] [ 1 ] , 4 ) , round ( matx [ 2 ] [ 2 ] , 4 ) , round ( matx [ 2 ] [ 3 ] , 4 ) ) )
self . file . write ( " %f , %f , %f , %f ;; " %
( round ( matx [ 3 ] [ 0 ] , 4 ) , round ( matx [ 3 ] [ 1 ] , 4 ) , round ( matx [ 3 ] [ 2 ] , 4 ) , round ( matx [ 3 ] [ 3 ] , 4 ) ) )
2004-06-12 20:46:24 +00:00
2006-01-29 19:17:53 +00:00
2004-06-12 20:46:24 +00:00
#***********************************************
#WRITE ANIMATION KEYS
#***********************************************
2005-10-11 19:05:56 +00:00
def writeAnimation ( self , arm_ob ) :
2006-03-11 10:45:48 +00:00
global mat_dict , root_bon
2005-10-11 19:05:56 +00:00
arm = arm_ob . getData ( )
act_list = arm_ob . getAction ( )
ip = act_list . getAllChannelIpos ( )
2006-01-29 19:17:53 +00:00
for bon in arm . bones . values ( ) :
2005-10-11 19:05:56 +00:00
point_list = [ ]
2006-01-29 19:17:53 +00:00
name = bon . name
2007-04-27 17:19:26 +00:00
name_f = make_legal_name ( name )
2005-10-11 19:05:56 +00:00
try :
ip_bon_channel = ip [ bon . name ]
ip_bon_name = ip_bon_channel . getName ( )
2006-01-29 19:17:53 +00:00
2005-10-11 19:05:56 +00:00
ip_bon = Blender . Ipo . Get ( ip_bon_name )
poi = ip_bon . getCurves ( )
2006-01-29 19:17:53 +00:00
2005-10-11 19:05:56 +00:00
for po in poi [ 3 ] . getPoints ( ) :
a = po . getPoints ( )
point_list . append ( int ( a [ 0 ] ) )
2006-01-29 19:17:53 +00:00
#point_list.pop(0)
2005-10-11 19:05:56 +00:00
self . file . write ( " Animation { \n " )
2006-03-11 10:45:48 +00:00
self . file . write ( " { %s } \n " % ( name_f ) )
2005-10-11 19:05:56 +00:00
self . file . write ( " AnimationKey { \n " )
self . file . write ( " 4; \n " )
2006-03-11 10:45:48 +00:00
self . file . write ( " %d ; \n " % ( len ( point_list ) ) )
2005-10-11 19:05:56 +00:00
for fr in point_list :
2006-01-29 19:17:53 +00:00
2006-03-11 10:45:48 +00:00
if name == root_bon . name :
mat_b = self . writeAnimCombineMatrix ( bon , fr )
mat_arm = self . getLocMat ( arm_ob )
mat = mat_b * mat_arm
else :
mat = self . writeAnimCombineMatrix ( bon , fr )
self . file . write ( " %d ; " % ( fr ) )
2005-10-11 19:05:56 +00:00
self . file . write ( " 16; " )
2004-06-12 20:46:24 +00:00
2006-01-29 19:17:53 +00:00
self . writeFrames ( mat )
2004-06-12 20:46:24 +00:00
2005-10-11 19:05:56 +00:00
if fr == point_list [ len ( point_list ) - 1 ] :
self . file . write ( " ; \n " )
else :
self . file . write ( " , \n " )
self . file . write ( " } \n " )
self . file . write ( " } \n " )
self . file . write ( " \n " )
except :
pass
#***********************************************
#WRITE ANIMATION KEYS
#***********************************************
def writeAnimationObj ( self , obj ) :
point_list = [ ]
2006-12-25 09:17:23 +00:00
ip = obj . ipo
2005-10-11 19:05:56 +00:00
poi = ip . getCurves ( )
for po in poi [ 0 ] . getPoints ( ) :
a = po . getPoints ( )
point_list . append ( int ( a [ 0 ] ) )
self . file . write ( " Animation { \n " )
2006-03-11 10:45:48 +00:00
self . file . write ( " { " )
2007-04-27 17:19:26 +00:00
self . file . write ( " %s } \n " % ( make_legal_name ( obj . name ) ) )
2005-10-11 19:05:56 +00:00
self . file . write ( " AnimationKey { \n " )
self . file . write ( " 4; \n " )
2006-03-11 10:45:48 +00:00
self . file . write ( " %d ; \n " % ( len ( point_list ) ) )
2005-10-11 19:05:56 +00:00
for fr in point_list :
2006-03-11 10:45:48 +00:00
self . file . write ( " %d ; " % ( fr ) )
2005-10-11 19:05:56 +00:00
self . file . write ( " 16; " )
Blender . Set ( ' curframe ' , fr )
2006-03-11 10:45:48 +00:00
#mat_new = self.getLocMat(obj)
mat_new = obj . matrixLocal
2005-10-11 19:05:56 +00:00
self . writeFrames ( mat_new )
if fr == point_list [ len ( point_list ) - 1 ] :
self . file . write ( " ; \n " )
else :
self . file . write ( " , \n " )
self . file . write ( " } \n " )
self . file . write ( " } \n " )
2006-03-11 10:45:48 +00:00
2004-06-12 20:46:24 +00:00
#***********************************************#***********************************************#***********************************************
2004-06-21 12:01:23 +00:00
2006-02-12 15:37:01 +00:00
2007-04-27 17:19:26 +00:00