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