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