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