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