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