destruction of previous slot api. if it returns, it'll
[blender.git] / release / scripts / export_fbx.py
1 #!BPY
2 """
3 Name: 'Autodesk FBX (.fbx)...'
4 Blender: 244
5 Group: 'Export'
6 Tooltip: 'Selection to an ASCII Autodesk FBX '
7 """
8 __author__ = "Campbell Barton"
9 __url__ = ['www.blender.org', 'blenderartists.org']
10 __version__ = "1.1"
11
12 __bpydoc__ = """\
13 This script is an exporter to the FBX file format.
14
15 http://wiki.blender.org/index.php/Scripts/Manual/Export/autodesk_fbx
16 """
17 # --------------------------------------------------------------------------
18 # FBX Export v0.1 by Campbell Barton (AKA Ideasman)
19 # --------------------------------------------------------------------------
20 # ***** BEGIN GPL LICENSE BLOCK *****
21 #
22 # This program is free software; you can redistribute it and/or
23 # modify it under the terms of the GNU General Public License
24 # as published by the Free Software Foundation; either version 2
25 # of the License, or (at your option) any later version.
26 #
27 # This program is distributed in the hope that it will be useful,
28 # but WITHOUT ANY WARRANTY; without even the implied warranty of
29 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 # GNU General Public License for more details.
31 #
32 # You should have received a copy of the GNU General Public License
33 # along with this program; if not, write to the Free Software Foundation,
34 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
35 #
36 # ***** END GPL LICENCE BLOCK *****
37 # --------------------------------------------------------------------------
38
39 try:
40         import time
41         # import os # only needed for batch export, nbot used yet
42 except:
43         time = None # use this to check if they have python modules installed
44
45 # for python 2.3 support
46 try:
47         set()
48 except:
49         try:
50                 from sets import Set as set
51         except:
52                 set = None # so it complains you dont have a !
53
54 # os is only needed for batch 'own dir' option
55 try:
56         import os
57 except:
58         os = None
59
60 import Blender
61 import bpy
62 from Blender.Mathutils import Matrix, Vector, RotationMatrix
63
64 import BPyObject
65 import BPyMesh
66 import BPySys
67 import BPyMessages
68
69 ## This was used to make V, but faster not to do all that
70 ##valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_,.()[]{}'
71 ##v = range(255)
72 ##for c in valid: v.remove(ord(c))
73 v = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,42,43,47,58,59,60,61,62,63,64,92,94,96,124,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254]
74 invalid = ''.join([chr(i) for i in v])
75 def cleanName(name):
76         for ch in invalid:      name = name.replace(ch, '_')
77         return name
78 del v, i
79
80
81 def copy_file(source, dest):
82         file = open(source, 'rb')
83         data = file.read()
84         file.close()
85         
86         file = open(dest, 'wb')
87         file.write(data)
88         file.close()
89
90
91 def copy_images(dest_dir, textures):
92         if not dest_dir.endswith(Blender.sys.sep):
93                 dest_dir += Blender.sys.sep
94         
95         image_paths = set()
96         for img in textures:
97                 image_paths.add(Blender.sys.expandpath(img.filename))
98         
99         # Now copy images
100         copyCount = 0
101         for image_path in image_paths:
102                 if Blender.sys.exists(image_path):
103                         # Make a name for the target path.
104                         dest_image_path = dest_dir + image_path.split('\\')[-1].split('/')[-1]
105                         if not Blender.sys.exists(dest_image_path): # Image isnt alredy there
106                                 print '\tCopying "%s" > "%s"' % (image_path, dest_image_path)
107                                 try:
108                                         copy_file(image_path, dest_image_path)
109                                         copyCount+=1
110                                 except:
111                                         print '\t\tWarning, file failed to copy, skipping.'
112         
113         print '\tCopied %d images' % copyCount
114
115 mtx4_identity = Matrix()
116
117 # testing
118 mtx_x90         = RotationMatrix( 90, 3, 'x') # used
119 #mtx_x90n       = RotationMatrix(-90, 3, 'x')
120 #mtx_y90        = RotationMatrix( 90, 3, 'y')
121 #mtx_y90n       = RotationMatrix(-90, 3, 'y')
122 #mtx_z90        = RotationMatrix( 90, 3, 'z')
123 #mtx_z90n       = RotationMatrix(-90, 3, 'z')
124
125 #mtx4_x90       = RotationMatrix( 90, 4, 'x')
126 mtx4_x90n       = RotationMatrix(-90, 4, 'x') # used
127 #mtx4_y90       = RotationMatrix( 90, 4, 'y')
128 mtx4_y90n       = RotationMatrix(-90, 4, 'y') # used
129 mtx4_z90        = RotationMatrix( 90, 4, 'z') # used
130 mtx4_z90n       = RotationMatrix(-90, 4, 'z') # used
131
132 def strip_path(p):
133         return p.split('\\')[-1].split('/')[-1]
134
135 # Used to add the scene name into the filename without using odd chars  
136 sane_name_mapping_ob = {}
137 sane_name_mapping_mat = {}
138 sane_name_mapping_tex = {}
139 sane_name_mapping_take = {}
140 sane_name_mapping_group = {}
141
142 # Make sure reserved names are not used
143 sane_name_mapping_ob['Scene'] = 'Scene_'
144 sane_name_mapping_ob['blend_root'] = 'blend_root_'
145
146 def increment_string(t):
147         name = t
148         num = ''
149         while name and name[-1].isdigit():
150                 num = name[-1] + num
151                 name = name[:-1]
152         if num: return '%s%d' % (name, int(num)+1)      
153         else:   return name + '_0'
154
155
156
157 # todo - Disallow the name 'Scene' and 'blend_root' - it will bugger things up.
158 def sane_name(data, dct):
159         #if not data: return None
160         name = data.name
161         
162         # dont cache, only ever call once for each data type now,
163         # so as to avoid namespace collision between types - like with objects <-> bones
164         #try:           return dct[name]
165         #except:                pass
166         
167         orig_name = name
168         if not name:
169                 name = 'unnamed' # blank string, ASKING FOR TROUBLE!
170         else:
171                 #name = BPySys.cleanName(name)
172                 name = cleanName(name) # use our own
173         
174         while name in dct.itervalues(): name = increment_string(name)
175         
176         dct[orig_name] = name
177         return name
178
179 def sane_obname(data):          return sane_name(data, sane_name_mapping_ob)
180 def sane_matname(data):         return sane_name(data, sane_name_mapping_mat)
181 def sane_texname(data):         return sane_name(data, sane_name_mapping_tex)
182 def sane_takename(data):        return sane_name(data, sane_name_mapping_take)
183 def sane_groupname(data):       return sane_name(data, sane_name_mapping_group)
184
185 def derived_paths(fname_orig, basepath, FORCE_CWD=False):
186         '''
187         fname_orig - blender path, can be relative
188         basepath - fname_rel will be relative to this
189         FORCE_CWD - dont use the basepath, just add a ./ to the filename.
190                 use when we know the file will be in the basepath.
191         '''
192         fname = Blender.sys.expandpath(fname_orig)
193         fname_strip = strip_path(fname)
194         if FORCE_CWD:   fname_rel = '.' + Blender.sys.sep + fname_strip
195         else:                           fname_rel = Blender.sys.relpath(fname, basepath)
196         if fname_rel.startswith('//'): fname_rel = '.' + Blender.sys.sep + fname_rel[2:]
197         return fname, fname_strip, fname_rel
198
199
200 def mat4x4str(mat):
201         return '%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f' % tuple([ f for v in mat for f in v ])
202
203 def meshNormalizedWeights(me):
204         try: # account for old bad BPyMesh
205                 groupNames, vWeightList = BPyMesh.meshWeight2List(me)
206         except:
207                 return [],[]
208         
209         if not groupNames:
210                 return [],[]
211         
212         for i, vWeights in enumerate(vWeightList):
213                 tot = 0.0
214                 for w in vWeights:
215                         tot+=w
216                 
217                 if tot:
218                         for j, w in enumerate(vWeights):
219                                 vWeights[j] = w/tot
220         
221         return groupNames, vWeightList
222
223 header_comment = \
224 '''; FBX 6.1.0 project file
225 ; Created by Blender FBX Exporter
226 ; for support mail: ideasman42@gmail.com
227 ; ----------------------------------------------------
228
229 '''
230
231 # This func can be called with just the filename
232 def write(filename, batch_objects = None, \
233                 EXP_OBS_SELECTED =                      True,
234                 EXP_MESH =                                      True,
235                 EXP_MESH_APPLY_MOD =            True,
236                 EXP_MESH_HQ_NORMALS =           False,
237                 EXP_ARMATURE =                          True,
238                 EXP_LAMP =                                      True,
239                 EXP_CAMERA =                            True,
240                 EXP_EMPTY =                                     True,
241                 EXP_IMAGE_COPY =                        False,
242                 GLOBAL_MATRIX =                         Matrix(),
243                 ANIM_ENABLE =                           True,
244                 ANIM_OPTIMIZE =                         True,
245                 ANIM_OPTIMIZE_PRECISSION =      6,
246                 ANIM_ACTION_ALL =                       False,
247                 BATCH_ENABLE =                          False,
248                 BATCH_GROUP =                           True,
249                 BATCH_SCENE =                           False,
250                 BATCH_FILE_PREFIX =                     '',
251                 BATCH_OWN_DIR =                         False
252         ):
253         
254         # ----------------- Batch support!
255         if BATCH_ENABLE:
256                 if os == None:  BATCH_OWN_DIR = False
257                 
258                 fbxpath = filename
259                 
260                 # get the path component of filename
261                 tmp_exists = Blender.sys.exists(fbxpath)
262                 
263                 if tmp_exists != 2: # a file, we want a path
264                         while fbxpath and fbxpath[-1] not in ('/', '\\'):
265                                 fbxpath = fbxpath[:-1]
266                         if not filename:
267                                 Draw.PupMenu('Error%t|Directory does not exist!')
268                                 return
269                         
270                         tmp_exists = Blender.sys.exists(fbxpath)
271                 
272                 if tmp_exists != 2:
273                         Draw.PupMenu('Error%t|Directory does not exist!')
274                         return
275                 
276                 if not fbxpath.endswith(Blender.sys.sep):
277                         fbxpath += Blender.sys.sep
278                 del tmp_exists
279                 
280                 
281                 if BATCH_GROUP:
282                         data_seq = bpy.data.groups
283                 else:
284                         data_seq = bpy.data.scenes
285                 
286                 # call this function within a loop with BATCH_ENABLE == False
287                 orig_sce = bpy.data.scenes.active
288                 
289                 
290                 new_fbxpath = fbxpath # own dir option modifies, we need to keep an original
291                 for data in data_seq: # scene or group
292                         newname = BATCH_FILE_PREFIX + BPySys.cleanName(data.name)
293                         
294                         
295                         if BATCH_OWN_DIR:
296                                 new_fbxpath = fbxpath + newname + Blender.sys.sep
297                                 # path may alredy exist
298                                 # TODO - might exist but be a file. unlikely but should probably account for it.
299                                 
300                                 if Blender.sys.exists(new_fbxpath) == 0:
301                                         os.mkdir(new_fbxpath)
302                                 
303                         
304                         filename = new_fbxpath + newname + '.fbx'
305                         
306                         print '\nBatch exporting %s as...\n\t"%s"' % (data, filename)
307                         
308                         if BATCH_GROUP: #group
309                                 # group, so objects update properly, add a dummy scene.
310                                 sce = bpy.data.scenes.new()
311                                 sce.Layers = (1<<20) -1
312                                 bpy.data.scenes.active = sce
313                                 for ob_base in data.objects:
314                                         sce.objects.link(ob_base)
315                                 
316                                 sce.update(1)
317                                 
318                                 # TODO - BUMMER! Armatures not in the group wont animate the mesh
319                                 
320                         else:# scene
321                                 
322                                 
323                                 data_seq.active = data
324                         
325                         
326                         # Call self with modified args
327                         # Dont pass batch options since we alredy usedt them
328                         write(filename, data.objects,
329                                 False,
330                                 EXP_MESH,
331                                 EXP_MESH_APPLY_MOD,
332                                 EXP_MESH_HQ_NORMALS,
333                                 EXP_ARMATURE,
334                                 EXP_LAMP,
335                                 EXP_CAMERA,
336                                 EXP_EMPTY,
337                                 EXP_IMAGE_COPY,
338                                 GLOBAL_MATRIX,
339                                 ANIM_ENABLE,
340                                 ANIM_OPTIMIZE,
341                                 ANIM_OPTIMIZE_PRECISSION,
342                                 ANIM_ACTION_ALL
343                         )
344                         
345                         if BATCH_GROUP:
346                                 # remove temp group scene
347                                 bpy.data.scenes.unlink(sce)
348                 
349                 bpy.data.scenes.active = orig_sce
350                 
351                 return # so the script wont run after we have batch exported.
352         
353         # end batch support
354         
355         # Use this for working out paths relative to the export location
356         basepath = Blender.sys.dirname(filename)
357         
358         # ----------------------------------------------
359         # storage classes
360         class my_bone_class:
361                 __slots__ =(\
362                   'blenName',\
363                   'blenBone',\
364                   'blenMeshes',\
365                   'restMatrix',\
366                   'parent',\
367                   'blenName',\
368                   'fbxName',\
369                   'fbxArm',\
370                   '__pose_bone',\
371                   '__anim_poselist')
372                 
373                 def __init__(self, blenBone, fbxArm):
374                         
375                         # This is so 2 armatures dont have naming conflicts since FBX bones use object namespace
376                         self.fbxName = sane_obname(blenBone)
377                         
378                         self.blenName =                 blenBone.name
379                         self.blenBone =                 blenBone
380                         self.blenMeshes =               {}                                      # fbxMeshObName : mesh
381                         self.fbxArm =                   fbxArm
382                         self.restMatrix =               blenBone.matrix['ARMATURESPACE']
383                         
384                         # not used yet
385                         # self.restMatrixInv =  self.restMatrix.copy().invert()
386                         # self.restMatrixLocal =        None # set later, need parent matrix
387                         
388                         self.parent =                   None
389                         
390                         # not public
391                         pose = fbxArm.blenObject.getPose()
392                         self.__pose_bone =              pose.bones[self.blenName]
393                         
394                         # store a list if matricies here, (poseMatrix, head, tail)
395                         # {frame:posematrix, frame:posematrix, ...}
396                         self.__anim_poselist = {}
397                 
398                 '''
399                 def calcRestMatrixLocal(self):
400                         if self.parent:
401                                 self.restMatrixLocal = self.restMatrix * self.parent.restMatrix.copy().invert()
402                         else:
403                                 self.restMatrixLocal = self.restMatrix.copy()
404                 '''
405                 def setPoseFrame(self, f):
406                         # cache pose info here, frame must be set beforehand
407                         
408                         # Didnt end up needing head or tail, if we do - here it is.
409                         '''
410                         self.__anim_poselist[f] = (\
411                                 self.__pose_bone.poseMatrix.copy(),\
412                                 self.__pose_bone.head.copy(),\
413                                 self.__pose_bone.tail.copy() )
414                         '''
415                         
416                         self.__anim_poselist[f] = self.__pose_bone.poseMatrix.copy()
417                 
418                 # get pose from frame.
419                 def getPoseMatrix(self, f):# ----------------------------------------------
420                         return self.__anim_poselist[f]
421                 '''
422                 def getPoseHead(self, f):
423                         #return self.__pose_bone.head.copy()
424                         return self.__anim_poselist[f][1].copy()
425                 def getPoseTail(self, f):
426                         #return self.__pose_bone.tail.copy()
427                         return self.__anim_poselist[f][2].copy()
428                 '''
429                 # end
430                 
431                 def getAnimParRelMatrix(self, frame):
432                         #arm_mat = self.fbxArm.matrixWorld
433                         #arm_mat = self.fbxArm.parRelMatrix()
434                         if not self.parent:
435                                 #return mtx4_z90 * (self.getPoseMatrix(frame) * arm_mat) # dont apply arm matrix anymore
436                                 return mtx4_z90 * self.getPoseMatrix(frame)
437                         else:
438                                 #return (mtx4_z90 * ((self.getPoseMatrix(frame) * arm_mat)))  *  (mtx4_z90 * (self.parent.getPoseMatrix(frame) * arm_mat)).invert()
439                                 return (mtx4_z90 * (self.getPoseMatrix(frame)))  *  (mtx4_z90 * self.parent.getPoseMatrix(frame)).invert()
440                 
441                 # we need thes because cameras and lights modified rotations
442                 def getAnimParRelMatrixRot(self, frame):
443                         return self.getAnimParRelMatrix(frame)
444                 
445                 def flushAnimData(self):
446                         self.__anim_poselist.clear()
447
448
449         class my_object_generic:
450                 # Other settings can be applied for each type - mesh, armature etc.
451                 def __init__(self, ob, matrixWorld = None):
452                         self.fbxName = sane_obname(ob)
453                         self.blenObject = ob
454                         self.fbxGroupNames = []
455                         self.fbxParent = None # set later on IF the parent is in the selection.
456                         if matrixWorld:         self.matrixWorld = matrixWorld * GLOBAL_MATRIX
457                         else:                           self.matrixWorld = ob.matrixWorld * GLOBAL_MATRIX
458                         self.__anim_poselist = {} # we should only access this
459                 
460                 def parRelMatrix(self):
461                         if self.fbxParent:
462                                 return self.matrixWorld * self.fbxParent.matrixWorld.copy().invert()
463                         else:
464                                 return self.matrixWorld
465                 
466                 def setPoseFrame(self, f):
467                         self.__anim_poselist[f] =  self.blenObject.matrixWorld.copy()
468                 
469                 def getAnimParRelMatrix(self, frame):
470                         if self.fbxParent:
471                                 #return (self.__anim_poselist[frame] * self.fbxParent.__anim_poselist[frame].copy().invert() ) * GLOBAL_MATRIX
472                                 return (self.__anim_poselist[frame] * GLOBAL_MATRIX) * (self.fbxParent.__anim_poselist[frame] * GLOBAL_MATRIX).invert()
473                         else:
474                                 return self.__anim_poselist[frame] * GLOBAL_MATRIX
475                 
476                 def getAnimParRelMatrixRot(self, frame):
477                         type = self.blenObject.type
478                         if self.fbxParent:
479                                 matrix_rot = (((self.__anim_poselist[frame] * GLOBAL_MATRIX) * (self.fbxParent.__anim_poselist[frame] * GLOBAL_MATRIX).invert())).rotationPart()
480                         else:
481                                 matrix_rot = (self.__anim_poselist[frame] * GLOBAL_MATRIX).rotationPart()
482                         
483                         # Lamps need to be rotated
484                         if type =='Lamp':
485                                 matrix_rot = mtx_x90 * matrix_rot
486                         elif ob and type =='Camera':
487                                 y = Vector(0,1,0) * matrix_rot
488                                 matrix_rot = matrix_rot * RotationMatrix(90, 3, 'r', y)
489                         
490                         return matrix_rot
491                         
492         # ----------------------------------------------
493         
494         
495         
496         
497         
498         print '\nFBX export starting...', filename
499         start_time = Blender.sys.time()
500         try:
501                 file = open(filename, 'w')
502         except:
503                 return False
504         
505         sce = bpy.data.scenes.active
506         world = sce.world
507         
508         
509         # ---------------------------- Write the header first
510         file.write(header_comment)
511         if time:
512                 curtime = time.localtime()[0:6]
513         else:
514                 curtime = [0,0,0,0,0,0]
515         # 
516         file.write(\
517 '''FBXHeaderExtension:  {
518         FBXHeaderVersion: 1003
519         FBXVersion: 6100
520         CreationTimeStamp:  {
521                 Version: 1000
522                 Year: %.4i
523                 Month: %.2i
524                 Day: %.2i
525                 Hour: %.2i
526                 Minute: %.2i
527                 Second: %.2i
528                 Millisecond: 0
529         }
530         Creator: "FBX SDK/FBX Plugins build 20070228"
531         OtherFlags:  {
532                 FlagPLE: 0
533         }
534 }''' % (curtime))
535         
536         file.write('\nCreationTime: "%.4i-%.2i-%.2i %.2i:%.2i:%.2i:000"' % curtime)
537         file.write('\nCreator: "Blender3D version %.2f"' % Blender.Get('version'))
538         
539         pose_items = [] # list of (fbxName, matrix) to write pose data for, easier to collect allong the way
540         
541         # --------------- funcs for exporting
542         def object_tx(ob, loc, matrix, matrix_mod = None):
543                 '''
544                 Matrix mod is so armature objects can modify their bone matricies
545                 '''
546                 if isinstance(ob, Blender.Types.BoneType):
547                         
548                         # we know we have a matrix
549                         # matrix = mtx4_z90 * (ob.matrix['ARMATURESPACE'] * matrix_mod)
550                         matrix = mtx4_z90 * ob.matrix['ARMATURESPACE'] # dont apply armature matrix anymore
551                         
552                         parent = ob.parent
553                         if parent:
554                                 #par_matrix = mtx4_z90 * (parent.matrix['ARMATURESPACE'] * matrix_mod)
555                                 par_matrix = mtx4_z90 * parent.matrix['ARMATURESPACE'] # dont apply armature matrix anymore
556                                 matrix = matrix * par_matrix.copy().invert()
557                                 
558                         matrix_rot =    matrix.rotationPart()
559                         
560                         loc =                   tuple(matrix.translationPart())
561                         scale =                 tuple(matrix.scalePart())
562                         rot =                   tuple(matrix_rot.toEuler())
563                         
564                 else:
565                         # This is bad because we need the parent relative matrix from the fbx parent (if we have one), dont use anymore
566                         #if ob and not matrix: matrix = ob.matrixWorld * GLOBAL_MATRIX
567                         if ob and not matrix: raise "error: this should never happen!"
568                         
569                         matrix_rot = matrix
570                         #if matrix:
571                         #       matrix = matrix_scale * matrix
572                         
573                         if matrix:
574                                 loc = tuple(matrix.translationPart())
575                                 scale = tuple(matrix.scalePart())
576                                 
577                                 matrix_rot = matrix.rotationPart()
578                                 # Lamps need to be rotated
579                                 if ob and ob.type =='Lamp':
580                                         matrix_rot = mtx_x90 * matrix_rot
581                                         rot = tuple(matrix_rot.toEuler())
582                                 elif ob and ob.type =='Camera':
583                                         y = Vector(0,1,0) * matrix_rot
584                                         matrix_rot = matrix_rot * RotationMatrix(90, 3, 'r', y)
585                                         rot = tuple(matrix_rot.toEuler())
586                                 else:
587                                         rot = tuple(matrix_rot.toEuler())
588                         else:
589                                 if not loc:
590                                         loc = 0,0,0
591                                 scale = 1,1,1
592                                 rot = 0,0,0
593                 
594                 return loc, rot, scale, matrix, matrix_rot
595         
596         def write_object_tx(ob, loc, matrix, matrix_mod= None):
597                 '''
598                 We have loc to set the location if non blender objects that have a location
599                 
600                 matrix_mod is only used for bones at the moment
601                 '''
602                 loc, rot, scale, matrix, matrix_rot = object_tx(ob, loc, matrix, matrix_mod)
603                 
604                 file.write('\n\t\t\tProperty: "Lcl Translation", "Lcl Translation", "A+",%.15f,%.15f,%.15f' % loc)
605                 file.write('\n\t\t\tProperty: "Lcl Rotation", "Lcl Rotation", "A+",%.15f,%.15f,%.15f' % rot)
606                 file.write('\n\t\t\tProperty: "Lcl Scaling", "Lcl Scaling", "A+",%.15f,%.15f,%.15f' % scale)
607                 return loc, rot, scale, matrix, matrix_rot
608         
609         def write_object_props(ob=None, loc=None, matrix=None, matrix_mod=None):
610                 # if the type is 0 its an empty otherwise its a mesh
611                 # only difference at the moment is one has a color
612                 file.write('''
613                 Properties60:  {
614                         Property: "QuaternionInterpolate", "bool", "",0
615                         Property: "Visibility", "Visibility", "A+",1''')
616                 
617                 loc, rot, scale, matrix, matrix_rot = write_object_tx(ob, loc, matrix, matrix_mod)
618                 
619                 # Rotation order, note, for FBX files Iv loaded normal order is 1
620                 # setting to zero.
621                 # eEULER_XYZ = 0
622                 # eEULER_XZY
623                 # eEULER_YZX
624                 # eEULER_YXZ
625                 # eEULER_ZXY
626                 # eEULER_ZYX
627                 
628                 file.write('''
629                         Property: "RotationOffset", "Vector3D", "",0,0,0
630                         Property: "RotationPivot", "Vector3D", "",0,0,0
631                         Property: "ScalingOffset", "Vector3D", "",0,0,0
632                         Property: "ScalingPivot", "Vector3D", "",0,0,0
633                         Property: "TranslationActive", "bool", "",0
634                         Property: "TranslationMin", "Vector3D", "",0,0,0
635                         Property: "TranslationMax", "Vector3D", "",0,0,0
636                         Property: "TranslationMinX", "bool", "",0
637                         Property: "TranslationMinY", "bool", "",0
638                         Property: "TranslationMinZ", "bool", "",0
639                         Property: "TranslationMaxX", "bool", "",0
640                         Property: "TranslationMaxY", "bool", "",0
641                         Property: "TranslationMaxZ", "bool", "",0
642                         Property: "RotationOrder", "enum", "",0
643                         Property: "RotationSpaceForLimitOnly", "bool", "",0
644                         Property: "AxisLen", "double", "",10
645                         Property: "PreRotation", "Vector3D", "",0,0,0
646                         Property: "PostRotation", "Vector3D", "",0,0,0
647                         Property: "RotationActive", "bool", "",0
648                         Property: "RotationMin", "Vector3D", "",0,0,0
649                         Property: "RotationMax", "Vector3D", "",0,0,0
650                         Property: "RotationMinX", "bool", "",0
651                         Property: "RotationMinY", "bool", "",0
652                         Property: "RotationMinZ", "bool", "",0
653                         Property: "RotationMaxX", "bool", "",0
654                         Property: "RotationMaxY", "bool", "",0
655                         Property: "RotationMaxZ", "bool", "",0
656                         Property: "RotationStiffnessX", "double", "",0
657                         Property: "RotationStiffnessY", "double", "",0
658                         Property: "RotationStiffnessZ", "double", "",0
659                         Property: "MinDampRangeX", "double", "",0
660                         Property: "MinDampRangeY", "double", "",0
661                         Property: "MinDampRangeZ", "double", "",0
662                         Property: "MaxDampRangeX", "double", "",0
663                         Property: "MaxDampRangeY", "double", "",0
664                         Property: "MaxDampRangeZ", "double", "",0
665                         Property: "MinDampStrengthX", "double", "",0
666                         Property: "MinDampStrengthY", "double", "",0
667                         Property: "MinDampStrengthZ", "double", "",0
668                         Property: "MaxDampStrengthX", "double", "",0
669                         Property: "MaxDampStrengthY", "double", "",0
670                         Property: "MaxDampStrengthZ", "double", "",0
671                         Property: "PreferedAngleX", "double", "",0
672                         Property: "PreferedAngleY", "double", "",0
673                         Property: "PreferedAngleZ", "double", "",0
674                         Property: "InheritType", "enum", "",0
675                         Property: "ScalingActive", "bool", "",0
676                         Property: "ScalingMin", "Vector3D", "",1,1,1
677                         Property: "ScalingMax", "Vector3D", "",1,1,1
678                         Property: "ScalingMinX", "bool", "",0
679                         Property: "ScalingMinY", "bool", "",0
680                         Property: "ScalingMinZ", "bool", "",0
681                         Property: "ScalingMaxX", "bool", "",0
682                         Property: "ScalingMaxY", "bool", "",0
683                         Property: "ScalingMaxZ", "bool", "",0
684                         Property: "GeometricTranslation", "Vector3D", "",0,0,0
685                         Property: "GeometricRotation", "Vector3D", "",0,0,0
686                         Property: "GeometricScaling", "Vector3D", "",1,1,1
687                         Property: "LookAtProperty", "object", ""
688                         Property: "UpVectorProperty", "object", ""
689                         Property: "Show", "bool", "",1
690                         Property: "NegativePercentShapeSupport", "bool", "",1
691                         Property: "DefaultAttributeIndex", "int", "",0''')
692                 if ob and type(ob) != Blender.Types.BoneType:
693                         # Only mesh objects have color 
694                         file.write('\n\t\t\tProperty: "Color", "Color", "A",0.8,0.8,0.8')
695                         file.write('\n\t\t\tProperty: "Size", "double", "",100')
696                         file.write('\n\t\t\tProperty: "Look", "enum", "",1')
697                 
698                 return loc, rot, scale, matrix, matrix_rot
699         
700         
701         # -------------------------------------------- Armatures
702         #def write_bone(bone, name, matrix_mod):
703         def write_bone(my_bone):
704                 file.write('\n\tModel: "Model::%s", "Limb" {' % my_bone.fbxName)
705                 file.write('\n\t\tVersion: 232')
706                 
707                 #poseMatrix = write_object_props(my_bone.blenBone, None, None, my_bone.fbxArm.parRelMatrix())[3]
708                 poseMatrix = write_object_props(my_bone.blenBone)[3] # dont apply bone matricies anymore
709                 pose_items.append( (my_bone.fbxName, poseMatrix) )
710                 
711                 
712                 # file.write('\n\t\t\tProperty: "Size", "double", "",%.6f' % ((my_bone.blenData.head['ARMATURESPACE'] - my_bone.blenData.tail['ARMATURESPACE']) * my_bone.fbxArm.parRelMatrix()).length)
713                 file.write('\n\t\t\tProperty: "Size", "double", "",1')
714                 
715                 #((my_bone.blenData.head['ARMATURESPACE'] * my_bone.fbxArm.matrixWorld) - (my_bone.blenData.tail['ARMATURESPACE'] * my_bone.fbxArm.parRelMatrix())).length)
716                 
717                 """
718                 file.write('\n\t\t\tProperty: "LimbLength", "double", "",%.6f' %\
719                         ((my_bone.blenBone.head['ARMATURESPACE'] - my_bone.blenBone.tail['ARMATURESPACE']) * my_bone.fbxArm.parRelMatrix()).length)
720                 """
721                 
722                 file.write('\n\t\t\tProperty: "LimbLength", "double", "",%.6f' %\
723                         (my_bone.blenBone.head['ARMATURESPACE'] - my_bone.blenBone.tail['ARMATURESPACE']).length)
724                 
725                 #file.write('\n\t\t\tProperty: "LimbLength", "double", "",1')
726                 file.write('\n\t\t\tProperty: "Color", "ColorRGB", "",0.8,0.8,0.8')
727                 file.write('\n\t\t\tProperty: "Color", "Color", "A",0.8,0.8,0.8')
728                 file.write('\n\t\t}')
729                 file.write('\n\t\tMultiLayer: 0')
730                 file.write('\n\t\tMultiTake: 1')
731                 file.write('\n\t\tShading: Y')
732                 file.write('\n\t\tCulling: "CullingOff"')
733                 file.write('\n\t\tTypeFlags: "Skeleton"')
734                 file.write('\n\t}')
735         
736         def write_camera_switch():
737                 file.write('''
738         Model: "Model::Camera Switcher", "CameraSwitcher" {
739                 Version: 232''')
740                 
741                 write_object_props()
742                 file.write('''
743                         Property: "Color", "Color", "A",0.8,0.8,0.8
744                         Property: "Camera Index", "Integer", "A+",100
745                 }
746                 MultiLayer: 0
747                 MultiTake: 1
748                 Hidden: "True"
749                 Shading: W
750                 Culling: "CullingOff"
751                 Version: 101
752                 Name: "Model::Camera Switcher"
753                 CameraId: 0
754                 CameraName: 100
755                 CameraIndexName: 
756         }''')
757         
758         def write_camera_dummy(name, loc, near, far, proj_type, up):
759                 file.write('\n\tModel: "Model::%s", "Camera" {' % name )
760                 file.write('\n\t\tVersion: 232')
761                 write_object_props(None, loc)
762                 
763                 file.write('\n\t\t\tProperty: "Color", "Color", "A",0.8,0.8,0.8')
764                 file.write('\n\t\t\tProperty: "Roll", "Roll", "A+",0')
765                 file.write('\n\t\t\tProperty: "FieldOfView", "FieldOfView", "A+",40')
766                 file.write('\n\t\t\tProperty: "FieldOfViewX", "FieldOfView", "A+",1')
767                 file.write('\n\t\t\tProperty: "FieldOfViewY", "FieldOfView", "A+",1')
768                 file.write('\n\t\t\tProperty: "OpticalCenterX", "Real", "A+",0')
769                 file.write('\n\t\t\tProperty: "OpticalCenterY", "Real", "A+",0')
770                 file.write('\n\t\t\tProperty: "BackgroundColor", "Color", "A+",0.63,0.63,0.63')
771                 file.write('\n\t\t\tProperty: "TurnTable", "Real", "A+",0')
772                 file.write('\n\t\t\tProperty: "DisplayTurnTableIcon", "bool", "",1')
773                 file.write('\n\t\t\tProperty: "Motion Blur Intensity", "Real", "A+",1')
774                 file.write('\n\t\t\tProperty: "UseMotionBlur", "bool", "",0')
775                 file.write('\n\t\t\tProperty: "UseRealTimeMotionBlur", "bool", "",1')
776                 file.write('\n\t\t\tProperty: "ResolutionMode", "enum", "",0')
777                 file.write('\n\t\t\tProperty: "ApertureMode", "enum", "",2')
778                 file.write('\n\t\t\tProperty: "GateFit", "enum", "",0')
779                 file.write('\n\t\t\tProperty: "FocalLength", "Real", "A+",21.3544940948486')
780                 file.write('\n\t\t\tProperty: "CameraFormat", "enum", "",0')
781                 file.write('\n\t\t\tProperty: "AspectW", "double", "",320')
782                 file.write('\n\t\t\tProperty: "AspectH", "double", "",200')
783                 file.write('\n\t\t\tProperty: "PixelAspectRatio", "double", "",1')
784                 file.write('\n\t\t\tProperty: "UseFrameColor", "bool", "",0')
785                 file.write('\n\t\t\tProperty: "FrameColor", "ColorRGB", "",0.3,0.3,0.3')
786                 file.write('\n\t\t\tProperty: "ShowName", "bool", "",1')
787                 file.write('\n\t\t\tProperty: "ShowGrid", "bool", "",1')
788                 file.write('\n\t\t\tProperty: "ShowOpticalCenter", "bool", "",0')
789                 file.write('\n\t\t\tProperty: "ShowAzimut", "bool", "",1')
790                 file.write('\n\t\t\tProperty: "ShowTimeCode", "bool", "",0')
791                 file.write('\n\t\t\tProperty: "NearPlane", "double", "",%.6f' % near)
792                 file.write('\n\t\t\tProperty: "FarPlane", "double", "",%.6f' % far)
793                 file.write('\n\t\t\tProperty: "FilmWidth", "double", "",0.816')
794                 file.write('\n\t\t\tProperty: "FilmHeight", "double", "",0.612')
795                 file.write('\n\t\t\tProperty: "FilmAspectRatio", "double", "",1.33333333333333')
796                 file.write('\n\t\t\tProperty: "FilmSqueezeRatio", "double", "",1')
797                 file.write('\n\t\t\tProperty: "FilmFormatIndex", "enum", "",4')
798                 file.write('\n\t\t\tProperty: "ViewFrustum", "bool", "",1')
799                 file.write('\n\t\t\tProperty: "ViewFrustumNearFarPlane", "bool", "",0')
800                 file.write('\n\t\t\tProperty: "ViewFrustumBackPlaneMode", "enum", "",2')
801                 file.write('\n\t\t\tProperty: "BackPlaneDistance", "double", "",100')
802                 file.write('\n\t\t\tProperty: "BackPlaneDistanceMode", "enum", "",0')
803                 file.write('\n\t\t\tProperty: "ViewCameraToLookAt", "bool", "",1')
804                 file.write('\n\t\t\tProperty: "LockMode", "bool", "",0')
805                 file.write('\n\t\t\tProperty: "LockInterestNavigation", "bool", "",0')
806                 file.write('\n\t\t\tProperty: "FitImage", "bool", "",0')
807                 file.write('\n\t\t\tProperty: "Crop", "bool", "",0')
808                 file.write('\n\t\t\tProperty: "Center", "bool", "",1')
809                 file.write('\n\t\t\tProperty: "KeepRatio", "bool", "",1')
810                 file.write('\n\t\t\tProperty: "BackgroundMode", "enum", "",0')
811                 file.write('\n\t\t\tProperty: "BackgroundAlphaTreshold", "double", "",0.5')
812                 file.write('\n\t\t\tProperty: "ForegroundTransparent", "bool", "",1')
813                 file.write('\n\t\t\tProperty: "DisplaySafeArea", "bool", "",0')
814                 file.write('\n\t\t\tProperty: "SafeAreaDisplayStyle", "enum", "",1')
815                 file.write('\n\t\t\tProperty: "SafeAreaAspectRatio", "double", "",1.33333333333333')
816                 file.write('\n\t\t\tProperty: "Use2DMagnifierZoom", "bool", "",0')
817                 file.write('\n\t\t\tProperty: "2D Magnifier Zoom", "Real", "A+",100')
818                 file.write('\n\t\t\tProperty: "2D Magnifier X", "Real", "A+",50')
819                 file.write('\n\t\t\tProperty: "2D Magnifier Y", "Real", "A+",50')
820                 file.write('\n\t\t\tProperty: "CameraProjectionType", "enum", "",%i' % proj_type)
821                 file.write('\n\t\t\tProperty: "UseRealTimeDOFAndAA", "bool", "",0')
822                 file.write('\n\t\t\tProperty: "UseDepthOfField", "bool", "",0')
823                 file.write('\n\t\t\tProperty: "FocusSource", "enum", "",0')
824                 file.write('\n\t\t\tProperty: "FocusAngle", "double", "",3.5')
825                 file.write('\n\t\t\tProperty: "FocusDistance", "double", "",200')
826                 file.write('\n\t\t\tProperty: "UseAntialiasing", "bool", "",0')
827                 file.write('\n\t\t\tProperty: "AntialiasingIntensity", "double", "",0.77777')
828                 file.write('\n\t\t\tProperty: "UseAccumulationBuffer", "bool", "",0')
829                 file.write('\n\t\t\tProperty: "FrameSamplingCount", "int", "",7')
830                 file.write('\n\t\t}')
831                 file.write('\n\t\tMultiLayer: 0')
832                 file.write('\n\t\tMultiTake: 0')
833                 file.write('\n\t\tHidden: "True"')
834                 file.write('\n\t\tShading: Y')
835                 file.write('\n\t\tCulling: "CullingOff"')
836                 file.write('\n\t\tTypeFlags: "Camera"')
837                 file.write('\n\t\tGeometryVersion: 124')
838                 file.write('\n\t\tPosition: %.6f,%.6f,%.6f' % loc)
839                 file.write('\n\t\tUp: %i,%i,%i' % up)
840                 file.write('\n\t\tLookAt: 0,0,0')
841                 file.write('\n\t\tShowInfoOnMoving: 1')
842                 file.write('\n\t\tShowAudio: 0')
843                 file.write('\n\t\tAudioColor: 0,1,0')
844                 file.write('\n\t\tCameraOrthoZoom: 1')
845                 file.write('\n\t}')
846         
847         def write_camera_default():
848                 # This sucks but to match FBX converter its easier to
849                 # write the cameras though they are not needed.
850                 write_camera_dummy('Producer Perspective',      (0,71.3,287.5), 10, 4000, 0, (0,1,0))
851                 write_camera_dummy('Producer Top',                      (0,4000,0), 1, 30000, 1, (0,0,-1))
852                 write_camera_dummy('Producer Bottom',                   (0,-4000,0), 1, 30000, 1, (0,0,-1))
853                 write_camera_dummy('Producer Front',                    (0,0,4000), 1, 30000, 1, (0,1,0))
854                 write_camera_dummy('Producer Back',                     (0,0,-4000), 1, 30000, 1, (0,1,0))
855                 write_camera_dummy('Producer Right',                    (4000,0,0), 1, 30000, 1, (0,1,0))
856                 write_camera_dummy('Producer Left',                     (-4000,0,0), 1, 30000, 1, (0,1,0))
857         
858         def write_camera(my_cam):
859                 '''
860                 Write a blender camera
861                 '''
862                 render = sce.render
863                 width   = render.sizeX
864                 height  = render.sizeY
865                 aspect  = float(width)/height
866                 
867                 data = my_cam.blenObject.data
868                 
869                 file.write('\n\tModel: "Model::%s", "Camera" {' % my_cam.fbxName )
870                 file.write('\n\t\tVersion: 232')
871                 loc, rot, scale, matrix, matrix_rot = write_object_props(my_cam.blenObject, None, my_cam.parRelMatrix())
872                 
873                 file.write('\n\t\t\tProperty: "Roll", "Roll", "A+",0')
874                 file.write('\n\t\t\tProperty: "FieldOfView", "FieldOfView", "A+",%.6f' % data.angle)
875                 file.write('\n\t\t\tProperty: "FieldOfViewX", "FieldOfView", "A+",1')
876                 file.write('\n\t\t\tProperty: "FieldOfViewY", "FieldOfView", "A+",1')
877                 file.write('\n\t\t\tProperty: "FocalLength", "Real", "A+",14.0323972702026')
878                 file.write('\n\t\t\tProperty: "OpticalCenterX", "Real", "A+",%.6f' % data.shiftX) # not sure if this is in the correct units?
879                 file.write('\n\t\t\tProperty: "OpticalCenterY", "Real", "A+",%.6f' % data.shiftY) # ditto 
880                 file.write('\n\t\t\tProperty: "BackgroundColor", "Color", "A+",0,0,0')
881                 file.write('\n\t\t\tProperty: "TurnTable", "Real", "A+",0')
882                 file.write('\n\t\t\tProperty: "DisplayTurnTableIcon", "bool", "",1')
883                 file.write('\n\t\t\tProperty: "Motion Blur Intensity", "Real", "A+",1')
884                 file.write('\n\t\t\tProperty: "UseMotionBlur", "bool", "",0')
885                 file.write('\n\t\t\tProperty: "UseRealTimeMotionBlur", "bool", "",1')
886                 file.write('\n\t\t\tProperty: "ResolutionMode", "enum", "",0')
887                 file.write('\n\t\t\tProperty: "ApertureMode", "enum", "",2')
888                 file.write('\n\t\t\tProperty: "GateFit", "enum", "",0')
889                 file.write('\n\t\t\tProperty: "CameraFormat", "enum", "",0')
890                 file.write('\n\t\t\tProperty: "AspectW", "double", "",%i' % width)
891                 file.write('\n\t\t\tProperty: "AspectH", "double", "",%i' % height)
892                 
893                 '''Camera aspect ratio modes.
894                         0 If the ratio mode is eWINDOW_SIZE, both width and height values aren't relevant.
895                         1 If the ratio mode is eFIXED_RATIO, the height value is set to 1.0 and the width value is relative to the height value.
896                         2 If the ratio mode is eFIXED_RESOLUTION, both width and height values are in pixels.
897                         3 If the ratio mode is eFIXED_WIDTH, the width value is in pixels and the height value is relative to the width value.
898                         4 If the ratio mode is eFIXED_HEIGHT, the height value is in pixels and the width value is relative to the height value. 
899                 
900                 Definition at line 234 of file kfbxcamera.h. '''
901                 
902                 file.write('\n\t\t\tProperty: "PixelAspectRatio", "double", "",2')
903                 
904                 file.write('\n\t\t\tProperty: "UseFrameColor", "bool", "",0')
905                 file.write('\n\t\t\tProperty: "FrameColor", "ColorRGB", "",0.3,0.3,0.3')
906                 file.write('\n\t\t\tProperty: "ShowName", "bool", "",1')
907                 file.write('\n\t\t\tProperty: "ShowGrid", "bool", "",1')
908                 file.write('\n\t\t\tProperty: "ShowOpticalCenter", "bool", "",0')
909                 file.write('\n\t\t\tProperty: "ShowAzimut", "bool", "",1')
910                 file.write('\n\t\t\tProperty: "ShowTimeCode", "bool", "",0')
911                 file.write('\n\t\t\tProperty: "NearPlane", "double", "",%.6f' % data.clipStart)
912                 file.write('\n\t\t\tProperty: "FarPlane", "double", "",%.6f' % data.clipStart)
913                 file.write('\n\t\t\tProperty: "FilmWidth", "double", "",1.0')
914                 file.write('\n\t\t\tProperty: "FilmHeight", "double", "",1.0')
915                 file.write('\n\t\t\tProperty: "FilmAspectRatio", "double", "",%.6f' % aspect)
916                 file.write('\n\t\t\tProperty: "FilmSqueezeRatio", "double", "",1')
917                 file.write('\n\t\t\tProperty: "FilmFormatIndex", "enum", "",0')
918                 file.write('\n\t\t\tProperty: "ViewFrustum", "bool", "",1')
919                 file.write('\n\t\t\tProperty: "ViewFrustumNearFarPlane", "bool", "",0')
920                 file.write('\n\t\t\tProperty: "ViewFrustumBackPlaneMode", "enum", "",2')
921                 file.write('\n\t\t\tProperty: "BackPlaneDistance", "double", "",100')
922                 file.write('\n\t\t\tProperty: "BackPlaneDistanceMode", "enum", "",0')
923                 file.write('\n\t\t\tProperty: "ViewCameraToLookAt", "bool", "",1')
924                 file.write('\n\t\t\tProperty: "LockMode", "bool", "",0')
925                 file.write('\n\t\t\tProperty: "LockInterestNavigation", "bool", "",0')
926                 file.write('\n\t\t\tProperty: "FitImage", "bool", "",0')
927                 file.write('\n\t\t\tProperty: "Crop", "bool", "",0')
928                 file.write('\n\t\t\tProperty: "Center", "bool", "",1')
929                 file.write('\n\t\t\tProperty: "KeepRatio", "bool", "",1')
930                 file.write('\n\t\t\tProperty: "BackgroundMode", "enum", "",0')
931                 file.write('\n\t\t\tProperty: "BackgroundAlphaTreshold", "double", "",0.5')
932                 file.write('\n\t\t\tProperty: "ForegroundTransparent", "bool", "",1')
933                 file.write('\n\t\t\tProperty: "DisplaySafeArea", "bool", "",0')
934                 file.write('\n\t\t\tProperty: "SafeAreaDisplayStyle", "enum", "",1')
935                 file.write('\n\t\t\tProperty: "SafeAreaAspectRatio", "double", "",%.6f' % aspect)
936                 file.write('\n\t\t\tProperty: "Use2DMagnifierZoom", "bool", "",0')
937                 file.write('\n\t\t\tProperty: "2D Magnifier Zoom", "Real", "A+",100')
938                 file.write('\n\t\t\tProperty: "2D Magnifier X", "Real", "A+",50')
939                 file.write('\n\t\t\tProperty: "2D Magnifier Y", "Real", "A+",50')
940                 file.write('\n\t\t\tProperty: "CameraProjectionType", "enum", "",0')
941                 file.write('\n\t\t\tProperty: "UseRealTimeDOFAndAA", "bool", "",0')
942                 file.write('\n\t\t\tProperty: "UseDepthOfField", "bool", "",0')
943                 file.write('\n\t\t\tProperty: "FocusSource", "enum", "",0')
944                 file.write('\n\t\t\tProperty: "FocusAngle", "double", "",3.5')
945                 file.write('\n\t\t\tProperty: "FocusDistance", "double", "",200')
946                 file.write('\n\t\t\tProperty: "UseAntialiasing", "bool", "",0')
947                 file.write('\n\t\t\tProperty: "AntialiasingIntensity", "double", "",0.77777')
948                 file.write('\n\t\t\tProperty: "UseAccumulationBuffer", "bool", "",0')
949                 file.write('\n\t\t\tProperty: "FrameSamplingCount", "int", "",7')
950                 
951                 file.write('\n\t\t}')
952                 file.write('\n\t\tMultiLayer: 0')
953                 file.write('\n\t\tMultiTake: 0')
954                 file.write('\n\t\tShading: Y')
955                 file.write('\n\t\tCulling: "CullingOff"')
956                 file.write('\n\t\tTypeFlags: "Camera"')
957                 file.write('\n\t\tGeometryVersion: 124')
958                 file.write('\n\t\tPosition: %.6f,%.6f,%.6f' % loc)
959                 file.write('\n\t\tUp: %.6f,%.6f,%.6f' % tuple(Vector(0,1,0) * matrix_rot) )
960                 file.write('\n\t\tLookAt: %.6f,%.6f,%.6f' % tuple(Vector(0,0,-1)*matrix_rot) )
961                 
962                 #file.write('\n\t\tUp: 0,0,0' )
963                 #file.write('\n\t\tLookAt: 0,0,0' )
964                 
965                 file.write('\n\t\tShowInfoOnMoving: 1')
966                 file.write('\n\t\tShowAudio: 0')
967                 file.write('\n\t\tAudioColor: 0,1,0')
968                 file.write('\n\t\tCameraOrthoZoom: 1')
969                 file.write('\n\t}')
970         
971         def write_light(my_light):
972                 light = my_light.blenObject.data
973                 file.write('\n\tModel: "Model::%s", "Light" {' % my_light.fbxName)
974                 file.write('\n\t\tVersion: 232')
975                 
976                 write_object_props(my_light.blenObject, None, my_light.parRelMatrix())
977                 
978                 # Why are these values here twice?????? - oh well, follow the holy sdk's output
979                 
980                 # Blender light types match FBX's, funny coincidence, we just need to
981                 # be sure that all unsupported types are made into a point light
982                 #ePOINT, 
983                 #eDIRECTIONAL
984                 #eSPOT
985                 light_type = light.type
986                 if light_type > 3: light_type = 0
987                 
988                 mode = light.mode
989                 if mode & Blender.Lamp.Modes.RayShadow or mode & Blender.Lamp.Modes.Shadows:
990                         do_shadow = 1
991                 else:
992                         do_shadow = 0
993                 
994                 if mode & Blender.Lamp.Modes.OnlyShadow or (mode & Blender.Lamp.Modes.NoDiffuse and mode & Blender.Lamp.Modes.NoSpecular):
995                         do_light = 0
996                 else:
997                         do_light = 1
998                 
999                 scale = abs(GLOBAL_MATRIX.scalePart()[0]) # scale is always uniform in this case
1000                 
1001                 file.write('\n\t\t\tProperty: "LightType", "enum", "",%i' % light_type)
1002                 file.write('\n\t\t\tProperty: "CastLightOnObject", "bool", "",1')
1003                 file.write('\n\t\t\tProperty: "DrawVolumetricLight", "bool", "",1')
1004                 file.write('\n\t\t\tProperty: "DrawGroundProjection", "bool", "",1')
1005                 file.write('\n\t\t\tProperty: "DrawFrontFacingVolumetricLight", "bool", "",0')
1006                 file.write('\n\t\t\tProperty: "GoboProperty", "object", ""')
1007                 file.write('\n\t\t\tProperty: "Color", "Color", "A+",1,1,1')
1008                 file.write('\n\t\t\tProperty: "Intensity", "Intensity", "A+",%.2f' % (min(light.energy*100, 200))) # clamp below 200
1009                 file.write('\n\t\t\tProperty: "Cone angle", "Cone angle", "A+",%.2f' % (light.spotSize * scale))
1010                 file.write('\n\t\t\tProperty: "Fog", "Fog", "A+",50')
1011                 file.write('\n\t\t\tProperty: "Color", "Color", "A",%.2f,%.2f,%.2f' % tuple(light.col))
1012                 file.write('\n\t\t\tProperty: "Intensity", "Intensity", "A+",%.2f' % (min(light.energy*100, 200))) # clamp below 200
1013                 file.write('\n\t\t\tProperty: "Cone angle", "Cone angle", "A+",%.2f' % (light.spotSize * scale))
1014                 file.write('\n\t\t\tProperty: "Fog", "Fog", "A+",50')
1015                 file.write('\n\t\t\tProperty: "LightType", "enum", "",%i' % light_type)
1016                 file.write('\n\t\t\tProperty: "CastLightOnObject", "bool", "",%i' % do_light)
1017                 file.write('\n\t\t\tProperty: "DrawGroundProjection", "bool", "",1')
1018                 file.write('\n\t\t\tProperty: "DrawFrontFacingVolumetricLight", "bool", "",0')
1019                 file.write('\n\t\t\tProperty: "DrawVolumetricLight", "bool", "",1')
1020                 file.write('\n\t\t\tProperty: "GoboProperty", "object", ""')
1021                 file.write('\n\t\t\tProperty: "DecayType", "enum", "",0')
1022                 file.write('\n\t\t\tProperty: "DecayStart", "double", "",%.2f' % light.dist)
1023                 file.write('\n\t\t\tProperty: "EnableNearAttenuation", "bool", "",0')
1024                 file.write('\n\t\t\tProperty: "NearAttenuationStart", "double", "",0')
1025                 file.write('\n\t\t\tProperty: "NearAttenuationEnd", "double", "",0')
1026                 file.write('\n\t\t\tProperty: "EnableFarAttenuation", "bool", "",0')
1027                 file.write('\n\t\t\tProperty: "FarAttenuationStart", "double", "",0')
1028                 file.write('\n\t\t\tProperty: "FarAttenuationEnd", "double", "",0')
1029                 file.write('\n\t\t\tProperty: "CastShadows", "bool", "",%i' % do_shadow)
1030                 file.write('\n\t\t\tProperty: "ShadowColor", "ColorRGBA", "",0,0,0,1')
1031                 file.write('\n\t\t}')
1032                 file.write('\n\t\tMultiLayer: 0')
1033                 file.write('\n\t\tMultiTake: 0')
1034                 file.write('\n\t\tShading: Y')
1035                 file.write('\n\t\tCulling: "CullingOff"')
1036                 file.write('\n\t\tTypeFlags: "Light"')
1037                 file.write('\n\t\tGeometryVersion: 124')
1038                 file.write('\n\t}')
1039         
1040         # matrixOnly is not used at the moment
1041         def write_null(my_null = None, fbxName = None, matrixOnly = None):
1042                 # ob can be null
1043                 if not fbxName: fbxName = my_null.fbxName
1044                 
1045                 file.write('\n\tModel: "Model::%s", "Null" {' % fbxName)
1046                 file.write('\n\t\tVersion: 232')
1047                 
1048                 # only use this for the root matrix at the moment
1049                 if matrixOnly:
1050                         poseMatrix = write_object_props(None, None, matrixOnly)[3]
1051                 
1052                 else: # all other Null's
1053                         if my_null:             poseMatrix = write_object_props(my_null.blenObject, None, my_null.parRelMatrix())[3]
1054                         else:                   poseMatrix = write_object_props()[3]
1055                 
1056                 pose_items.append((fbxName, poseMatrix))
1057                 
1058                 file.write('''
1059                 }
1060                 MultiLayer: 0
1061                 MultiTake: 1
1062                 Shading: Y
1063                 Culling: "CullingOff"
1064                 TypeFlags: "Null"
1065         }''')
1066         
1067         # Material Settings
1068         if world:       world_amb = world.getAmb()
1069         else:           world_amb = (0,0,0) # Default value
1070         
1071         def write_material(matname, mat):
1072                 file.write('\n\tMaterial: "Material::%s", "" {' % matname)
1073                 
1074                 # Todo, add more material Properties.
1075                 if mat:
1076                         mat_cold = tuple(mat.rgbCol)
1077                         mat_cols = tuple(mat.specCol)
1078                         #mat_colm = tuple(mat.mirCol) # we wont use the mirror color
1079                         mat_colamb = tuple([c for c in world_amb])
1080                         
1081                         mat_dif = mat.ref
1082                         mat_amb = mat.amb
1083                         mat_hard = (float(mat.hard)-1)/5.10
1084                         mat_spec = mat.spec/2.0
1085                         mat_alpha = mat.alpha
1086                         mat_emit = mat.emit
1087                         mat_shadeless = mat.mode & Blender.Material.Modes.SHADELESS
1088                         if mat_shadeless:
1089                                 mat_shader = 'Lambert'
1090                         else:
1091                                 if mat.diffuseShader == Blender.Material.Shaders.DIFFUSE_LAMBERT:
1092                                         mat_shader = 'Lambert'
1093                                 else:
1094                                         mat_shader = 'Phong'
1095                 else:
1096                         mat_cols = mat_cold = 0.8, 0.8, 0.8
1097                         mat_colamb = 0.0,0.0,0.0
1098                         # mat_colm 
1099                         mat_dif = 1.0
1100                         mat_amb = 0.5
1101                         mat_hard = 20.0
1102                         mat_spec = 0.2
1103                         mat_alpha = 1.0
1104                         mat_emit = 0.0
1105                         mat_shadeless = False
1106                         mat_shader = 'Phong'
1107                 
1108                 file.write('\n\t\tVersion: 102')
1109                 file.write('\n\t\tShadingModel: "%s"' % mat_shader.lower())
1110                 file.write('\n\t\tMultiLayer: 0')
1111                 
1112                 file.write('\n\t\tProperties60:  {')
1113                 file.write('\n\t\t\tProperty: "ShadingModel", "KString", "", "%s"' % mat_shader)
1114                 file.write('\n\t\t\tProperty: "MultiLayer", "bool", "",0')
1115                 file.write('\n\t\t\tProperty: "EmissiveColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_cold) # emit and diffuse color are he same in blender
1116                 file.write('\n\t\t\tProperty: "EmissiveFactor", "double", "",%.4f' % mat_emit)
1117                 
1118                 file.write('\n\t\t\tProperty: "AmbientColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_colamb)
1119                 file.write('\n\t\t\tProperty: "AmbientFactor", "double", "",%.4f' % mat_amb)
1120                 file.write('\n\t\t\tProperty: "DiffuseColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_cold)
1121                 file.write('\n\t\t\tProperty: "DiffuseFactor", "double", "",%.4f' % mat_dif)
1122                 file.write('\n\t\t\tProperty: "Bump", "Vector3D", "",0,0,0')
1123                 file.write('\n\t\t\tProperty: "TransparentColor", "ColorRGB", "",1,1,1')
1124                 file.write('\n\t\t\tProperty: "TransparencyFactor", "double", "",%.4f' % (1.0 - mat_alpha))
1125                 if not mat_shadeless:
1126                         file.write('\n\t\t\tProperty: "SpecularColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_cols)
1127                         file.write('\n\t\t\tProperty: "SpecularFactor", "double", "",%.4f' % mat_spec)
1128                         file.write('\n\t\t\tProperty: "ShininessExponent", "double", "",80.0')
1129                         file.write('\n\t\t\tProperty: "ReflectionColor", "ColorRGB", "",0,0,0')
1130                         file.write('\n\t\t\tProperty: "ReflectionFactor", "double", "",1')
1131                 file.write('\n\t\t\tProperty: "Emissive", "ColorRGB", "",0,0,0')
1132                 file.write('\n\t\t\tProperty: "Ambient", "ColorRGB", "",%.1f,%.1f,%.1f' % mat_colamb)
1133                 file.write('\n\t\t\tProperty: "Diffuse", "ColorRGB", "",%.1f,%.1f,%.1f' % mat_cold)
1134                 if not mat_shadeless:
1135                         file.write('\n\t\t\tProperty: "Specular", "ColorRGB", "",%.1f,%.1f,%.1f' % mat_cols)
1136                         file.write('\n\t\t\tProperty: "Shininess", "double", "",%.1f' % mat_hard)
1137                 file.write('\n\t\t\tProperty: "Opacity", "double", "",%.1f' % mat_alpha)
1138                 if not mat_shadeless:
1139                         file.write('\n\t\t\tProperty: "Reflectivity", "double", "",0')
1140
1141                 file.write('\n\t\t}')
1142                 file.write('\n\t}')
1143         
1144         def write_video(texname, tex):
1145                 # Same as texture really!
1146                 file.write('\n\tVideo: "Video::%s", "Clip" {' % texname)
1147                 
1148                 file.write('''
1149                 Type: "Clip"
1150                 Properties60:  {
1151                         Property: "FrameRate", "double", "",0
1152                         Property: "LastFrame", "int", "",0
1153                         Property: "Width", "int", "",0
1154                         Property: "Height", "int", "",0''')
1155                 if tex:
1156                         fname, fname_strip, fname_rel = derived_paths(tex.filename, basepath, EXP_IMAGE_COPY)
1157                 else:
1158                         fname = fname_strip = fname_rel = ''
1159                 
1160                 file.write('\n\t\t\tProperty: "Path", "charptr", "", "%s"' % fname_strip)
1161                 
1162                 
1163                 file.write('''
1164                         Property: "StartFrame", "int", "",0
1165                         Property: "StopFrame", "int", "",0
1166                         Property: "PlaySpeed", "double", "",1
1167                         Property: "Offset", "KTime", "",0
1168                         Property: "InterlaceMode", "enum", "",0
1169                         Property: "FreeRunning", "bool", "",0
1170                         Property: "Loop", "bool", "",0
1171                         Property: "AccessMode", "enum", "",0
1172                 }
1173                 UseMipMap: 0''')
1174                 
1175                 file.write('\n\t\tFilename: "%s"' % fname_strip)
1176                 if fname_strip: fname_strip = '/' + fname_strip
1177                 file.write('\n\t\tRelativeFilename: "%s"' % fname_rel) # make relative
1178                 file.write('\n\t}')
1179
1180         
1181         def write_texture(texname, tex, num):
1182                 # if tex == None then this is a dummy tex
1183                 file.write('\n\tTexture: "Texture::%s", "TextureVideoClip" {' % texname)
1184                 file.write('\n\t\tType: "TextureVideoClip"')
1185                 file.write('\n\t\tVersion: 202')
1186                 # TODO, rare case _empty_ exists as a name.
1187                 file.write('\n\t\tTextureName: "Texture::%s"' % texname)
1188                 
1189                 file.write('''
1190                 Properties60:  {
1191                         Property: "Translation", "Vector", "A+",0,0,0
1192                         Property: "Rotation", "Vector", "A+",0,0,0
1193                         Property: "Scaling", "Vector", "A+",1,1,1''')
1194                 file.write('\n\t\t\tProperty: "Texture alpha", "Number", "A+",%i' % num)
1195                 
1196                 
1197                 # WrapModeU/V 0==rep, 1==clamp, TODO add support
1198                 file.write('''
1199                         Property: "TextureTypeUse", "enum", "",0
1200                         Property: "CurrentTextureBlendMode", "enum", "",1
1201                         Property: "UseMaterial", "bool", "",0
1202                         Property: "UseMipMap", "bool", "",0
1203                         Property: "CurrentMappingType", "enum", "",0
1204                         Property: "UVSwap", "bool", "",0''')
1205                 
1206                 file.write('\n\t\t\tProperty: "WrapModeU", "enum", "",%i' % tex.clampX)
1207                 file.write('\n\t\t\tProperty: "WrapModeV", "enum", "",%i' % tex.clampY)
1208                 
1209                 file.write('''
1210                         Property: "TextureRotationPivot", "Vector3D", "",0,0,0
1211                         Property: "TextureScalingPivot", "Vector3D", "",0,0,0
1212                         Property: "VideoProperty", "object", ""
1213                 }''')
1214                 
1215                 file.write('\n\t\tMedia: "Video::%s"' % texname)
1216                 
1217                 if tex:
1218                         fname, fname_strip, fname_rel = derived_paths(tex.filename, basepath, EXP_IMAGE_COPY)
1219                 else:
1220                         fname = fname_strip = fname_rel = ''
1221                 
1222                 file.write('\n\t\tFileName: "%s"' % fname_strip)
1223                 file.write('\n\t\tRelativeFilename: "%s"' % fname_rel) # need some make relative command
1224                 
1225                 file.write('''
1226                 ModelUVTranslation: 0,0
1227                 ModelUVScaling: 1,1
1228                 Texture_Alpha_Source: "None"
1229                 Cropping: 0,0,0,0
1230         }''')
1231
1232         def write_deformer_skin(obname):
1233                 '''
1234                 Each mesh has its own deformer
1235                 '''
1236                 file.write('\n\tDeformer: "Deformer::Skin %s", "Skin" {' % obname)
1237                 file.write('''
1238                 Version: 100
1239                 MultiLayer: 0
1240                 Type: "Skin"
1241                 Properties60:  {
1242                 }
1243                 Link_DeformAcuracy: 50
1244         }''')
1245         
1246         # in the example was 'Bip01 L Thigh_2'
1247         def write_sub_deformer_skin(my_mesh, my_bone, weights):
1248         
1249                 '''
1250                 Each subdeformer is spesific to a mesh, but the bone it links to can be used by many sub-deformers
1251                 So the SubDeformer needs the mesh-object name as a prefix to make it unique
1252                 
1253                 Its possible that there is no matching vgroup in this mesh, in that case no verts are in the subdeformer,
1254                 a but silly but dosnt really matter
1255                 '''
1256                 file.write('\n\tDeformer: "SubDeformer::Cluster %s %s", "Cluster" {' % (my_mesh.fbxName, my_bone.fbxName))
1257                 
1258                 file.write('''
1259                 Version: 100
1260                 MultiLayer: 0
1261                 Type: "Cluster"
1262                 Properties60:  {
1263                         Property: "SrcModel", "object", ""
1264                         Property: "SrcModelReference", "object", ""
1265                 }
1266                 UserData: "", ""''')
1267                 
1268                 # Support for bone parents
1269                 if my_mesh.fbxBoneParent:
1270                         if my_mesh.fbxBoneParent == my_bone:
1271                                 # TODO - this is a bit lazy, we could have a simple write loop
1272                                 # for this case because all weights are 1.0 but for now this is ok
1273                                 # Parent Bones arent used all that much anyway.
1274                                 vgroup_data = [(j, 1.0) for j in xrange(len(my_mesh.blenData.verts))]
1275                         else:
1276                                 # This bone is not a parent of this mesh object, no weights
1277                                 vgroup_data = []
1278                         
1279                 else:
1280                         # Normal weight painted mesh
1281                         if my_bone.blenName in weights[0]:
1282                                 # Before we used normalized wright list
1283                                 #vgroup_data = me.getVertsFromGroup(bone.name, 1)
1284                                 group_index = weights[0].index(my_bone.blenName)
1285                                 vgroup_data = [(j, weight[group_index]) for j, weight in enumerate(weights[1]) if weight[group_index]] 
1286                         else:
1287                                 vgroup_data = []
1288                 
1289                 file.write('\n\t\tIndexes: ')
1290                 
1291                 i = -1
1292                 for vg in vgroup_data:
1293                         if i == -1:
1294                                 file.write('%i'  % vg[0])
1295                                 i=0
1296                         else:
1297                                 if i==23:
1298                                         file.write('\n\t\t')
1299                                         i=0
1300                                 file.write(',%i' % vg[0])
1301                         i+=1
1302                 
1303                 file.write('\n\t\tWeights: ')
1304                 i = -1
1305                 for vg in vgroup_data:
1306                         if i == -1:
1307                                 file.write('%.8f'  % vg[1])
1308                                 i=0
1309                         else:
1310                                 if i==38:
1311                                         file.write('\n\t\t')
1312                                         i=0
1313                                 file.write(',%.8f' % vg[1])
1314                         i+=1
1315                 
1316                 if my_mesh.fbxParent:
1317                         # TODO FIXME, this case is broken in some cases. skinned meshes just shouldnt have parents where possible!
1318                         m = mtx4_z90 * (my_bone.restMatrix * my_bone.fbxArm.matrixWorld.copy() * my_mesh.matrixWorld.copy().invert() )
1319                 else:
1320                         # Yes! this is it...  - but dosnt work when the mesh is a.
1321                         m = mtx4_z90 * (my_bone.restMatrix * my_bone.fbxArm.matrixWorld.copy() * my_mesh.matrixWorld.copy().invert() )
1322                 
1323                 #m = mtx4_z90 * my_bone.restMatrix
1324                 matstr = mat4x4str(m)
1325                 matstr_i = mat4x4str(m.invert())
1326                 
1327                 file.write('\n\t\tTransform: %s' % matstr_i) # THIS IS __NOT__ THE GLOBAL MATRIX AS DOCUMENTED :/
1328                 file.write('\n\t\tTransformLink: %s' % matstr)
1329                 file.write('\n\t}')
1330         
1331         def write_mesh(my_mesh):
1332                 
1333                 me = my_mesh.blenData
1334                 
1335                 # if there are non NULL materials on this mesh
1336                 if [mat for mat in my_mesh.blenMaterials if mat]:       do_materials = True
1337                 else:                                                                                           do_materials = False
1338                 
1339                 if my_mesh.blenTextures:        do_textures = True
1340                 else:                                           do_textures = False                     
1341                 
1342                 
1343                 file.write('\n\tModel: "Model::%s", "Mesh" {' % my_mesh.fbxName)
1344                 file.write('\n\t\tVersion: 232') # newline is added in write_object_props
1345                 
1346                 write_object_props(my_mesh.blenObject, None, my_mesh.parRelMatrix())
1347                 
1348                 file.write('\n\t\t}')
1349                 file.write('\n\t\tMultiLayer: 0')
1350                 file.write('\n\t\tMultiTake: 1')
1351                 file.write('\n\t\tShading: Y')
1352                 file.write('\n\t\tCulling: "CullingOff"')
1353                 
1354                 
1355                 # Write the Real Mesh data here
1356                 file.write('\n\t\tVertices: ')
1357                 i=-1
1358                 
1359                 for v in me.verts:
1360                         if i==-1:
1361                                 file.write('%.6f,%.6f,%.6f' % tuple(v.co));     i=0
1362                         else:
1363                                 if i==7:
1364                                         file.write('\n\t\t');   i=0
1365                                 file.write(',%.6f,%.6f,%.6f'% tuple(v.co))
1366                         i+=1
1367                                 
1368                 file.write('\n\t\tPolygonVertexIndex: ')
1369                 i=-1
1370                 for f in me.faces:
1371                         fi = [v.index for v in f]
1372                         # flip the last index, odd but it looks like
1373                         # this is how fbx tells one face from another
1374                         fi[-1] = -(fi[-1]+1)
1375                         fi = tuple(fi)
1376                         if i==-1:
1377                                 if len(f) == 3:         file.write('%i,%i,%i' % fi )
1378                                 else:                           file.write('%i,%i,%i,%i' % fi )
1379                                 i=0
1380                         else:
1381                                 if i==13:
1382                                         file.write('\n\t\t')
1383                                         i=0
1384                                 if len(f) == 3:         file.write(',%i,%i,%i' % fi )
1385                                 else:                           file.write(',%i,%i,%i,%i' % fi )
1386                         i+=1
1387                 
1388                 ed_val = [None, None]
1389                 LOOSE = Blender.Mesh.EdgeFlags.LOOSE
1390                 for ed in me.edges:
1391                         if ed.flag & LOOSE:
1392                                 ed_val[0] = ed.v1.index
1393                                 ed_val[1] = -(ed.v2.index+1)
1394                                 if i==-1:
1395                                         file.write('%i,%i' % tuple(ed_val) )
1396                                         i=0
1397                                 else:
1398                                         if i==13:
1399                                                 file.write('\n\t\t')
1400                                                 i=0
1401                                         file.write(',%i,%i' % tuple(ed_val) )
1402                                 i+=1
1403                 del LOOSE
1404                 
1405                 file.write('\n\t\tGeometryVersion: 124')
1406                 
1407                 file.write('''
1408                 LayerElementNormal: 0 {
1409                         Version: 101
1410                         Name: ""
1411                         MappingInformationType: "ByVertice"
1412                         ReferenceInformationType: "Direct"
1413                         Normals: ''')
1414                 
1415                 i=-1
1416                 for v in me.verts:
1417                         if i==-1:
1418                                 file.write('%.15f,%.15f,%.15f' % tuple(v.no));  i=0
1419                         else:
1420                                 if i==2:
1421                                         file.write('\n                   ');    i=0
1422                                 file.write(',%.15f,%.15f,%.15f' % tuple(v.no))
1423                         i+=1
1424                 file.write('\n\t\t}')
1425                 
1426                 # Write VertexColor Layers
1427                 # note, no programs seem to use this info :/
1428                 collayers = []
1429                 if me.vertexColors:
1430                         collayers = me.getColorLayerNames()
1431                         collayer_orig = me.activeColorLayer
1432                         for colindex, collayer in enumerate(collayers):
1433                                 me.activeColorLayer = collayer
1434                                 file.write('\n\t\tLayerElementColor: %i {' % colindex)
1435                                 file.write('\n\t\t\tVersion: 101')
1436                                 file.write('\n\t\t\tName: "%s"' % collayer)
1437                                 
1438                                 file.write('''
1439                         MappingInformationType: "ByPolygonVertex"
1440                         ReferenceInformationType: "IndexToDirect"
1441                         Colors: ''')
1442                         
1443                                 i = -1
1444                                 ii = 0 # Count how many Colors we write
1445                                 
1446                                 for f in me.faces:
1447                                         for col in f.col:
1448                                                 if i==-1:
1449                                                         file.write('%i,%i,%i' % (col[0], col[1], col[2]))
1450                                                         i=0
1451                                                 else:
1452                                                         if i==7:
1453                                                                 file.write('\n\t\t\t\t')
1454                                                                 i=0
1455                                                         file.write(',%i,%i,%i' % (col[0], col[1], col[2]))
1456                                                 i+=1
1457                                                 ii+=1 # One more Color
1458                                 
1459                                 file.write('\n\t\t\tColorIndex: ')
1460                                 i = -1
1461                                 for j in xrange(ii):
1462                                         if i == -1:
1463                                                 file.write('%i' % j)
1464                                                 i=0
1465                                         else:
1466                                                 if i==55:
1467                                                         file.write('\n\t\t\t\t')
1468                                                         i=0
1469                                                 file.write(',%i' % j)
1470                                         i+=1
1471                                 
1472                                 file.write('\n\t\t}')
1473                 
1474                 
1475                 
1476                 # Write UV and texture layers.
1477                 uvlayers = []
1478                 if me.faceUV:
1479                         uvlayers = me.getUVLayerNames()
1480                         uvlayer_orig = me.activeUVLayer
1481                         for uvindex, uvlayer in enumerate(uvlayers):
1482                                 me.activeUVLayer = uvlayer
1483                                 file.write('\n\t\tLayerElementUV: %i {' % uvindex)
1484                                 file.write('\n\t\t\tVersion: 101')
1485                                 file.write('\n\t\t\tName: "%s"' % uvlayer)
1486                                 
1487                                 file.write('''
1488                         MappingInformationType: "ByPolygonVertex"
1489                         ReferenceInformationType: "IndexToDirect"
1490                         UV: ''')
1491                         
1492                                 i = -1
1493                                 ii = 0 # Count how many UVs we write
1494                                 
1495                                 for f in me.faces:
1496                                         for uv in f.uv:
1497                                                 if i==-1:
1498                                                         file.write('%.6f,%.6f' % tuple(uv))
1499                                                         i=0
1500                                                 else:
1501                                                         if i==7:
1502                                                                 file.write('\n                   ')
1503                                                                 i=0
1504                                                         file.write(',%.6f,%.6f' % tuple(uv))
1505                                                 i+=1
1506                                                 ii+=1 # One more UV
1507                                 
1508                                 file.write('\n\t\t\tUVIndex: ')
1509                                 i = -1
1510                                 for j in xrange(ii):
1511                                         if i == -1:
1512                                                 file.write('%i'  % j)
1513                                                 i=0
1514                                         else:
1515                                                 if i==55:
1516                                                         file.write('\n\t\t\t\t')
1517                                                         i=0
1518                                                 file.write(',%i' % j)
1519                                         i+=1
1520                                 
1521                                 file.write('\n\t\t}')
1522                                 
1523                                 if do_textures:
1524                                         file.write('\n\t\tLayerElementTexture: %i {' % uvindex)
1525                                         file.write('\n\t\t\tVersion: 101')
1526                                         file.write('\n\t\t\tName: "%s"' % uvlayer)
1527                                         
1528                                         if len(my_mesh.blenTextures) == 1:
1529                                                 file.write('\n\t\t\tMappingInformationType: "AllSame"')
1530                                         else:
1531                                                 file.write('\n\t\t\tMappingInformationType: "ByPolygon"')
1532                                         
1533                                         file.write('\n\t\t\tReferenceInformationType: "IndexToDirect"')
1534                                         file.write('\n\t\t\tBlendMode: "Translucent"')
1535                                         file.write('\n\t\t\tTextureAlpha: 1')
1536                                         file.write('\n\t\t\tTextureId: ')
1537                                         
1538                                         if len(my_mesh.blenTextures) == 1:
1539                                                 file.write('0')
1540                                         else:
1541                                                 #texture_mapping_local = {None:0}
1542                                                 texture_mapping_local = {None:-1}
1543                                                 
1544                                                 i = 0 # 1 for dummy
1545                                                 for tex in my_mesh.blenTextures:
1546                                                         texture_mapping_local[tex] = i
1547                                                         i+=1
1548                                                 
1549                                                 i=-1
1550                                                 for f in me.faces:
1551                                                         img_key = f.image
1552                                                         
1553                                                         if i==-1:
1554                                                                 i=0
1555                                                                 file.write( '%s' % texture_mapping_local[img_key])
1556                                                         else:
1557                                                                 if i==55:
1558                                                                         file.write('\n                   ')
1559                                                                         i=0
1560                                                                 
1561                                                                 file.write(',%s' % texture_mapping_local[img_key])
1562                                                         i+=1
1563                                 
1564                                 else:
1565                                         file.write('''
1566                 LayerElementTexture: 0 {
1567                         Version: 101
1568                         Name: ""
1569                         MappingInformationType: "NoMappingInformation"
1570                         ReferenceInformationType: "IndexToDirect"
1571                         BlendMode: "Translucent"
1572                         TextureAlpha: 1
1573                         TextureId: ''')
1574                                 file.write('\n\t\t}')
1575                         
1576                         me.activeUVLayer = uvlayer_orig
1577                         
1578                 # Done with UV/textures.
1579                 
1580                 if do_materials:
1581                         file.write('\n\t\tLayerElementMaterial: 0 {')
1582                         file.write('\n\t\t\tVersion: 101')
1583                         file.write('\n\t\t\tName: ""')
1584                         
1585                         if len(my_mesh.blenMaterials) == 1:
1586                                 file.write('\n\t\t\tMappingInformationType: "AllSame"')
1587                         else:
1588                                 file.write('\n\t\t\tMappingInformationType: "ByPolygon"')
1589                         
1590                         file.write('\n\t\t\tReferenceInformationType: "IndexToDirect"')
1591                         file.write('\n\t\t\tMaterials: ')
1592                         
1593                         if len(my_mesh.blenMaterials) == 1:
1594                                 file.write('0')
1595                         else:
1596                                 # Build a material mapping for this 
1597                                 #material_mapping_local = [0] * 16 # local-index : global index.
1598                                 material_mapping_local = [-1] * 16 # local-index : global index.
1599                                 i= 0 # 1
1600                                 for j, mat in enumerate(my_mesh.blenMaterials):
1601                                         if mat:
1602                                                 material_mapping_local[j] = i
1603                                                 i+=1
1604                                         # else leave as -1
1605                                 
1606                                 len_material_mapping_local = len(material_mapping_local)
1607                                 
1608                                 i=-1
1609                                 for f in me.faces:
1610                                         f_mat = f.mat
1611                                         if f_mat >= len_material_mapping_local:
1612                                                 f_mat = 0
1613                                         
1614                                         if i==-1:
1615                                                 i=0
1616                                                 file.write( '%s' % (material_mapping_local[f_mat]))
1617                                         else:
1618                                                 if i==55:
1619                                                         file.write('\n\t\t\t\t')
1620                                                         i=0
1621                                                 
1622                                                 file.write(',%s' % (material_mapping_local[f_mat]))
1623                                         i+=1
1624                         
1625                         file.write('\n\t\t}')
1626                 
1627                 file.write('''
1628                 Layer: 0 {
1629                         Version: 100
1630                         LayerElement:  {
1631                                 Type: "LayerElementNormal"
1632                                 TypedIndex: 0
1633                         }''')
1634                 
1635                 if do_materials:
1636                         file.write('''
1637                         LayerElement:  {
1638                                 Type: "LayerElementMaterial"
1639                                 TypedIndex: 0
1640                         }''')
1641                         
1642                 # Always write this
1643                 if do_textures:
1644                         file.write('''
1645                         LayerElement:  {
1646                                 Type: "LayerElementTexture"
1647                                 TypedIndex: 0
1648                         }''')
1649                 
1650                 if me.vertexColors:
1651                         file.write('''
1652                         LayerElement:  {
1653                                 Type: "LayerElementColor"
1654                                 TypedIndex: 0
1655                         }''')
1656                 
1657                 if me.faceUV:
1658                         file.write('''
1659                         LayerElement:  {
1660                                 Type: "LayerElementUV"
1661                                 TypedIndex: 0
1662                         }''')
1663                 
1664                 
1665                 file.write('\n\t\t}')
1666                 
1667                 if len(uvlayers) > 1:
1668                         for i in xrange(1, len(uvlayers)):
1669                                 
1670                                 file.write('\n\t\tLayer: %i {' % i)
1671                                 file.write('\n\t\t\tVersion: 100')
1672                                 
1673                                 file.write('''
1674                         LayerElement:  {
1675                                 Type: "LayerElementUV"''')
1676                                 
1677                                 file.write('\n\t\t\t\tTypedIndex: %i' % i)
1678                                 file.write('\n\t\t\t}')
1679                                 
1680                                 if do_textures:
1681                                         
1682                                         file.write('''
1683                         LayerElement:  {
1684                                 Type: "LayerElementTexture"''')
1685                                         
1686                                         file.write('\n\t\t\t\tTypedIndex: %i' % i)
1687                                         file.write('\n\t\t\t}')
1688                                 
1689                                 file.write('\n\t\t}')
1690                 
1691                 if len(collayers) > 1:
1692                         # Take into account any UV layers
1693                         layer_offset = 0
1694                         if uvlayers: layer_offset = len(uvlayers)-1
1695                         
1696                         for i in xrange(layer_offset, len(collayers)+layer_offset):
1697                                 file.write('\n\t\tLayer: %i {' % i)
1698                                 file.write('\n\t\t\tVersion: 100')
1699                                 
1700                                 file.write('''
1701                         LayerElement:  {
1702                                 Type: "LayerElementColor"''')
1703                                 
1704                                 file.write('\n\t\t\t\tTypedIndex: %i' % i)
1705                                 file.write('\n\t\t\t}')
1706                                 file.write('\n\t\t}')
1707                 file.write('\n\t}')
1708         
1709         def write_group(name):
1710                 file.write('\n\tGroupSelection: "GroupSelection::%s", "Default" {' % name)
1711                 
1712                 file.write('''
1713                 Properties60:  {
1714                         Property: "MultiLayer", "bool", "",0
1715                         Property: "Pickable", "bool", "",1
1716                         Property: "Transformable", "bool", "",1
1717                         Property: "Show", "bool", "",1
1718                 }
1719                 MultiLayer: 0
1720         }''')
1721         
1722         
1723         # add meshes here to clear because they are not used anywhere.
1724         meshes_to_clear = []
1725         
1726         ob_meshes = []  
1727         ob_lights = []
1728         ob_cameras = []
1729         # in fbx we export bones as children of the mesh
1730         # armatures not a part of a mesh, will be added to ob_arms
1731         ob_bones = [] 
1732         ob_arms = []
1733         ob_null = [] # emptys
1734         
1735         # List of types that have blender objects (not bones)
1736         ob_all_typegroups = [ob_meshes, ob_lights, ob_cameras, ob_arms, ob_null]
1737         
1738         groups = [] # blender groups, only add ones that have objects in the selections
1739         materials = {}
1740         textures = {}
1741         
1742         tmp_ob_type = ob_type = None # incase no objects are exported, so as not to raise an error
1743         
1744         # if EXP_OBS_SELECTED is false, use sceens objects
1745         if not batch_objects:
1746                 if EXP_OBS_SELECTED:    tmp_objects = sce.objects.context
1747                 else:                                   tmp_objects = sce.objects
1748         else:
1749                 tmp_objects = batch_objects
1750         
1751         if EXP_ARMATURE:
1752                 # This is needed so applying modifiers dosnt apply the armature deformation, its also needed
1753                 # ...so mesh objects return their rest worldspace matrix when bone-parents are exported as weighted meshes.
1754                 # set every armature to its rest, backup the original values so we done mess up the scene
1755                 ob_arms_orig_rest = [arm.restPosition for arm in bpy.data.armatures]
1756                 
1757                 for arm in bpy.data.armatures:
1758                         arm.restPosition = True
1759                 
1760                 if ob_arms_orig_rest:
1761                         for ob_base in bpy.data.objects:
1762                                 #if ob_base.type == 'Armature':
1763                                 ob_base.makeDisplayList()
1764                                         
1765                         # This causes the makeDisplayList command to effect the mesh
1766                         Blender.Set('curframe', Blender.Get('curframe'))
1767                         
1768         
1769         for ob_base in tmp_objects:
1770                 for ob, mtx in BPyObject.getDerivedObjects(ob_base):
1771                         #for ob in [ob_base,]:
1772                         tmp_ob_type = ob.type
1773                         if tmp_ob_type == 'Camera':
1774                                 if EXP_CAMERA:
1775                                         ob_cameras.append(my_object_generic(ob, mtx))
1776                         elif tmp_ob_type == 'Lamp':
1777                                 if EXP_LAMP:
1778                                         ob_lights.append(my_object_generic(ob, mtx))
1779                         elif tmp_ob_type == 'Armature':
1780                                 if EXP_ARMATURE:
1781                                         # TODO - armatures dont work in dupligroups!
1782                                         if ob not in ob_arms: ob_arms.append(ob)
1783                                         # ob_arms.append(ob) # replace later. was "ob_arms.append(sane_obname(ob), ob)"
1784                         elif tmp_ob_type == 'Empty':
1785                                 if EXP_EMPTY:
1786                                         ob_null.append(my_object_generic(ob, mtx))
1787                         elif EXP_MESH:
1788                                 origData = True
1789                                 if tmp_ob_type != 'Mesh':
1790                                         me = bpy.data.meshes.new()
1791                                         try:    me.getFromObject(ob)
1792                                         except: me = None
1793                                         if me:
1794                                                 meshes_to_clear.append( me )
1795                                                 mats = me.materials
1796                                                 origData = False
1797                                 else:
1798                                         # Mesh Type!
1799                                         if EXP_MESH_APPLY_MOD:
1800                                                 me = bpy.data.meshes.new()
1801                                                 me.getFromObject(ob)
1802                                                 
1803                                                 # so we keep the vert groups
1804                                                 if EXP_ARMATURE:
1805                                                         orig_mesh = ob.getData(mesh=1)
1806                                                         if orig_mesh.getVertGroupNames():
1807                                                                 ob.copy().link(me)
1808                                                                 # If new mesh has no vgroups we can try add if verts are teh same
1809                                                                 if not me.getVertGroupNames(): # vgroups were not kept by the modifier
1810                                                                         if len(me.verts) == len(orig_mesh.verts):
1811                                                                                 groupNames, vWeightDict = BPyMesh.meshWeight2Dict(orig_mesh)
1812                                                                                 BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict)
1813                                                 
1814                                                 # print ob, me, me.getVertGroupNames()
1815                                                 meshes_to_clear.append( me )
1816                                                 origData = False
1817                                                 mats = me.materials
1818                                         else:
1819                                                 me = ob.getData(mesh=1)
1820                                                 mats = me.materials
1821                                                 
1822                                                 # Support object colors
1823                                                 tmp_colbits = ob.colbits
1824                                                 if tmp_colbits:
1825                                                         tmp_ob_mats = ob.getMaterials(1) # 1 so we get None's too.
1826                                                         for i in xrange(16):
1827                                                                 if tmp_colbits & (1<<i):
1828                                                                         mats[i] = tmp_ob_mats[i]
1829                                                         del tmp_ob_mats
1830                                                 del tmp_colbits
1831                                                         
1832                                         
1833                                 if me:
1834                                         # This WILL modify meshes in blender if EXP_MESH_APPLY_MOD is disabled.
1835                                         # so strictly this is bad. but only in rare cases would it have negative results
1836                                         # say with dupliverts the objects would rotate a bit differently
1837                                         if EXP_MESH_HQ_NORMALS:
1838                                                 BPyMesh.meshCalcNormals(me) # high quality normals nice for realtime engines.
1839                                         
1840                                         for mat in mats:
1841                                                 # 2.44 use mat.lib too for uniqueness
1842                                                 if mat: materials[mat] = mat
1843                                         
1844                                         texture_mapping_local = {}
1845                                         if me.faceUV:
1846                                                 uvlayer_orig = me.activeUVLayer
1847                                                 for uvlayer in me.getUVLayerNames():
1848                                                         me.activeUVLayer = uvlayer
1849                                                         for f in me.faces:
1850                                                                 img = f.image
1851                                                                 textures[img] = texture_mapping_local[img] = img
1852                                                         
1853                                                         me.activeUVLayer = uvlayer_orig
1854                                         
1855                                         if EXP_ARMATURE:
1856                                                 armob = BPyObject.getObjectArmature(ob)
1857                                                 blenParentBoneName = None
1858                                                 
1859                                                 # Note - Fixed in BPyObject but for now just copy the function because testers wont have up to date modukes,
1860                                                 # TODO - remove this for 2.45 release since getObjectArmature has been fixed
1861                                                 if (not armob) and ob.parent and ob.parent.type == 'Armature' and ob.parentType == Blender.Object.ParentTypes.ARMATURE:
1862                                                         armob = ob.parent
1863                                                 
1864                                                 # parent bone - special case
1865                                                 if (not armob) and ob.parent and ob.parent.type == 'Armature' and ob.parentType == Blender.Object.ParentTypes.BONE:
1866                                                         armob = ob.parent
1867                                                         blenParentBoneName = ob.parentbonename
1868                                                 
1869                                                         
1870                                                 if armob and armob not in ob_arms:
1871                                                         ob_arms.append(armob)
1872                                         
1873                                         else:
1874                                                 blenParentBoneName = armob = None
1875                                         
1876                                         my_mesh = my_object_generic(ob, mtx)
1877                                         my_mesh.blenData =              me
1878                                         my_mesh.origData =              origData
1879                                         my_mesh.blenMaterials = mats
1880                                         my_mesh.blenTextures =  texture_mapping_local.values()
1881                                         
1882                                         # if only 1 null texture then empty the list
1883                                         if len(my_mesh.blenTextures) == 1 and my_mesh.blenTextures[0] == None:
1884                                                 my_mesh.blenTextures = []
1885                                         
1886                                         my_mesh.fbxArm =        armob                                   # replace with my_object_generic armature instance later
1887                                         my_mesh.fbxBoneParent = blenParentBoneName      # replace with my_bone instance later
1888                                         
1889                                         ob_meshes.append( my_mesh )
1890         
1891         if EXP_ARMATURE:
1892                 # now we have the meshes, restore the rest arm position
1893                 for i, arm in enumerate(bpy.data.armatures):
1894                         arm.restPosition = ob_arms_orig_rest[i]
1895                         
1896                 if ob_arms_orig_rest:
1897                         for ob_base in bpy.data.objects:
1898                                 if ob_base.type == 'Armature':
1899                                         ob_base.makeDisplayList()
1900                         # This causes the makeDisplayList command to effect the mesh
1901                         Blender.Set('curframe', Blender.Get('curframe'))
1902         
1903         del tmp_ob_type, tmp_objects
1904         
1905         # now we have collected all armatures, add bones
1906         for i, ob in enumerate(ob_arms):
1907                 
1908                 ob_arms[i] = my_arm = my_object_generic(ob)
1909                 
1910                 my_arm.fbxBones =               []
1911                 my_arm.blenData =               ob.data
1912                 my_arm.blenAction =             ob.action
1913                 my_arm.blenActionList = []
1914                 
1915                 # fbxName, blenderObject, my_bones, blenderActions
1916                 #ob_arms[i] = fbxArmObName, ob, arm_my_bones, (ob.action, [])
1917                 
1918                 for bone in my_arm.blenData.bones.values():
1919                         my_bone = my_bone_class(bone, my_arm)
1920                         my_arm.fbxBones.append( my_bone )
1921                         ob_bones.append( my_bone )
1922         
1923         # add the meshes to the bones and replace the meshes armature with own armature class
1924         #for obname, ob, mtx, me, mats, arm, armname in ob_meshes:
1925         for my_mesh in ob_meshes:
1926                 # Replace 
1927                 # ...this could be sped up with dictionary mapping but its unlikely for
1928                 # it ever to be a bottleneck - (would need 100+ meshes using armatures)
1929                 if my_mesh.fbxArm:
1930                         for my_arm in ob_arms:
1931                                 if my_arm.blenObject == my_mesh.fbxArm:
1932                                         my_mesh.fbxArm = my_arm
1933                                         break
1934                 
1935                 for my_bone in ob_bones:
1936                         
1937                         # The mesh uses this bones armature!
1938                         if my_bone.fbxArm == my_mesh.fbxArm:
1939                                 my_bone.blenMeshes[my_mesh.fbxName] = me
1940                                 
1941                                 
1942                                 # parent bone: replace bone names with our class instances
1943                                 # my_mesh.fbxBoneParent is None or a blender bone name initialy, replacing if the names match.
1944                                 if my_mesh.fbxBoneParent == my_bone.blenName:
1945                                         my_mesh.fbxBoneParent = my_bone
1946         
1947         bone_deformer_count = 0 # count how many bones deform a mesh
1948         my_bone_blenParent = None
1949         for my_bone in ob_bones:
1950                 my_bone_blenParent = my_bone.blenBone.parent
1951                 if my_bone_blenParent:
1952                         for my_bone_parent in ob_bones:
1953                                 # Note 2.45rc2 you can compare bones normally
1954                                 if my_bone_blenParent.name == my_bone_parent.blenName and my_bone.fbxArm == my_bone_parent.fbxArm:
1955                                         my_bone.parent = my_bone_parent
1956                                         break
1957                 
1958                 # Not used at the moment
1959                 # my_bone.calcRestMatrixLocal()
1960                 bone_deformer_count += len(my_bone.blenMeshes)
1961         
1962         del my_bone_blenParent 
1963         
1964         
1965         # Build blenObject -> fbxObject mapping
1966         # this is needed for groups as well as fbxParenting
1967         bpy.data.objects.tag = False
1968         tmp_obmapping = {}
1969         for ob_generic in ob_all_typegroups:
1970                 for ob_base in ob_generic:
1971                         ob_base.blenObject.tag = True
1972                         tmp_obmapping[ob_base.blenObject] = ob_base
1973         
1974         # Build Groups from objects we export
1975         for blenGroup in bpy.data.groups:
1976                 fbxGroupName = None
1977                 for ob in blenGroup.objects:
1978                         if ob.tag:
1979                                 if fbxGroupName == None:
1980                                         fbxGroupName = sane_groupname(blenGroup)
1981                                         groups.append((fbxGroupName, blenGroup))
1982                                 
1983                                 tmp_obmapping[ob].fbxGroupNames.append(fbxGroupName) # also adds to the objects fbxGroupNames
1984         
1985         groups.sort() # not really needed
1986         
1987         # Assign parents using this mapping
1988         for ob_generic in ob_all_typegroups:
1989                 for my_ob in ob_generic:
1990                         parent = my_ob.blenObject.parent
1991                         if parent and parent.tag: # does it exist and is it in the mapping
1992                                 my_ob.fbxParent = tmp_obmapping[parent]
1993         
1994         
1995         del tmp_obmapping
1996         # Finished finding groups we use
1997         
1998         
1999         materials =     [(sane_matname(mat), mat) for mat in materials.itervalues() if mat]
2000         textures =      [(sane_texname(img), img) for img in textures.itervalues()  if img]
2001         materials.sort() # sort by name
2002         textures.sort()
2003         
2004         camera_count = 8
2005         file.write('''
2006
2007 ; Object definitions
2008 ;------------------------------------------------------------------
2009
2010 Definitions:  {
2011         Version: 100
2012         Count: %i''' % (\
2013                 1+1+camera_count+\
2014                 len(ob_meshes)+\
2015                 len(ob_lights)+\
2016                 len(ob_cameras)+\
2017                 len(ob_arms)+\
2018                 len(ob_null)+\
2019                 len(ob_bones)+\
2020                 bone_deformer_count+\
2021                 len(materials)+\
2022                 (len(textures)*2))) # add 1 for the root model 1 for global settings
2023         
2024         del bone_deformer_count
2025         
2026         file.write('''
2027         ObjectType: "Model" {
2028                 Count: %i
2029         }''' % (\
2030                 1+camera_count+\
2031                 len(ob_meshes)+\
2032                 len(ob_lights)+\
2033                 len(ob_cameras)+\
2034                 len(ob_arms)+\
2035                 len(ob_null)+\
2036                 len(ob_bones))) # add 1 for the root model
2037         
2038         file.write('''
2039         ObjectType: "Geometry" {
2040                 Count: %i
2041         }''' % len(ob_meshes))
2042         
2043         if materials:
2044                 file.write('''
2045         ObjectType: "Material" {
2046                 Count: %i
2047         }''' % len(materials))
2048         
2049         if textures:
2050                 file.write('''
2051         ObjectType: "Texture" {
2052                 Count: %i
2053         }''' % len(textures)) # add 1 for an empty tex
2054                 file.write('''
2055         ObjectType: "Video" {
2056                 Count: %i
2057         }''' % len(textures)) # add 1 for an empty tex
2058         
2059         tmp = 0
2060         # Add deformer nodes
2061         for my_mesh in ob_meshes:
2062                 if my_mesh.fbxArm:
2063                         tmp+=1
2064         
2065         # Add subdeformers
2066         for my_bone in ob_bones:
2067                 tmp += len(my_bone.blenMeshes)
2068         
2069         if tmp:
2070                 file.write('''
2071         ObjectType: "Deformer" {
2072                 Count: %i
2073         }''' % tmp)
2074         del tmp
2075         
2076         # we could avoid writing this possibly but for now just write it
2077         
2078         file.write('''
2079         ObjectType: "Pose" {
2080                 Count: 1
2081         }''')
2082         
2083         if groups:
2084                 file.write('''
2085         ObjectType: "GroupSelection" {
2086                 Count: %i
2087         }''' % len(groups))
2088         
2089         file.write('''
2090         ObjectType: "GlobalSettings" {
2091                 Count: 1
2092         }
2093 }''')
2094
2095         file.write('''
2096
2097 ; Object properties
2098 ;------------------------------------------------------------------
2099
2100 Objects:  {''')
2101         
2102         # To comply with other FBX FILES
2103         write_camera_switch()
2104         
2105         # Write the null object
2106         write_null(None, 'blend_root')# , GLOBAL_MATRIX) 
2107         
2108         for my_null in ob_null:
2109                 write_null(my_null)
2110         
2111         for my_arm in ob_arms:
2112                 write_null(my_arm)
2113         
2114         for my_cam in ob_cameras:
2115                 write_camera(my_cam)
2116
2117         for my_light in ob_lights:
2118                 write_light(my_light)
2119         
2120         for my_mesh in ob_meshes:
2121                 write_mesh(my_mesh)
2122
2123         #for bonename, bone, obname, me, armob in ob_bones:
2124         for my_bone in ob_bones:
2125                 write_bone(my_bone)
2126         
2127         write_camera_default()
2128         
2129         for matname, mat in materials:
2130                 write_material(matname, mat)
2131         
2132         # each texture uses a video, odd
2133         for texname, tex in textures:
2134                 write_video(texname, tex)
2135         i = 0
2136         for texname, tex in textures:
2137                 write_texture(texname, tex, i)
2138                 i+=1
2139         
2140         for groupname, group in groups:
2141                 write_group(groupname)
2142         
2143         # NOTE - c4d and motionbuilder dont need normalized weights, but deep-exploration 5 does and (max?) do.
2144         
2145         # Write armature modifiers
2146         # TODO - add another MODEL? - because of this skin definition.
2147         for my_mesh in ob_meshes:
2148                 if my_mesh.fbxArm:
2149                         write_deformer_skin(my_mesh.fbxName)
2150                         
2151                         # Get normalized weights for temorary use
2152                         if my_mesh.fbxBoneParent:
2153                                 weights = None
2154                         else:
2155                                 weights = meshNormalizedWeights(my_mesh.blenData)
2156                         
2157                         #for bonename, bone, obname, bone_mesh, armob in ob_bones:
2158                         for my_bone in ob_bones:
2159                                 if me in my_bone.blenMeshes.itervalues():
2160                                         write_sub_deformer_skin(my_mesh, my_bone, weights)
2161         
2162         # Write pose's really weired, only needed when an armature and mesh are used together
2163         # each by themselves dont need pose data. for now only pose meshes and bones
2164         
2165         file.write('''
2166         Pose: "Pose::BIND_POSES", "BindPose" {
2167                 Type: "BindPose"
2168                 Version: 100
2169                 Properties60:  {
2170                 }
2171                 NbPoseNodes: ''')
2172         file.write(str(len(pose_items)))
2173         
2174
2175         for fbxName, matrix in pose_items:
2176                 file.write('\n\t\tPoseNode:  {')
2177                 file.write('\n\t\t\tNode: "Model::%s"' % fbxName )
2178                 if matrix:              file.write('\n\t\t\tMatrix: %s' % mat4x4str(matrix))
2179                 else:                   file.write('\n\t\t\tMatrix: %s' % mat4x4str(mtx4_identity))
2180                 file.write('\n\t\t}')
2181         
2182         file.write('\n\t}')
2183         
2184         
2185         # Finish Writing Objects
2186         # Write global settings
2187         file.write('''
2188         GlobalSettings:  {
2189                 Version: 1000
2190                 Properties60:  {
2191                         Property: "UpAxis", "int", "",1
2192                         Property: "UpAxisSign", "int", "",1
2193                         Property: "FrontAxis", "int", "",2
2194                         Property: "FrontAxisSign", "int", "",1
2195                         Property: "CoordAxis", "int", "",0
2196                         Property: "CoordAxisSign", "int", "",1
2197                         Property: "UnitScaleFactor", "double", "",100
2198                 }
2199         }
2200 ''')    
2201         file.write('}')
2202         
2203         file.write('''
2204
2205 ; Object relations
2206 ;------------------------------------------------------------------
2207
2208 Relations:  {''')
2209
2210         file.write('\n\tModel: "Model::blend_root", "Null" {\n\t}')
2211
2212         for my_null in ob_null:
2213                 file.write('\n\tModel: "Model::%s", "Null" {\n\t}' % my_null.fbxName)
2214
2215         for my_arm in ob_arms:
2216                 file.write('\n\tModel: "Model::%s", "Null" {\n\t}' % my_arm.fbxName)
2217
2218         for my_mesh in ob_meshes:
2219                 file.write('\n\tModel: "Model::%s", "Mesh" {\n\t}' % my_mesh.fbxName)
2220
2221         # TODO - limbs can have the same name for multiple armatures, should prefix.
2222         #for bonename, bone, obname, me, armob in ob_bones:
2223         for my_bone in ob_bones:
2224                 file.write('\n\tModel: "Model::%s", "Limb" {\n\t}' % my_bone.fbxName)
2225         
2226         for my_cam in ob_cameras:
2227                 file.write('\n\tModel: "Model::%s", "Camera" {\n\t}' % my_cam.fbxName)
2228         
2229         for my_light in ob_lights:
2230                 file.write('\n\tModel: "Model::%s", "Light" {\n\t}' % my_light.fbxName)
2231         
2232         file.write('''
2233         Model: "Model::Producer Perspective", "Camera" {
2234         }
2235         Model: "Model::Producer Top", "Camera" {
2236         }
2237         Model: "Model::Producer Bottom", "Camera" {
2238         }
2239         Model: "Model::Producer Front", "Camera" {
2240         }
2241         Model: "Model::Producer Back", "Camera" {
2242         }
2243         Model: "Model::Producer Right", "Camera" {
2244         }
2245         Model: "Model::Producer Left", "Camera" {
2246         }
2247         Model: "Model::Camera Switcher", "CameraSwitcher" {
2248         }''')
2249         
2250         for matname, mat in materials:
2251                 file.write('\n\tMaterial: "Material::%s", "" {\n\t}' % matname)
2252
2253         if textures:
2254                 for texname, tex in textures:
2255                         file.write('\n\tTexture: "Texture::%s", "TextureVideoClip" {\n\t}' % texname)
2256                 for texname, tex in textures:
2257                         file.write('\n\tVideo: "Video::%s", "Clip" {\n\t}' % texname)
2258
2259         # deformers - modifiers
2260         for my_mesh in ob_meshes:
2261                 if my_mesh.fbxArm:
2262                         file.write('\n\tDeformer: "Deformer::Skin %s", "Skin" {\n\t}' % my_mesh.fbxName)
2263         
2264         #for bonename, bone, obname, me, armob in ob_bones:
2265         for my_bone in ob_bones:
2266                 for fbxMeshObName in my_bone.blenMeshes: # .keys() - fbxMeshObName
2267                         # is this bone effecting a mesh?
2268                         file.write('\n\tDeformer: "SubDeformer::Cluster %s %s", "Cluster" {\n\t}' % (fbxMeshObName, my_bone.fbxName))
2269         
2270         # This should be at the end
2271         # file.write('\n\tPose: "Pose::BIND_POSES", "BindPose" {\n\t}')
2272         
2273         for groupname, group in groups:
2274                 file.write('\n\tGroupSelection: "GroupSelection::%s", "Default" {\n\t}' % groupname)
2275         
2276         file.write('\n}')
2277         file.write('''
2278
2279 ; Object connections
2280 ;------------------------------------------------------------------
2281
2282 Connections:  {''')
2283         
2284         # NOTE - The FBX SDK dosnt care about the order but some importers DO!
2285         # for instance, defining the material->mesh connection
2286         # before the mesh->blend_root crashes cinema4d
2287         
2288
2289         # write the fake root node
2290         file.write('\n\tConnect: "OO", "Model::blend_root", "Model::Scene"')
2291         
2292         for ob_generic in ob_all_typegroups: # all blender 'Object's we support
2293                 for my_ob in ob_generic:
2294                         if my_ob.fbxParent:
2295                                 file.write('\n\tConnect: "OO", "Model::%s", "Model::%s"' % (my_ob.fbxName, my_ob.fbxParent.fbxName))
2296                         else:
2297                                 file.write('\n\tConnect: "OO", "Model::%s", "Model::blend_root"' % my_ob.fbxName)
2298         
2299         if materials:
2300                 for my_mesh in ob_meshes:
2301                         # Connect all materials to all objects, not good form but ok for now.
2302                         for mat in my_mesh.blenMaterials:
2303                                 if mat:
2304                                         file.write('\n\tConnect: "OO", "Material::%s", "Model::%s"' % (sane_name_mapping_mat[mat.name], my_mesh.fbxName))
2305         
2306         if textures:
2307                 for my_mesh in ob_meshes:
2308                         if my_mesh.blenTextures:
2309                                 # file.write('\n\tConnect: "OO", "Texture::_empty_", "Model::%s"' % my_mesh.fbxName)
2310                                 for tex in my_mesh.blenTextures:
2311                                         if tex:
2312                                                 file.write('\n\tConnect: "OO", "Texture::%s", "Model::%s"' % (sane_name_mapping_tex[tex.name], my_mesh.fbxName))
2313                 
2314                 for texname, tex in textures:
2315                         file.write('\n\tConnect: "OO", "Video::%s", "Texture::%s"' % (texname, texname))
2316         
2317         for my_mesh in ob_meshes:
2318                 if my_mesh.fbxArm:
2319                         file.write('\n\tConnect: "OO", "Deformer::Skin %s", "Model::%s"' % (my_mesh.fbxName, my_mesh.fbxName))
2320         
2321         #for bonename, bone, obname, me, armob in ob_bones:
2322         for my_bone in ob_bones:
2323                 for fbxMeshObName in my_bone.blenMeshes: # .keys()
2324                         file.write('\n\tConnect: "OO", "SubDeformer::Cluster %s %s", "Deformer::Skin %s"' % (fbxMeshObName, my_bone.fbxName, fbxMeshObName))
2325         
2326         # limbs -> deformers
2327         # for bonename, bone, obname, me, armob in ob_bones:
2328         for my_bone in ob_bones:
2329                 for fbxMeshObName in my_bone.blenMeshes: # .keys()
2330                         file.write('\n\tConnect: "OO", "Model::%s", "SubDeformer::Cluster %s %s"' % (my_bone.fbxName, fbxMeshObName, my_bone.fbxName))
2331         
2332         
2333         #for bonename, bone, obname, me, armob in ob_bones:
2334         for my_bone in ob_bones:
2335                 # Always parent to armature now
2336                 if my_bone.parent:
2337                         file.write('\n\tConnect: "OO", "Model::%s", "Model::%s"' % (my_bone.fbxName, my_bone.parent.fbxName) )
2338                 else:
2339                         # the armature object is written as an empty and all root level bones connect to it
2340                         file.write('\n\tConnect: "OO", "Model::%s", "Model::%s"' % (my_bone.fbxName, my_bone.fbxArm.fbxName) )
2341         
2342         # groups
2343         if groups:
2344                 for ob_generic in ob_all_typegroups:
2345                         for ob_base in ob_generic:
2346                                 for fbxGroupName in ob_base.fbxGroupNames:
2347                                         file.write('\n\tConnect: "OO", "Model::%s", "GroupSelection::%s"' % (ob_base.fbxName, fbxGroupName))
2348         
2349         for my_arm in ob_arms:
2350                 file.write('\n\tConnect: "OO", "Model::%s", "Model::blend_root"' % my_arm.fbxName)
2351         
2352         file.write('\n}')
2353         
2354         
2355         # Needed for scene footer as well as animation
2356         render = sce.render
2357         
2358         # from the FBX sdk
2359         #define KTIME_ONE_SECOND        KTime (K_LONGLONG(46186158000))
2360         def fbx_time(t):
2361                 # 0.5 + val is the same as rounding.
2362                 return int(0.5 + ((t/fps) * 46186158000))
2363         
2364         fps = float(render.fps) 
2365         start = render.sFrame
2366         end =   render.eFrame
2367         if end < start: start, end = end, start
2368         if start==end: ANIM_ENABLE = False
2369         
2370         # animations for these object types
2371         ob_anim_lists = ob_bones, ob_meshes, ob_null, ob_cameras, ob_lights, ob_arms
2372         
2373         if ANIM_ENABLE and [tmp for tmp in ob_anim_lists if tmp]:
2374                 
2375                 frame_orig = Blender.Get('curframe')
2376                 
2377                 if ANIM_OPTIMIZE:
2378                         ANIM_OPTIMIZE_PRECISSION_FLOAT = 0.1 ** ANIM_OPTIMIZE_PRECISSION
2379                 
2380                 # default action, when no actions are avaioable
2381                 tmp_actions = [None] # None is the default action
2382                 blenActionDefault = None
2383                 action_lastcompat = None
2384                 
2385                 if ANIM_ACTION_ALL:
2386                         bpy.data.actions.tag = False
2387                         tmp_actions = list(bpy.data.actions)
2388                         
2389                         
2390                         # find which actions are compatible with the armatures
2391                         # blenActions is not yet initialized so do it now.
2392                         tmp_act_count = 0
2393                         for my_arm in ob_arms:
2394                                 
2395                                 # get the default name
2396                                 if not blenActionDefault:
2397                                         blenActionDefault = my_arm.blenAction
2398                                 
2399                                 arm_bone_names = set([my_bone.blenName for my_bone in my_arm.fbxBones])
2400                                 
2401                                 for action in tmp_actions:
2402                                         
2403                                         action_chan_names = arm_bone_names.intersection( set(action.getChannelNames()) )
2404                                         
2405                                         if action_chan_names: # at least one channel matches.
2406                                                 my_arm.blenActionList.append(action)
2407                                                 action.tag = True
2408                                                 tmp_act_count += 1
2409                                                 
2410                                                 # incase there is no actions applied to armatures
2411                                                 action_lastcompat = action
2412                         
2413                         if tmp_act_count:
2414                                 # unlikely to ever happen but if no actions applied to armatures, just use the last compatible armature.
2415                                 if not blenActionDefault:
2416                                         blenActionDefault = action_lastcompat
2417                 
2418                 del action_lastcompat
2419                 
2420                 file.write('''
2421 ;Takes and animation section
2422 ;----------------------------------------------------
2423
2424 Takes:  {''')
2425                 
2426                 if blenActionDefault:
2427                         file.write('\n\tCurrent: "%s"' % sane_takename(blenActionDefault))
2428                 else:
2429                         file.write('\n\tCurrent: "Default Take"')
2430                 
2431                 for blenAction in tmp_actions:
2432                         # we have tagged all actious that are used be selected armatures
2433                         if blenAction:
2434                                 if blenAction.tag:
2435                                         print '\taction: "%s" exporting...' % blenAction.name
2436                                 else:
2437                                         print '\taction: "%s" has no armature using it, skipping' % blenAction.name
2438                                         continue
2439                         
2440                         if blenAction == None:
2441                                 # Warning, this only accounts for tmp_actions being [None]
2442                                 file.write('\n\tTake: "Default Take" {')
2443                                 act_start =     start
2444                                 act_end =       end
2445                         else:
2446                                 # use existing name
2447                                 if blenAction == blenActionDefault: # have we alredy got the name
2448                                         file.write('\n\tTake: "%s" {' % sane_name_mapping_take[blenAction.name])
2449                                 else:
2450                                         file.write('\n\tTake: "%s" {' % sane_takename(blenAction))
2451                                         
2452                                 tmp = blenAction.getFrameNumbers()
2453                                 if tmp:
2454                                         act_start =     min(tmp)
2455                                         act_end =       max(tmp)
2456                                         del tmp
2457                                 else:
2458                                         # Fallback on this, theres not much else we can do? :/
2459                                         # when an action has no length
2460                                         act_start =     start
2461                                         act_end =       end
2462                                 
2463                                 # Set the action active
2464                                 for my_bone in ob_arms:
2465                                         if blenAction in my_bone.blenActionList:
2466                                                 ob.action = blenAction
2467                                                 # print '\t\tSetting Action!', blenAction
2468                                 # sce.update(1)
2469                         
2470                         file.write('\n\t\tFileName: "Default_Take.tak"') # ??? - not sure why this is needed
2471                         file.write('\n\t\tLocalTime: %i,%i' % (fbx_time(act_start-1), fbx_time(act_end-1))) # ??? - not sure why this is needed
2472                         file.write('\n\t\tReferenceTime: %i,%i' % (fbx_time(act_start-1), fbx_time(act_end-1))) # ??? - not sure why this is needed
2473                         
2474                         file.write('''
2475
2476                 ;Models animation
2477                 ;----------------------------------------------------''')
2478                         
2479                         
2480                         # set pose data for all bones
2481                         # do this here incase the action changes
2482                         '''
2483                         for my_bone in ob_bones:
2484                                 my_bone.flushAnimData()
2485                         '''
2486                         i = act_start
2487                         while i <= act_end:
2488                                 Blender.Set('curframe', i)
2489                                 for ob_generic in ob_anim_lists:
2490                                         for my_ob in ob_generic:
2491                                                 #Blender.Window.RedrawAll()
2492                                                 if ob_generic == ob_meshes and my_ob.fbxArm:
2493                                                         # We cant animate armature meshes!
2494                                                         pass
2495                                                 else:
2496                                                         my_ob.setPoseFrame(i)
2497                                                 
2498                                 i+=1
2499                         
2500                         
2501                         #for bonename, bone, obname, me, armob in ob_bones:
2502                         for ob_generic in (ob_bones, ob_meshes, ob_null, ob_cameras, ob_lights, ob_arms):
2503                                         
2504                                 for my_ob in ob_generic:
2505                                         
2506                                         if ob_generic == ob_meshes and my_ob.fbxArm:
2507                                                 # do nothing,
2508                                                 pass
2509                                         else:
2510                                                         
2511                                                 file.write('\n\t\tModel: "Model::%s" {' % my_ob.fbxName) # ??? - not sure why this is needed
2512                                                 file.write('\n\t\t\tVersion: 1.1')
2513                                                 file.write('\n\t\t\tChannel: "Transform" {')
2514                                                 
2515                                                 context_bone_anim_mats = [ (my_ob.getAnimParRelMatrix(frame), my_ob.getAnimParRelMatrixRot(frame)) for frame in xrange(act_start, act_end+1) ]
2516                                                 
2517                                                 # ----------------
2518                                                 # ----------------
2519                                                 for TX_LAYER, TX_CHAN in enumerate('TRS'): # transform, rotate, scale
2520                                                         
2521                                                         if              TX_CHAN=='T':   context_bone_anim_vecs = [mtx[0].translationPart()      for mtx in context_bone_anim_mats]
2522                                                         elif    TX_CHAN=='R':   context_bone_anim_vecs = [mtx[1].toEuler()                      for mtx in context_bone_anim_mats]
2523                                                         else:                                   context_bone_anim_vecs = [mtx[0].scalePart()            for mtx in context_bone_anim_mats]
2524                                                         
2525                                                         file.write('\n\t\t\t\tChannel: "%s" {' % TX_CHAN) # translation
2526                                                         
2527                                                         for i in xrange(3):
2528                                                                 # Loop on each axis of the bone
2529                                                                 file.write('\n\t\t\t\t\tChannel: "%s" {'% ('XYZ'[i])) # translation
2530                                                                 file.write('\n\t\t\t\t\t\tDefault: %.15f' % context_bone_anim_vecs[0][i] )
2531                                                                 file.write('\n\t\t\t\t\t\tKeyVer: 4005')
2532                                                                 
2533                                                                 if not ANIM_OPTIMIZE:
2534                                                                         # Just write all frames, simple but in-eficient
2535                                                                         file.write('\n\t\t\t\t\t\tKeyCount: %i' % (1 + act_end - act_start))
2536                                                                         file.write('\n\t\t\t\t\t\tKey: ')
2537                                                                         frame = act_start
2538                                                                         while frame <= act_end:
2539                                                                                 if frame!=act_start:
2540                                                                                         file.write(',')
2541                                                                                 
2542                                                                                 # Curve types are 
2543                                                                                 # C,n is for bezier? - linear is best for now so we can do simple keyframe removal
2544                                                                                 file.write('\n\t\t\t\t\t\t\t%i,%.15f,C,n'  % (fbx_time(frame-1), context_bone_anim_vecs[frame-act_start][i] ))
2545                                                                                 #file.write('\n\t\t\t\t\t\t\t%i,%.15f,L'  % (fbx_time(frame-1), context_bone_anim_vecs[frame-act_start][i] ))
2546                                                                                 frame+=1
2547                                                                 else:
2548                                                                         # remove unneeded keys, j is the frame, needed when some frames are removed.
2549                                                                         context_bone_anim_keys = [ (vec[i], j) for j, vec in enumerate(context_bone_anim_vecs) ]
2550                                                                         
2551                                                                         # last frame to fisrt frame, missing 1 frame on either side.
2552                                                                         # removeing in a backwards loop is faster
2553                                                                         for j in xrange( (act_end-act_start)-1, 0, -1 ):
2554                                                                                 # Is this key reduenant?
2555                                                                                 if      abs(context_bone_anim_keys[j][0] - context_bone_anim_keys[j-1][0]) < ANIM_OPTIMIZE_PRECISSION_FLOAT and\
2556                                                                                         abs(context_bone_anim_keys[j][0] - context_bone_anim_keys[j+1][0]) < ANIM_OPTIMIZE_PRECISSION_FLOAT:
2557                                                                                         del context_bone_anim_keys[j]
2558                                                                         
2559                                                                         if len(context_bone_anim_keys) == 2 and context_bone_anim_keys[0][0] == context_bone_anim_keys[1][0]:
2560                                                                                 # This axis has no moton, its okay to skip KeyCount and Keys in this case
2561                                                                                 pass
2562                                                                         else:
2563                                                                                 # We only need to write these if there is at least one 
2564                                                                                 file.write('\n\t\t\t\t\t\tKeyCount: %i' % len(context_bone_anim_keys))
2565                                                                                 file.write('\n\t\t\t\t\t\tKey: ')
2566                                                                                 for val, frame in context_bone_anim_keys:
2567                                                                                         if frame != context_bone_anim_keys[0][1]: # not the first
2568                                                                                                 file.write(',')
2569                                                                                         # frame is alredy one less then blenders frame
2570                                                                                         file.write('\n\t\t\t\t\t\t\t%i,%.15f,C,n'  % (fbx_time(frame), val ))
2571                                                                                         #file.write('\n\t\t\t\t\t\t\t%i,%.15f,L'  % (fbx_time(frame), val ))
2572                                                                 
2573                                                                 if              i==0:   file.write('\n\t\t\t\t\t\tColor: 1,0,0')
2574                                                                 elif    i==1:   file.write('\n\t\t\t\t\t\tColor: 0,1,0')
2575                                                                 elif    i==2:   file.write('\n\t\t\t\t\t\tColor: 0,0,1')
2576                                                                 
2577                                                                 file.write('\n\t\t\t\t\t}')
2578                                                         file.write('\n\t\t\t\t\tLayerType: %i' % (TX_LAYER+1) )
2579                                                         file.write('\n\t\t\t\t}')
2580                                                 
2581                                                 # --------------- 
2582                                                 
2583                                                 file.write('\n\t\t\t}')
2584                                                 file.write('\n\t\t}')
2585                         
2586                         # end the take
2587                         file.write('\n\t}')
2588                         
2589                         # end action loop. set original actions 
2590                         # do this after every loop incase actions effect eachother.
2591                         for my_bone in ob_arms:
2592                                 my_bone.blenObject.action = my_bone.blenAction
2593                 
2594                 file.write('\n}')
2595                 
2596                 Blender.Set('curframe', frame_orig)
2597                 
2598         else:
2599                 # no animation
2600                 file.write('\n;Takes and animation section')
2601                 file.write('\n;----------------------------------------------------')
2602                 file.write('\n')
2603                 file.write('\nTakes:  {')
2604                 file.write('\n\tCurrent: ""')
2605                 file.write('\n}')
2606         
2607         
2608         # write meshes animation
2609         #for obname, ob, mtx, me, mats, arm, armname in ob_meshes:
2610                 
2611         
2612         # Clear mesh data Only when writing with modifiers applied
2613         for me in meshes_to_clear:
2614                 me.verts = None
2615         
2616         
2617         
2618         # --------------------------- Footer
2619         if world:
2620                 has_mist = world.mode & 1
2621                 mist_intense, mist_start, mist_end, mist_height = world.mist
2622                 world_hor = world.hor
2623         else:
2624                 has_mist = mist_intense = mist_start = mist_end = mist_height = 0
2625                 world_hor = 0,0,0
2626         
2627         file.write('\n;Version 5 settings')
2628         file.write('\n;------------------------------------------------------------------')
2629         file.write('\n')
2630         file.write('\nVersion5:  {')
2631         file.write('\n\tAmbientRenderSettings:  {')
2632         file.write('\n\t\tVersion: 101')
2633         file.write('\n\t\tAmbientLightColor: %.1f,%.1f,%.1f,0' % tuple(world_amb))
2634         file.write('\n\t}')
2635         file.write('\n\tFogOptions:  {')
2636         file.write('\n\t\tFlogEnable: %i' % has_mist)
2637         file.write('\n\t\tFogMode: 0')
2638         file.write('\n\t\tFogDensity: %.3f' % mist_intense)
2639         file.write('\n\t\tFogStart: %.3f' % mist_start)
2640         file.write('\n\t\tFogEnd: %.3f' % mist_end)
2641         file.write('\n\t\tFogColor: %.1f,%.1f,%.1f,1' % tuple(world_hor))
2642         file.write('\n\t}')
2643         file.write('\n\tSettings:  {')
2644         file.write('\n\t\tFrameRate: "%i"' % int(fps))
2645         file.write('\n\t\tTimeFormat: 1')
2646         file.write('\n\t\tSnapOnFrames: 0')
2647         file.write('\n\t\tReferenceTimeIndex: -1')
2648         file.write('\n\t\tTimeLineStartTime: %i' % fbx_time(start-1))
2649         file.write('\n\t\tTimeLineStopTime: %i' % fbx_time(end-1))
2650         file.write('\n\t}')
2651         file.write('\n\tRendererSetting:  {')
2652         file.write('\n\t\tDefaultCamera: "Producer Perspective"')
2653         file.write('\n\t\tDefaultViewingMode: 0')
2654         file.write('\n\t}')
2655         file.write('\n}')
2656         file.write('\n')
2657         
2658         # Incase sombody imports this, clean up by clearing global dicts
2659         sane_name_mapping_ob.clear()
2660         sane_name_mapping_mat.clear()
2661         sane_name_mapping_tex.clear()
2662         
2663         ob_arms[:] =    []
2664         ob_bones[:] =   []
2665         ob_cameras[:] = []
2666         ob_lights[:] =  []
2667         ob_meshes[:] =  []
2668         ob_null[:] =    []
2669         
2670         
2671         # copy images if enabled
2672         if EXP_IMAGE_COPY:
2673                 copy_images( basepath,  [ tex[1] for tex in textures if tex[1] != None ])       
2674         
2675         print 'export finished in %.4f sec.' % (Blender.sys.time() - start_time)
2676         return True
2677         
2678
2679 # --------------------------------------------
2680 # UI Function - not a part of the exporter.
2681 # this is to seperate the user interface from the rest of the exporter.
2682 from Blender import Draw, Window
2683 EVENT_NONE = 0
2684 EVENT_EXIT = 1
2685 EVENT_REDRAW = 2
2686 EVENT_FILESEL = 3
2687
2688 GLOBALS = {}
2689
2690 # export opts
2691
2692 def do_redraw(e,v):             GLOBALS['EVENT'] = e
2693
2694 # toggle between these 2, only allow one on at once
2695 def do_obs_sel(e,v):
2696         GLOBALS['EVENT'] = e
2697         GLOBALS['EXP_OBS_SCENE'].val = 0
2698         GLOBALS['EXP_OBS_SELECTED'].val = 1
2699
2700 def do_obs_sce(e,v):
2701         GLOBALS['EVENT'] = e
2702         GLOBALS['EXP_OBS_SCENE'].val = 1
2703         GLOBALS['EXP_OBS_SELECTED'].val = 0
2704
2705 def do_obs_sce(e,v):
2706         GLOBALS['EVENT'] = e
2707         GLOBALS['EXP_OBS_SCENE'].val = 1
2708         GLOBALS['EXP_OBS_SELECTED'].val = 0
2709
2710 def do_batch_type_grp(e,v):
2711         GLOBALS['EVENT'] = e
2712         GLOBALS['BATCH_GROUP'].val = 1
2713         GLOBALS['BATCH_SCENE'].val = 0
2714
2715 def do_batch_type_sce(e,v):
2716         GLOBALS['EVENT'] = e
2717         GLOBALS['BATCH_GROUP'].val = 0
2718         GLOBALS['BATCH_SCENE'].val = 1
2719
2720 def do_anim_act_all(e,v):
2721         GLOBALS['EVENT'] = e
2722         GLOBALS['ANIM_ACTION_ALL'][0].val = 1
2723         GLOBALS['ANIM_ACTION_ALL'][1].val = 0
2724