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