Use Python3.5's unpacking generalizations
[blender-addons.git] / io_scene_fbx / export_fbx.py
blobff39ff441576ca2ded590fd66f713aae1f43f65d
1 # ##### BEGIN GPL LICENSE BLOCK #####
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.
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.
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.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8 compliant>
21 # Script copyright (C) Campbell Barton
24 import os
25 import time
26 import math # math.pi
28 import bpy
29 from mathutils import Vector, Matrix
31 def grouper_exact(it, chunk_size):
32 """
33 Grouper-like func, but returns exactly all elements from it:
35 >>> for chunk in grouper_exact(range(10), 3): print(e)
36 (0,1,2)
37 (3,4,5)
38 (6,7,8)
39 (9,)
41 About 2 times slower than simple zip(*[it] * 3), but does not need to convert iterator to sequence to be sure to
42 get exactly all elements in it (i.e. get a last chunk that may be smaller than chunk_size).
43 """
44 import itertools
45 i = itertools.zip_longest(*[iter(it)] * chunk_size, fillvalue=...)
46 curr = next(i)
47 for nxt in i:
48 yield curr
49 curr = nxt
50 if ... in curr:
51 yield curr[:curr.index(...)]
52 else:
53 yield curr
55 # I guess FBX uses degrees instead of radians (Arystan).
56 # Call this function just before writing to FBX.
57 # 180 / math.pi == 57.295779513
58 def tuple_rad_to_deg(eul):
59 return eul[0] * 57.295779513, eul[1] * 57.295779513, eul[2] * 57.295779513
61 # Used to add the scene name into the filepath without using odd chars
62 sane_name_mapping_ob = {}
63 sane_name_mapping_ob_unique = set()
64 sane_name_mapping_mat = {}
65 sane_name_mapping_tex = {}
66 sane_name_mapping_take = {}
67 sane_name_mapping_group = {}
69 # Make sure reserved names are not used
70 sane_name_mapping_ob['Scene'] = 'Scene_'
71 sane_name_mapping_ob_unique.add('Scene_')
74 def increment_string(t):
75 name = t
76 num = ''
77 while name and name[-1].isdigit():
78 num = name[-1] + num
79 name = name[:-1]
80 if num:
81 return '%s%d' % (name, int(num) + 1)
82 else:
83 return name + '_0'
86 # todo - Disallow the name 'Scene' - it will bugger things up.
87 def sane_name(data, dct, unique_set=None):
88 #if not data: return None
90 if type(data) == tuple: # materials are paired up with images
91 data, other = data
92 use_other = True
93 else:
94 other = None
95 use_other = False
97 name = data.name if data else None
98 orig_name = name
100 if other:
101 orig_name_other = other.name
102 name = '%s #%s' % (name, orig_name_other)
103 else:
104 orig_name_other = None
106 # dont cache, only ever call once for each data type now,
107 # so as to avoid namespace collision between types - like with objects <-> bones
108 #try: return dct[name]
109 #except: pass
111 if not name:
112 name = 'unnamed' # blank string, ASKING FOR TROUBLE!
113 else:
115 name = bpy.path.clean_name(name) # use our own
117 name_unique = dct.values() if unique_set is None else unique_set
119 while name in name_unique:
120 name = increment_string(name)
122 if use_other: # even if other is None - orig_name_other will be a string or None
123 dct[orig_name, orig_name_other] = name
124 else:
125 dct[orig_name] = name
127 if unique_set is not None:
128 unique_set.add(name)
130 return name
133 def sane_obname(data):
134 return sane_name(data, sane_name_mapping_ob, sane_name_mapping_ob_unique)
137 def sane_matname(data):
138 return sane_name(data, sane_name_mapping_mat)
141 def sane_texname(data):
142 return sane_name(data, sane_name_mapping_tex)
145 def sane_takename(data):
146 return sane_name(data, sane_name_mapping_take)
149 def sane_groupname(data):
150 return sane_name(data, sane_name_mapping_group)
153 def mat4x4str(mat):
154 # blender matrix is row major, fbx is col major so transpose on write
155 return ("%.15f,%.15f,%.15f,%.15f,"
156 "%.15f,%.15f,%.15f,%.15f,"
157 "%.15f,%.15f,%.15f,%.15f,"
158 "%.15f,%.15f,%.15f,%.15f" %
159 tuple([f for v in mat.transposed() for f in v]))
162 def action_bone_names(obj, action):
163 from bpy.types import PoseBone
165 names = set()
166 path_resolve = obj.path_resolve
168 for fcu in action.fcurves:
169 try:
170 prop = path_resolve(fcu.data_path, False)
171 except:
172 prop = None
174 if prop is not None:
175 data = prop.data
176 if isinstance(data, PoseBone):
177 names.add(data.name)
179 return names
182 # ob must be OB_MESH
183 def BPyMesh_meshWeight2List(ob, me):
184 """ Takes a mesh and return its group names and a list of lists, one list per vertex.
185 aligning the each vert list with the group names, each list contains float value for the weight.
186 These 2 lists can be modified and then used with list2MeshWeight to apply the changes.
189 # Clear the vert group.
190 groupNames = [g.name for g in ob.vertex_groups]
191 len_groupNames = len(groupNames)
193 if not len_groupNames:
194 # no verts? return a vert aligned empty list
195 return [[] for i in range(len(me.vertices))], []
196 else:
197 vWeightList = [[0.0] * len_groupNames for i in range(len(me.vertices))]
199 for i, v in enumerate(me.vertices):
200 for g in v.groups:
201 # possible weights are out of range
202 index = g.group
203 if index < len_groupNames:
204 vWeightList[i][index] = g.weight
206 return groupNames, vWeightList
209 def meshNormalizedWeights(ob, me):
210 groupNames, vWeightList = BPyMesh_meshWeight2List(ob, me)
212 if not groupNames:
213 return [], []
215 for i, vWeights in enumerate(vWeightList):
216 tot = 0.0
217 for w in vWeights:
218 tot += w
220 if tot:
221 for j, w in enumerate(vWeights):
222 vWeights[j] = w / tot
224 return groupNames, vWeightList
226 header_comment = \
227 '''; FBX 6.1.0 project file
228 ; Created by Blender FBX Exporter
229 ; for support mail: ideasman42@gmail.com
230 ; ----------------------------------------------------
235 # This func can be called with just the filepath
236 def save_single(operator, scene, filepath="",
237 global_matrix=None,
238 context_objects=None,
239 object_types={'EMPTY', 'CAMERA', 'LAMP', 'ARMATURE', 'MESH'},
240 use_mesh_modifiers=True,
241 mesh_smooth_type='FACE',
242 use_armature_deform_only=False,
243 use_anim=True,
244 use_anim_optimize=True,
245 anim_optimize_precision=6,
246 use_anim_action_all=False,
247 use_metadata=True,
248 path_mode='AUTO',
249 use_mesh_edges=True,
250 use_default_take=True,
251 **kwargs
254 import bpy_extras.io_utils
256 # Only used for camera and lamp rotations
257 mtx_x90 = Matrix.Rotation(math.pi / 2.0, 3, 'X')
258 # Used for mesh and armature rotations
259 mtx4_z90 = Matrix.Rotation(math.pi / 2.0, 4, 'Z')
261 if global_matrix is None:
262 global_matrix = Matrix()
263 global_scale = 1.0
264 else:
265 global_scale = global_matrix.median_scale
267 # Use this for working out paths relative to the export location
268 base_src = os.path.dirname(bpy.data.filepath)
269 base_dst = os.path.dirname(filepath)
271 # collect images to copy
272 copy_set = set()
274 # ----------------------------------------------
275 # storage classes
276 class my_bone_class(object):
277 __slots__ = ("blenName",
278 "blenBone",
279 "blenMeshes",
280 "restMatrix",
281 "parent",
282 "blenName",
283 "fbxName",
284 "fbxArm",
285 "__pose_bone",
286 "__anim_poselist")
288 def __init__(self, blenBone, fbxArm):
290 # This is so 2 armatures dont have naming conflicts since FBX bones use object namespace
291 self.fbxName = sane_obname(blenBone)
293 self.blenName = blenBone.name
294 self.blenBone = blenBone
295 self.blenMeshes = {} # fbxMeshObName : mesh
296 self.fbxArm = fbxArm
297 self.restMatrix = blenBone.matrix_local
299 # not used yet
300 #~ self.restMatrixInv = self.restMatrix.inverted()
301 #~ self.restMatrixLocal = None # set later, need parent matrix
303 self.parent = None
305 # not public
306 pose = fbxArm.blenObject.pose
307 self.__pose_bone = pose.bones[self.blenName]
309 # store a list if matrices here, (poseMatrix, head, tail)
310 # {frame:posematrix, frame:posematrix, ...}
311 self.__anim_poselist = {}
314 def calcRestMatrixLocal(self):
315 if self.parent:
316 self.restMatrixLocal = self.restMatrix * self.parent.restMatrix.inverted()
317 else:
318 self.restMatrixLocal = self.restMatrix.copy()
320 def setPoseFrame(self, f):
321 # cache pose info here, frame must be set beforehand
323 # Didnt end up needing head or tail, if we do - here it is.
325 self.__anim_poselist[f] = (\
326 self.__pose_bone.poseMatrix.copy(),\
327 self.__pose_bone.head.copy(),\
328 self.__pose_bone.tail.copy() )
331 self.__anim_poselist[f] = self.__pose_bone.matrix.copy()
333 def getPoseBone(self):
334 return self.__pose_bone
336 # get pose from frame.
337 def getPoseMatrix(self, f): # ----------------------------------------------
338 return self.__anim_poselist[f]
340 def getPoseHead(self, f):
341 #return self.__pose_bone.head.copy()
342 return self.__anim_poselist[f][1].copy()
343 def getPoseTail(self, f):
344 #return self.__pose_bone.tail.copy()
345 return self.__anim_poselist[f][2].copy()
347 # end
349 def getAnimParRelMatrix(self, frame):
350 #arm_mat = self.fbxArm.matrixWorld
351 #arm_mat = self.fbxArm.parRelMatrix()
352 if not self.parent:
353 #return mtx4_z90 * (self.getPoseMatrix(frame) * arm_mat) # dont apply arm matrix anymore
354 return self.getPoseMatrix(frame) * mtx4_z90
355 else:
356 #return (mtx4_z90 * ((self.getPoseMatrix(frame) * arm_mat))) * (mtx4_z90 * (self.parent.getPoseMatrix(frame) * arm_mat)).inverted()
357 return (self.parent.getPoseMatrix(frame) * mtx4_z90).inverted() * ((self.getPoseMatrix(frame)) * mtx4_z90)
359 # we need thes because cameras and lights modified rotations
360 def getAnimParRelMatrixRot(self, frame):
361 return self.getAnimParRelMatrix(frame)
363 def flushAnimData(self):
364 self.__anim_poselist.clear()
366 class my_object_generic(object):
367 __slots__ = ("fbxName",
368 "blenObject",
369 "blenData",
370 "origData",
371 "blenTextures",
372 "blenMaterials",
373 "blenMaterialList",
374 "blenAction",
375 "blenActionList",
376 "fbxGroupNames",
377 "fbxParent",
378 "fbxBoneParent",
379 "fbxBones",
380 "fbxArm",
381 "matrixWorld",
382 "__anim_poselist",
385 # Other settings can be applied for each type - mesh, armature etc.
386 def __init__(self, ob, matrixWorld=None):
387 self.fbxName = sane_obname(ob)
388 self.blenObject = ob
389 self.fbxGroupNames = []
390 self.fbxParent = None # set later on IF the parent is in the selection.
391 self.fbxArm = None
392 if matrixWorld:
393 self.matrixWorld = global_matrix * matrixWorld
394 else:
395 self.matrixWorld = global_matrix * ob.matrix_world
397 self.__anim_poselist = {} # we should only access this
399 def parRelMatrix(self):
400 if self.fbxParent:
401 return self.fbxParent.matrixWorld.inverted() * self.matrixWorld
402 else:
403 return self.matrixWorld
405 def setPoseFrame(self, f, fake=False):
406 if fake:
407 self.__anim_poselist[f] = self.matrixWorld * global_matrix.inverted()
408 else:
409 self.__anim_poselist[f] = self.blenObject.matrix_world.copy()
411 def getAnimParRelMatrix(self, frame):
412 if self.fbxParent:
413 #return (self.__anim_poselist[frame] * self.fbxParent.__anim_poselist[frame].inverted() ) * global_matrix
414 return (global_matrix * self.fbxParent.__anim_poselist[frame]).inverted() * (global_matrix * self.__anim_poselist[frame])
415 else:
416 return global_matrix * self.__anim_poselist[frame]
418 def getAnimParRelMatrixRot(self, frame):
419 obj_type = self.blenObject.type
420 if self.fbxParent:
421 matrix_rot = ((global_matrix * self.fbxParent.__anim_poselist[frame]).inverted() * (global_matrix * self.__anim_poselist[frame])).to_3x3()
422 else:
423 matrix_rot = (global_matrix * self.__anim_poselist[frame]).to_3x3()
425 # Lamps need to be rotated
426 if obj_type == 'LAMP':
427 matrix_rot = matrix_rot * mtx_x90
428 elif obj_type == 'CAMERA':
429 y = matrix_rot * Vector((0.0, 1.0, 0.0))
430 matrix_rot = Matrix.Rotation(math.pi / 2.0, 3, y) * matrix_rot
432 return matrix_rot
434 # ----------------------------------------------
436 print('\nFBX export starting... %r' % filepath)
437 start_time = time.process_time()
438 try:
439 file = open(filepath, "w", encoding="utf8", newline="\n")
440 except:
441 import traceback
442 traceback.print_exc()
443 operator.report({'ERROR'}, "Couldn't open file %r" % filepath)
444 return {'CANCELLED'}
446 # convenience
447 fw = file.write
449 # scene = context.scene # now passed as an arg instead of context
450 world = scene.world
452 # ---------------------------- Write the header first
453 fw(header_comment)
454 if use_metadata:
455 curtime = time.localtime()[0:6]
456 else:
457 curtime = (0, 0, 0, 0, 0, 0)
460 '''FBXHeaderExtension: {
461 FBXHeaderVersion: 1003
462 FBXVersion: 6100
463 CreationTimeStamp: {
464 Version: 1000
465 Year: %.4i
466 Month: %.2i
467 Day: %.2i
468 Hour: %.2i
469 Minute: %.2i
470 Second: %.2i
471 Millisecond: 0
473 Creator: "FBX SDK/FBX Plugins build 20070228"
474 OtherFlags: {
475 FlagPLE: 0
477 }''' % (curtime))
479 fw('\nCreationTime: "%.4i-%.2i-%.2i %.2i:%.2i:%.2i:000"' % curtime)
480 fw('\nCreator: "Blender version %s"' % bpy.app.version_string)
482 pose_items = [] # list of (fbxName, matrix) to write pose data for, easier to collect along the way
484 # --------------- funcs for exporting
485 def object_tx(ob, loc, matrix, matrix_mod=None):
487 Matrix mod is so armature objects can modify their bone matrices
489 if isinstance(ob, bpy.types.Bone):
491 # we know we have a matrix
492 # matrix = mtx4_z90 * (ob.matrix['ARMATURESPACE'] * matrix_mod)
493 matrix = ob.matrix_local * mtx4_z90 # dont apply armature matrix anymore
495 parent = ob.parent
496 if parent:
497 #par_matrix = mtx4_z90 * (parent.matrix['ARMATURESPACE'] * matrix_mod)
498 par_matrix = parent.matrix_local * mtx4_z90 # dont apply armature matrix anymore
499 matrix = par_matrix.inverted() * matrix
501 loc, rot, scale = matrix.decompose()
502 matrix_rot = rot.to_matrix()
504 loc = tuple(loc)
505 rot = tuple(rot.to_euler()) # quat -> euler
506 scale = tuple(scale)
508 else:
509 # This is bad because we need the parent relative matrix from the fbx parent (if we have one), dont use anymore
510 #if ob and not matrix: matrix = ob.matrix_world * global_matrix
511 if ob and not matrix:
512 raise Exception("error: this should never happen!")
514 matrix_rot = matrix
515 #if matrix:
516 # matrix = matrix_scale * matrix
518 if matrix:
519 loc, rot, scale = matrix.decompose()
520 matrix_rot = rot.to_matrix()
522 # Lamps need to be rotated
523 if ob and ob.type == 'LAMP':
524 matrix_rot = matrix_rot * mtx_x90
525 elif ob and ob.type == 'CAMERA':
526 y = matrix_rot * Vector((0.0, 1.0, 0.0))
527 matrix_rot = Matrix.Rotation(math.pi / 2.0, 3, y) * matrix_rot
528 # else do nothing.
530 loc = tuple(loc)
531 rot = tuple(matrix_rot.to_euler())
532 scale = tuple(scale)
533 else:
534 if not loc:
535 loc = 0.0, 0.0, 0.0
536 scale = 1.0, 1.0, 1.0
537 rot = 0.0, 0.0, 0.0
539 return loc, rot, scale, matrix, matrix_rot
541 def write_object_tx(ob, loc, matrix, matrix_mod=None):
543 We have loc to set the location if non blender objects that have a location
545 matrix_mod is only used for bones at the moment
547 loc, rot, scale, matrix, matrix_rot = object_tx(ob, loc, matrix, matrix_mod)
549 fw('\n\t\t\tProperty: "Lcl Translation", "Lcl Translation", "A+",%.15f,%.15f,%.15f' % loc)
550 fw('\n\t\t\tProperty: "Lcl Rotation", "Lcl Rotation", "A+",%.15f,%.15f,%.15f' % tuple_rad_to_deg(rot))
551 fw('\n\t\t\tProperty: "Lcl Scaling", "Lcl Scaling", "A+",%.15f,%.15f,%.15f' % scale)
552 return loc, rot, scale, matrix, matrix_rot
554 def get_constraints(ob=None):
555 # Set variables to their defaults.
556 constraint_values = {"loc_min": (0.0, 0.0, 0.0),
557 "loc_max": (0.0, 0.0, 0.0),
558 "loc_limit": (0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
559 "rot_min": (0.0, 0.0, 0.0),
560 "rot_max": (0.0, 0.0, 0.0),
561 "rot_limit": (0.0, 0.0, 0.0),
562 "sca_min": (1.0, 1.0, 1.0),
563 "sca_max": (1.0, 1.0, 1.0),
564 "sca_limit": (0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
567 # Iterate through the list of constraints for this object to get the information in a format which is compatible with the FBX format.
568 if ob is not None:
569 for constraint in ob.constraints:
570 if constraint.type == 'LIMIT_LOCATION':
571 constraint_values["loc_min"] = constraint.min_x, constraint.min_y, constraint.min_z
572 constraint_values["loc_max"] = constraint.max_x, constraint.max_y, constraint.max_z
573 constraint_values["loc_limit"] = constraint.use_min_x, constraint.use_min_y, constraint.use_min_z, constraint.use_max_x, constraint.use_max_y, constraint.use_max_z
574 elif constraint.type == 'LIMIT_ROTATION':
575 constraint_values["rot_min"] = math.degrees(constraint.min_x), math.degrees(constraint.min_y), math.degrees(constraint.min_z)
576 constraint_values["rot_max"] = math.degrees(constraint.max_x), math.degrees(constraint.max_y), math.degrees(constraint.max_z)
577 constraint_values["rot_limit"] = constraint.use_limit_x, constraint.use_limit_y, constraint.use_limit_z
578 elif constraint.type == 'LIMIT_SCALE':
579 constraint_values["sca_min"] = constraint.min_x, constraint.min_y, constraint.min_z
580 constraint_values["sca_max"] = constraint.max_x, constraint.max_y, constraint.max_z
581 constraint_values["sca_limit"] = constraint.use_min_x, constraint.use_min_y, constraint.use_min_z, constraint.use_max_x, constraint.use_max_y, constraint.use_max_z
583 # in case bad values are assigned.
584 assert(len(constraint_values) == 9)
586 return constraint_values
588 def write_object_props(ob=None, loc=None, matrix=None, matrix_mod=None, pose_bone=None):
589 # Check if a pose exists for this object and set the constraint soruce accordingly. (Poses only exsit if the object is a bone.)
590 if pose_bone:
591 constraints = get_constraints(pose_bone)
592 else:
593 constraints = get_constraints(ob)
595 # if the type is 0 its an empty otherwise its a mesh
596 # only difference at the moment is one has a color
597 fw('''
598 Properties60: {
599 Property: "QuaternionInterpolate", "bool", "",0
600 Property: "Visibility", "Visibility", "A+",1''')
602 loc, rot, scale, matrix, matrix_rot = write_object_tx(ob, loc, matrix, matrix_mod)
604 # Rotation order, note, for FBX files Iv loaded normal order is 1
605 # setting to zero.
606 # eEULER_XYZ = 0
607 # eEULER_XZY
608 # eEULER_YZX
609 # eEULER_YXZ
610 # eEULER_ZXY
611 # eEULER_ZYX
613 fw('\n\t\t\tProperty: "RotationOffset", "Vector3D", "",0,0,0'
614 '\n\t\t\tProperty: "RotationPivot", "Vector3D", "",0,0,0'
615 '\n\t\t\tProperty: "ScalingOffset", "Vector3D", "",0,0,0'
616 '\n\t\t\tProperty: "ScalingPivot", "Vector3D", "",0,0,0'
617 '\n\t\t\tProperty: "TranslationActive", "bool", "",0'
620 fw('\n\t\t\tProperty: "TranslationMin", "Vector3D", "",%.15g,%.15g,%.15g' % constraints["loc_min"])
621 fw('\n\t\t\tProperty: "TranslationMax", "Vector3D", "",%.15g,%.15g,%.15g' % constraints["loc_max"])
622 fw('\n\t\t\tProperty: "TranslationMinX", "bool", "",%d' % constraints["loc_limit"][0])
623 fw('\n\t\t\tProperty: "TranslationMinY", "bool", "",%d' % constraints["loc_limit"][1])
624 fw('\n\t\t\tProperty: "TranslationMinZ", "bool", "",%d' % constraints["loc_limit"][2])
625 fw('\n\t\t\tProperty: "TranslationMaxX", "bool", "",%d' % constraints["loc_limit"][3])
626 fw('\n\t\t\tProperty: "TranslationMaxY", "bool", "",%d' % constraints["loc_limit"][4])
627 fw('\n\t\t\tProperty: "TranslationMaxZ", "bool", "",%d' % constraints["loc_limit"][5])
629 fw('\n\t\t\tProperty: "RotationOrder", "enum", "",0'
630 '\n\t\t\tProperty: "RotationSpaceForLimitOnly", "bool", "",0'
631 '\n\t\t\tProperty: "AxisLen", "double", "",10'
632 '\n\t\t\tProperty: "PreRotation", "Vector3D", "",0,0,0'
633 '\n\t\t\tProperty: "PostRotation", "Vector3D", "",0,0,0'
634 '\n\t\t\tProperty: "RotationActive", "bool", "",0'
637 fw('\n\t\t\tProperty: "RotationMin", "Vector3D", "",%.15g,%.15g,%.15g' % constraints["rot_min"])
638 fw('\n\t\t\tProperty: "RotationMax", "Vector3D", "",%.15g,%.15g,%.15g' % constraints["rot_max"])
639 fw('\n\t\t\tProperty: "RotationMinX", "bool", "",%d' % constraints["rot_limit"][0])
640 fw('\n\t\t\tProperty: "RotationMinY", "bool", "",%d' % constraints["rot_limit"][1])
641 fw('\n\t\t\tProperty: "RotationMinZ", "bool", "",%d' % constraints["rot_limit"][2])
642 fw('\n\t\t\tProperty: "RotationMaxX", "bool", "",%d' % constraints["rot_limit"][0])
643 fw('\n\t\t\tProperty: "RotationMaxY", "bool", "",%d' % constraints["rot_limit"][1])
644 fw('\n\t\t\tProperty: "RotationMaxZ", "bool", "",%d' % constraints["rot_limit"][2])
646 fw('\n\t\t\tProperty: "RotationStiffnessX", "double", "",0'
647 '\n\t\t\tProperty: "RotationStiffnessY", "double", "",0'
648 '\n\t\t\tProperty: "RotationStiffnessZ", "double", "",0'
649 '\n\t\t\tProperty: "MinDampRangeX", "double", "",0'
650 '\n\t\t\tProperty: "MinDampRangeY", "double", "",0'
651 '\n\t\t\tProperty: "MinDampRangeZ", "double", "",0'
652 '\n\t\t\tProperty: "MaxDampRangeX", "double", "",0'
653 '\n\t\t\tProperty: "MaxDampRangeY", "double", "",0'
654 '\n\t\t\tProperty: "MaxDampRangeZ", "double", "",0'
655 '\n\t\t\tProperty: "MinDampStrengthX", "double", "",0'
656 '\n\t\t\tProperty: "MinDampStrengthY", "double", "",0'
657 '\n\t\t\tProperty: "MinDampStrengthZ", "double", "",0'
658 '\n\t\t\tProperty: "MaxDampStrengthX", "double", "",0'
659 '\n\t\t\tProperty: "MaxDampStrengthY", "double", "",0'
660 '\n\t\t\tProperty: "MaxDampStrengthZ", "double", "",0'
661 '\n\t\t\tProperty: "PreferedAngleX", "double", "",0'
662 '\n\t\t\tProperty: "PreferedAngleY", "double", "",0'
663 '\n\t\t\tProperty: "PreferedAngleZ", "double", "",0'
664 '\n\t\t\tProperty: "InheritType", "enum", "",0'
665 '\n\t\t\tProperty: "ScalingActive", "bool", "",0'
668 fw('\n\t\t\tProperty: "ScalingMin", "Vector3D", "",%.15g,%.15g,%.15g' % constraints["sca_min"])
669 fw('\n\t\t\tProperty: "ScalingMax", "Vector3D", "",%.15g,%.15g,%.15g' % constraints["sca_max"])
670 fw('\n\t\t\tProperty: "ScalingMinX", "bool", "",%d' % constraints["sca_limit"][0])
671 fw('\n\t\t\tProperty: "ScalingMinY", "bool", "",%d' % constraints["sca_limit"][1])
672 fw('\n\t\t\tProperty: "ScalingMinZ", "bool", "",%d' % constraints["sca_limit"][2])
673 fw('\n\t\t\tProperty: "ScalingMaxX", "bool", "",%d' % constraints["sca_limit"][3])
674 fw('\n\t\t\tProperty: "ScalingMaxY", "bool", "",%d' % constraints["sca_limit"][4])
675 fw('\n\t\t\tProperty: "ScalingMaxZ", "bool", "",%d' % constraints["sca_limit"][5])
677 fw('\n\t\t\tProperty: "GeometricTranslation", "Vector3D", "",0,0,0'
678 '\n\t\t\tProperty: "GeometricRotation", "Vector3D", "",0,0,0'
679 '\n\t\t\tProperty: "GeometricScaling", "Vector3D", "",1,1,1'
680 '\n\t\t\tProperty: "LookAtProperty", "object", ""'
681 '\n\t\t\tProperty: "UpVectorProperty", "object", ""'
682 '\n\t\t\tProperty: "Show", "bool", "",1'
683 '\n\t\t\tProperty: "NegativePercentShapeSupport", "bool", "",1'
684 '\n\t\t\tProperty: "DefaultAttributeIndex", "int", "",0'
687 if ob and not isinstance(ob, bpy.types.Bone):
688 # Only mesh objects have color
689 fw('\n\t\t\tProperty: "Color", "Color", "A",0.8,0.8,0.8'
690 '\n\t\t\tProperty: "Size", "double", "",100'
691 '\n\t\t\tProperty: "Look", "enum", "",1'
694 return loc, rot, scale, matrix, matrix_rot
696 # -------------------------------------------- Armatures
697 #def write_bone(bone, name, matrix_mod):
698 def write_bone(my_bone):
699 fw('\n\tModel: "Model::%s", "Limb" {' % my_bone.fbxName)
700 fw('\n\t\tVersion: 232')
702 #~ poseMatrix = write_object_props(my_bone.blenBone, None, None, my_bone.fbxArm.parRelMatrix())[3]
703 poseMatrix = write_object_props(my_bone.blenBone, pose_bone=my_bone.getPoseBone())[3] # dont apply bone matrices anymore
705 # Use the same calculation as in write_sub_deformer_skin to compute the global
706 # transform of the bone for the bind pose.
707 global_matrix_bone = (my_bone.fbxArm.matrixWorld * my_bone.restMatrix) * mtx4_z90
708 pose_items.append((my_bone.fbxName, global_matrix_bone))
710 # fw('\n\t\t\tProperty: "Size", "double", "",%.6f' % ((my_bone.blenData.head['ARMATURESPACE'] - my_bone.blenData.tail['ARMATURESPACE']) * my_bone.fbxArm.parRelMatrix()).length)
711 fw('\n\t\t\tProperty: "Size", "double", "",1')
713 #((my_bone.blenData.head['ARMATURESPACE'] * my_bone.fbxArm.matrixWorld) - (my_bone.blenData.tail['ARMATURESPACE'] * my_bone.fbxArm.parRelMatrix())).length)
716 fw('\n\t\t\tProperty: "LimbLength", "double", "",%.6f' %\
717 ((my_bone.blenBone.head['ARMATURESPACE'] - my_bone.blenBone.tail['ARMATURESPACE']) * my_bone.fbxArm.parRelMatrix()).length)
720 fw('\n\t\t\tProperty: "LimbLength", "double", "",%.6f' %
721 (my_bone.blenBone.head_local - my_bone.blenBone.tail_local).length)
723 #fw('\n\t\t\tProperty: "LimbLength", "double", "",1')
724 fw('\n\t\t\tProperty: "Color", "ColorRGB", "",0.8,0.8,0.8'
725 '\n\t\t\tProperty: "Color", "Color", "A",0.8,0.8,0.8'
726 '\n\t\t}'
727 '\n\t\tMultiLayer: 0'
728 '\n\t\tMultiTake: 1'
729 '\n\t\tShading: Y'
730 '\n\t\tCulling: "CullingOff"'
731 '\n\t\tTypeFlags: "Skeleton"'
732 '\n\t}'
735 def write_camera_switch():
736 fw('''
737 Model: "Model::Camera Switcher", "CameraSwitcher" {
738 Version: 232''')
740 write_object_props()
741 fw('''
742 Property: "Color", "Color", "A",0.8,0.8,0.8
743 Property: "Camera Index", "Integer", "A+",100
745 MultiLayer: 0
746 MultiTake: 1
747 Hidden: "True"
748 Shading: W
749 Culling: "CullingOff"
750 Version: 101
751 Name: "Model::Camera Switcher"
752 CameraId: 0
753 CameraName: 100
754 CameraIndexName:
755 }''')
757 def write_camera_dummy(name, loc, near, far, proj_type, up):
758 fw('\n\tModel: "Model::%s", "Camera" {' % name)
759 fw('\n\t\tVersion: 232')
760 write_object_props(None, loc)
762 fw('\n\t\t\tProperty: "Color", "Color", "A",0.8,0.8,0.8'
763 '\n\t\t\tProperty: "Roll", "Roll", "A+",0'
764 '\n\t\t\tProperty: "FieldOfView", "FieldOfView", "A+",40'
765 '\n\t\t\tProperty: "FieldOfViewX", "FieldOfView", "A+",1'
766 '\n\t\t\tProperty: "FieldOfViewY", "FieldOfView", "A+",1'
767 '\n\t\t\tProperty: "OpticalCenterX", "Real", "A+",0'
768 '\n\t\t\tProperty: "OpticalCenterY", "Real", "A+",0'
769 '\n\t\t\tProperty: "BackgroundColor", "Color", "A+",0.63,0.63,0.63'
770 '\n\t\t\tProperty: "TurnTable", "Real", "A+",0'
771 '\n\t\t\tProperty: "DisplayTurnTableIcon", "bool", "",1'
772 '\n\t\t\tProperty: "Motion Blur Intensity", "Real", "A+",1'
773 '\n\t\t\tProperty: "UseMotionBlur", "bool", "",0'
774 '\n\t\t\tProperty: "UseRealTimeMotionBlur", "bool", "",1'
775 '\n\t\t\tProperty: "ResolutionMode", "enum", "",0'
776 '\n\t\t\tProperty: "ApertureMode", "enum", "",2'
777 '\n\t\t\tProperty: "GateFit", "enum", "",0'
778 '\n\t\t\tProperty: "FocalLength", "Real", "A+",21.3544940948486'
779 '\n\t\t\tProperty: "CameraFormat", "enum", "",0'
780 '\n\t\t\tProperty: "AspectW", "double", "",320'
781 '\n\t\t\tProperty: "AspectH", "double", "",200'
782 '\n\t\t\tProperty: "PixelAspectRatio", "double", "",1'
783 '\n\t\t\tProperty: "UseFrameColor", "bool", "",0'
784 '\n\t\t\tProperty: "FrameColor", "ColorRGB", "",0.3,0.3,0.3'
785 '\n\t\t\tProperty: "ShowName", "bool", "",1'
786 '\n\t\t\tProperty: "ShowGrid", "bool", "",1'
787 '\n\t\t\tProperty: "ShowOpticalCenter", "bool", "",0'
788 '\n\t\t\tProperty: "ShowAzimut", "bool", "",1'
789 '\n\t\t\tProperty: "ShowTimeCode", "bool", "",0'
792 fw('\n\t\t\tProperty: "NearPlane", "double", "",%.6f' % near)
793 fw('\n\t\t\tProperty: "FarPlane", "double", "",%.6f' % far)
795 fw('\n\t\t\tProperty: "FilmWidth", "double", "",0.816'
796 '\n\t\t\tProperty: "FilmHeight", "double", "",0.612'
797 '\n\t\t\tProperty: "FilmAspectRatio", "double", "",1.33333333333333'
798 '\n\t\t\tProperty: "FilmSqueezeRatio", "double", "",1'
799 '\n\t\t\tProperty: "FilmFormatIndex", "enum", "",4'
800 '\n\t\t\tProperty: "ViewFrustum", "bool", "",1'
801 '\n\t\t\tProperty: "ViewFrustumNearFarPlane", "bool", "",0'
802 '\n\t\t\tProperty: "ViewFrustumBackPlaneMode", "enum", "",2'
803 '\n\t\t\tProperty: "BackPlaneDistance", "double", "",100'
804 '\n\t\t\tProperty: "BackPlaneDistanceMode", "enum", "",0'
805 '\n\t\t\tProperty: "ViewCameraToLookAt", "bool", "",1'
806 '\n\t\t\tProperty: "LockMode", "bool", "",0'
807 '\n\t\t\tProperty: "LockInterestNavigation", "bool", "",0'
808 '\n\t\t\tProperty: "FitImage", "bool", "",0'
809 '\n\t\t\tProperty: "Crop", "bool", "",0'
810 '\n\t\t\tProperty: "Center", "bool", "",1'
811 '\n\t\t\tProperty: "KeepRatio", "bool", "",1'
812 '\n\t\t\tProperty: "BackgroundMode", "enum", "",0'
813 '\n\t\t\tProperty: "BackgroundAlphaTreshold", "double", "",0.5'
814 '\n\t\t\tProperty: "ForegroundTransparent", "bool", "",1'
815 '\n\t\t\tProperty: "DisplaySafeArea", "bool", "",0'
816 '\n\t\t\tProperty: "SafeAreaDisplayStyle", "enum", "",1'
817 '\n\t\t\tProperty: "SafeAreaAspectRatio", "double", "",1.33333333333333'
818 '\n\t\t\tProperty: "Use2DMagnifierZoom", "bool", "",0'
819 '\n\t\t\tProperty: "2D Magnifier Zoom", "Real", "A+",100'
820 '\n\t\t\tProperty: "2D Magnifier X", "Real", "A+",50'
821 '\n\t\t\tProperty: "2D Magnifier Y", "Real", "A+",50'
824 fw('\n\t\t\tProperty: "CameraProjectionType", "enum", "",%i' % proj_type)
826 fw('\n\t\t\tProperty: "UseRealTimeDOFAndAA", "bool", "",0'
827 '\n\t\t\tProperty: "UseDepthOfField", "bool", "",0'
828 '\n\t\t\tProperty: "FocusSource", "enum", "",0'
829 '\n\t\t\tProperty: "FocusAngle", "double", "",3.5'
830 '\n\t\t\tProperty: "FocusDistance", "double", "",200'
831 '\n\t\t\tProperty: "UseAntialiasing", "bool", "",0'
832 '\n\t\t\tProperty: "AntialiasingIntensity", "double", "",0.77777'
833 '\n\t\t\tProperty: "UseAccumulationBuffer", "bool", "",0'
834 '\n\t\t\tProperty: "FrameSamplingCount", "int", "",7'
835 '\n\t\t}'
836 '\n\t\tMultiLayer: 0'
837 '\n\t\tMultiTake: 0'
838 '\n\t\tHidden: "True"'
839 '\n\t\tShading: Y'
840 '\n\t\tCulling: "CullingOff"'
841 '\n\t\tTypeFlags: "Camera"'
842 '\n\t\tGeometryVersion: 124'
845 fw('\n\t\tPosition: %.6f,%.6f,%.6f' % loc)
846 fw('\n\t\tUp: %i,%i,%i' % up)
848 fw('\n\t\tLookAt: 0,0,0'
849 '\n\t\tShowInfoOnMoving: 1'
850 '\n\t\tShowAudio: 0'
851 '\n\t\tAudioColor: 0,1,0'
852 '\n\t\tCameraOrthoZoom: 1'
853 '\n\t}'
856 def write_camera_default():
857 # This sucks but to match FBX converter its easier to
858 # write the cameras though they are not needed.
859 write_camera_dummy('Producer Perspective', (0, 71.3, 287.5), 10, 4000, 0, (0, 1, 0))
860 write_camera_dummy('Producer Top', (0, 4000, 0), 1, 30000, 1, (0, 0, -1))
861 write_camera_dummy('Producer Bottom', (0, -4000, 0), 1, 30000, 1, (0, 0, -1))
862 write_camera_dummy('Producer Front', (0, 0, 4000), 1, 30000, 1, (0, 1, 0))
863 write_camera_dummy('Producer Back', (0, 0, -4000), 1, 30000, 1, (0, 1, 0))
864 write_camera_dummy('Producer Right', (4000, 0, 0), 1, 30000, 1, (0, 1, 0))
865 write_camera_dummy('Producer Left', (-4000, 0, 0), 1, 30000, 1, (0, 1, 0))
867 def write_camera(my_cam):
869 Write a blender camera
871 render = scene.render
872 width = render.resolution_x
873 height = render.resolution_y
874 aspect = width / height
876 data = my_cam.blenObject.data
877 # film width & height from mm to inches
878 filmwidth = data.sensor_width * 0.0393700787
879 filmheight = data.sensor_height * 0.0393700787
880 filmaspect = filmwidth / filmheight
881 # film offset
882 offsetx = filmwidth * data.shift_x
883 offsety = filmaspect * filmheight * data.shift_y
885 fw('\n\tModel: "Model::%s", "Camera" {' % my_cam.fbxName)
886 fw('\n\t\tVersion: 232')
887 loc, rot, scale, matrix, matrix_rot = write_object_props(my_cam.blenObject, None, my_cam.parRelMatrix())
889 fw('\n\t\t\tProperty: "Roll", "Roll", "A+",0')
890 fw('\n\t\t\tProperty: "FieldOfView", "FieldOfView", "A+",%.6f' % math.degrees(data.angle_x))
891 fw('\n\t\t\tProperty: "FieldOfViewX", "FieldOfView", "A+",%.6f' % math.degrees(data.angle_x))
892 fw('\n\t\t\tProperty: "FieldOfViewY", "FieldOfView", "A+",%.6f' % math.degrees(data.angle_y))
894 fw('\n\t\t\tProperty: "FocalLength", "Number", "A+",%.6f' % data.lens)
895 fw('\n\t\t\tProperty: "FilmOffsetX", "Number", "A+",%.6f' % offsetx)
896 fw('\n\t\t\tProperty: "FilmOffsetY", "Number", "A+",%.6f' % offsety)
898 fw('\n\t\t\tProperty: "BackgroundColor", "Color", "A+",0,0,0'
899 '\n\t\t\tProperty: "TurnTable", "Real", "A+",0'
900 '\n\t\t\tProperty: "DisplayTurnTableIcon", "bool", "",1'
901 '\n\t\t\tProperty: "Motion Blur Intensity", "Real", "A+",1'
902 '\n\t\t\tProperty: "UseMotionBlur", "bool", "",0'
903 '\n\t\t\tProperty: "UseRealTimeMotionBlur", "bool", "",1'
904 '\n\t\t\tProperty: "ResolutionMode", "enum", "",0'
905 # note that aperture mode 3 is focal length and not horizontal
906 '\n\t\t\tProperty: "ApertureMode", "enum", "",3' # horizontal - Houdini compatible
907 '\n\t\t\tProperty: "GateFit", "enum", "",2'
908 '\n\t\t\tProperty: "CameraFormat", "enum", "",0'
911 fw('\n\t\t\tProperty: "AspectW", "double", "",%i' % width)
912 fw('\n\t\t\tProperty: "AspectH", "double", "",%i' % height)
914 """Camera aspect ratio modes.
915 0 If the ratio mode is eWINDOW_SIZE, both width and height values aren't relevant.
916 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.
917 2 If the ratio mode is eFIXED_RESOLUTION, both width and height values are in pixels.
918 3 If the ratio mode is eFIXED_WIDTH, the width value is in pixels and the height value is relative to the width value.
919 4 If the ratio mode is eFIXED_HEIGHT, the height value is in pixels and the width value is relative to the height value.
921 Definition at line 234 of file kfbxcamera.h. """
923 fw('\n\t\t\tProperty: "PixelAspectRatio", "double", "",1'
924 '\n\t\t\tProperty: "UseFrameColor", "bool", "",0'
925 '\n\t\t\tProperty: "FrameColor", "ColorRGB", "",0.3,0.3,0.3'
926 '\n\t\t\tProperty: "ShowName", "bool", "",1'
927 '\n\t\t\tProperty: "ShowGrid", "bool", "",1'
928 '\n\t\t\tProperty: "ShowOpticalCenter", "bool", "",0'
929 '\n\t\t\tProperty: "ShowAzimut", "bool", "",1'
930 '\n\t\t\tProperty: "ShowTimeCode", "bool", "",0'
933 fw('\n\t\t\tProperty: "NearPlane", "double", "",%.6f' % (data.clip_start * global_scale))
934 fw('\n\t\t\tProperty: "FarPlane", "double", "",%.6f' % (data.clip_end * global_scale))
936 fw('\n\t\t\tProperty: "FilmWidth", "double", "",%.6f' % filmwidth)
937 fw('\n\t\t\tProperty: "FilmHeight", "double", "",%.6f' % filmheight)
938 fw('\n\t\t\tProperty: "FilmAspectRatio", "double", "",%.6f' % filmaspect)
940 fw('\n\t\t\tProperty: "FilmSqueezeRatio", "double", "",1'
941 '\n\t\t\tProperty: "FilmFormatIndex", "enum", "",0'
942 '\n\t\t\tProperty: "ViewFrustum", "bool", "",1'
943 '\n\t\t\tProperty: "ViewFrustumNearFarPlane", "bool", "",0'
944 '\n\t\t\tProperty: "ViewFrustumBackPlaneMode", "enum", "",2'
945 '\n\t\t\tProperty: "BackPlaneDistance", "double", "",100'
946 '\n\t\t\tProperty: "BackPlaneDistanceMode", "enum", "",0'
947 '\n\t\t\tProperty: "ViewCameraToLookAt", "bool", "",1'
948 '\n\t\t\tProperty: "LockMode", "bool", "",0'
949 '\n\t\t\tProperty: "LockInterestNavigation", "bool", "",0'
950 '\n\t\t\tProperty: "FitImage", "bool", "",0'
951 '\n\t\t\tProperty: "Crop", "bool", "",0'
952 '\n\t\t\tProperty: "Center", "bool", "",1'
953 '\n\t\t\tProperty: "KeepRatio", "bool", "",1'
954 '\n\t\t\tProperty: "BackgroundMode", "enum", "",0'
955 '\n\t\t\tProperty: "BackgroundAlphaTreshold", "double", "",0.5'
956 '\n\t\t\tProperty: "ForegroundTransparent", "bool", "",1'
957 '\n\t\t\tProperty: "DisplaySafeArea", "bool", "",0'
958 '\n\t\t\tProperty: "SafeAreaDisplayStyle", "enum", "",1'
961 fw('\n\t\t\tProperty: "SafeAreaAspectRatio", "double", "",%.6f' % aspect)
963 fw('\n\t\t\tProperty: "Use2DMagnifierZoom", "bool", "",0'
964 '\n\t\t\tProperty: "2D Magnifier Zoom", "Real", "A+",100'
965 '\n\t\t\tProperty: "2D Magnifier X", "Real", "A+",50'
966 '\n\t\t\tProperty: "2D Magnifier Y", "Real", "A+",50'
967 '\n\t\t\tProperty: "CameraProjectionType", "enum", "",0'
968 '\n\t\t\tProperty: "UseRealTimeDOFAndAA", "bool", "",0'
969 '\n\t\t\tProperty: "UseDepthOfField", "bool", "",0'
970 '\n\t\t\tProperty: "FocusSource", "enum", "",0'
971 '\n\t\t\tProperty: "FocusAngle", "double", "",3.5'
972 '\n\t\t\tProperty: "FocusDistance", "double", "",200'
973 '\n\t\t\tProperty: "UseAntialiasing", "bool", "",0'
974 '\n\t\t\tProperty: "AntialiasingIntensity", "double", "",0.77777'
975 '\n\t\t\tProperty: "UseAccumulationBuffer", "bool", "",0'
976 '\n\t\t\tProperty: "FrameSamplingCount", "int", "",7'
979 fw('\n\t\t}')
981 fw('\n\t\tMultiLayer: 0'
982 '\n\t\tMultiTake: 0'
983 '\n\t\tShading: Y'
984 '\n\t\tCulling: "CullingOff"'
985 '\n\t\tTypeFlags: "Camera"'
986 '\n\t\tGeometryVersion: 124'
989 fw('\n\t\tPosition: %.6f,%.6f,%.6f' % loc)
990 fw('\n\t\tUp: %.6f,%.6f,%.6f' % (matrix_rot * Vector((0.0, 1.0, 0.0)))[:])
991 fw('\n\t\tLookAt: %.6f,%.6f,%.6f' % (matrix_rot * Vector((0.0, 0.0, -1.0)))[:])
993 #fw('\n\t\tUp: 0,0,0' )
994 #fw('\n\t\tLookAt: 0,0,0' )
996 fw('\n\t\tShowInfoOnMoving: 1')
997 fw('\n\t\tShowAudio: 0')
998 fw('\n\t\tAudioColor: 0,1,0')
999 fw('\n\t\tCameraOrthoZoom: 1')
1000 fw('\n\t}')
1002 def write_light(my_light):
1003 light = my_light.blenObject.data
1004 fw('\n\tModel: "Model::%s", "Light" {' % my_light.fbxName)
1005 fw('\n\t\tVersion: 232')
1007 write_object_props(my_light.blenObject, None, my_light.parRelMatrix())
1009 # Why are these values here twice?????? - oh well, follow the holy sdk's output
1011 # Blender light types match FBX's, funny coincidence, we just need to
1012 # be sure that all unsupported types are made into a point light
1013 #ePOINT,
1014 #eDIRECTIONAL
1015 #eSPOT
1016 light_type_items = {'POINT': 0, 'SUN': 1, 'SPOT': 2, 'HEMI': 3, 'AREA': 4}
1017 light_type = light_type_items[light.type]
1019 if light_type > 2:
1020 light_type = 1 # hemi and area lights become directional
1022 if light.type == 'HEMI':
1023 do_light = not (light.use_diffuse or light.use_specular)
1024 do_shadow = False
1025 else:
1026 do_light = not (light.use_only_shadow or (not light.use_diffuse and not light.use_specular))
1027 do_shadow = (light.shadow_method in {'RAY_SHADOW', 'BUFFER_SHADOW'})
1029 # scale = abs(global_matrix.to_scale()[0]) # scale is always uniform in this case # UNUSED
1031 fw('\n\t\t\tProperty: "LightType", "enum", "",%i' % light_type)
1032 fw('\n\t\t\tProperty: "CastLightOnObject", "bool", "",1')
1033 fw('\n\t\t\tProperty: "DrawVolumetricLight", "bool", "",1')
1034 fw('\n\t\t\tProperty: "DrawGroundProjection", "bool", "",1')
1035 fw('\n\t\t\tProperty: "DrawFrontFacingVolumetricLight", "bool", "",0')
1036 fw('\n\t\t\tProperty: "GoboProperty", "object", ""')
1037 fw('\n\t\t\tProperty: "Color", "Color", "A+",1,1,1')
1038 if light.type == 'SPOT':
1039 fw('\n\t\t\tProperty: "OuterAngle", "Number", "A+",%.2f' %
1040 math.degrees(light.spot_size))
1041 fw('\n\t\t\tProperty: "InnerAngle", "Number", "A+",%.2f' %
1042 (math.degrees(light.spot_size) - math.degrees(light.spot_size) * light.spot_blend))
1044 fw('\n\t\t\tProperty: "Fog", "Fog", "A+",50')
1045 fw('\n\t\t\tProperty: "Color", "Color", "A",%.2f,%.2f,%.2f' % tuple(light.color))
1046 fw('\n\t\t\tProperty: "Intensity", "Intensity", "A+",%.2f' % (light.energy * 100.0))
1048 fw('\n\t\t\tProperty: "Fog", "Fog", "A+",50')
1049 fw('\n\t\t\tProperty: "LightType", "enum", "",%i' % light_type)
1050 fw('\n\t\t\tProperty: "CastLightOnObject", "bool", "",%i' % do_light)
1051 fw('\n\t\t\tProperty: "DrawGroundProjection", "bool", "",1')
1052 fw('\n\t\t\tProperty: "DrawFrontFacingVolumetricLight", "bool", "",0')
1053 fw('\n\t\t\tProperty: "DrawVolumetricLight", "bool", "",1')
1054 fw('\n\t\t\tProperty: "GoboProperty", "object", ""')
1055 if light.type in {'SPOT', 'POINT'}:
1056 if light.falloff_type == 'CONSTANT':
1057 fw('\n\t\t\tProperty: "DecayType", "enum", "",0')
1058 if light.falloff_type == 'INVERSE_LINEAR':
1059 fw('\n\t\t\tProperty: "DecayType", "enum", "",1')
1060 fw('\n\t\t\tProperty: "EnableFarAttenuation", "bool", "",1')
1061 fw('\n\t\t\tProperty: "FarAttenuationEnd", "double", "",%.2f' % (light.distance * 2.0))
1062 if light.falloff_type == 'INVERSE_SQUARE':
1063 fw('\n\t\t\tProperty: "DecayType", "enum", "",2')
1064 fw('\n\t\t\tProperty: "EnableFarAttenuation", "bool", "",1')
1065 fw('\n\t\t\tProperty: "FarAttenuationEnd", "double", "",%.2f' % (light.distance * 2.0))
1067 fw('\n\t\t\tProperty: "CastShadows", "bool", "",%i' % do_shadow)
1068 fw('\n\t\t\tProperty: "ShadowColor", "ColorRGBA", "",0,0,0,1')
1069 fw('\n\t\t}')
1071 fw('\n\t\tMultiLayer: 0'
1072 '\n\t\tMultiTake: 0'
1073 '\n\t\tShading: Y'
1074 '\n\t\tCulling: "CullingOff"'
1075 '\n\t\tTypeFlags: "Light"'
1076 '\n\t\tGeometryVersion: 124'
1077 '\n\t}'
1080 # matrixOnly is not used at the moment
1081 def write_null(my_null=None, fbxName=None, fbxType="Null", fbxTypeFlags="Null"):
1082 # ob can be null
1083 if not fbxName:
1084 fbxName = my_null.fbxName
1086 fw('\n\tModel: "Model::%s", "%s" {' % (fbxName, fbxType))
1087 fw('\n\t\tVersion: 232')
1089 if my_null:
1090 poseMatrix = write_object_props(my_null.blenObject, None, my_null.parRelMatrix())[3]
1091 else:
1092 poseMatrix = write_object_props()[3]
1094 pose_items.append((fbxName, poseMatrix))
1096 fw('\n\t\t}'
1097 '\n\t\tMultiLayer: 0'
1098 '\n\t\tMultiTake: 1'
1099 '\n\t\tShading: Y'
1100 '\n\t\tCulling: "CullingOff"'
1103 fw('\n\t\tTypeFlags: "%s"' % fbxTypeFlags)
1104 fw('\n\t}')
1106 # Material Settings
1107 if world:
1108 world_amb = world.ambient_color[:]
1109 else:
1110 world_amb = 0.0, 0.0, 0.0 # default value
1112 def write_material(matname, mat):
1113 fw('\n\tMaterial: "Material::%s", "" {' % matname)
1115 # Todo, add more material Properties.
1116 if mat:
1117 mat_cold = tuple(mat.diffuse_color)
1118 mat_cols = tuple(mat.specular_color)
1119 #mat_colm = tuple(mat.mirCol) # we wont use the mirror color
1120 mat_colamb = 1.0, 1.0, 1.0
1122 mat_dif = mat.diffuse_intensity
1123 mat_amb = mat.ambient
1124 mat_hard = ((float(mat.specular_hardness) - 1.0) / 510.0) * 128.0
1125 mat_spec = mat.specular_intensity
1126 mat_alpha = mat.alpha
1127 mat_emit = mat.emit
1128 mat_shadeless = mat.use_shadeless
1129 if mat_shadeless:
1130 mat_shader = 'Lambert'
1131 else:
1132 if mat.diffuse_shader == 'LAMBERT':
1133 mat_shader = 'Lambert'
1134 else:
1135 mat_shader = 'Phong'
1136 else:
1137 mat_cold = 0.8, 0.8, 0.8
1138 mat_cols = 1.0, 1.0, 1.0
1139 mat_colamb = 1.0, 1.0, 1.0
1140 # mat_colm
1141 mat_dif = 0.8
1142 mat_amb = 1.0
1143 mat_hard = 12.3
1144 mat_spec = 0.5
1145 mat_alpha = 1.0
1146 mat_emit = 0.0
1147 mat_shadeless = False
1148 mat_shader = 'Phong'
1150 fw('\n\t\tVersion: 102')
1151 fw('\n\t\tShadingModel: "%s"' % mat_shader.lower())
1152 fw('\n\t\tMultiLayer: 0')
1154 fw('\n\t\tProperties60: {')
1155 fw('\n\t\t\tProperty: "ShadingModel", "KString", "", "%s"' % mat_shader)
1156 fw('\n\t\t\tProperty: "MultiLayer", "bool", "",0')
1157 fw('\n\t\t\tProperty: "EmissiveColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_cold) # emit and diffuse color are he same in blender
1158 fw('\n\t\t\tProperty: "EmissiveFactor", "double", "",%.4f' % mat_emit)
1160 fw('\n\t\t\tProperty: "AmbientColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_colamb)
1161 fw('\n\t\t\tProperty: "AmbientFactor", "double", "",%.4f' % mat_amb)
1162 fw('\n\t\t\tProperty: "DiffuseColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_cold)
1163 fw('\n\t\t\tProperty: "DiffuseFactor", "double", "",%.4f' % mat_dif)
1164 fw('\n\t\t\tProperty: "Bump", "Vector3D", "",0,0,0')
1165 fw('\n\t\t\tProperty: "TransparentColor", "ColorRGB", "",1,1,1')
1166 fw('\n\t\t\tProperty: "TransparencyFactor", "double", "",%.4f' % (1.0 - mat_alpha))
1167 if not mat_shadeless:
1168 fw('\n\t\t\tProperty: "SpecularColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_cols)
1169 fw('\n\t\t\tProperty: "SpecularFactor", "double", "",%.4f' % mat_spec)
1170 fw('\n\t\t\tProperty: "ShininessExponent", "double", "",%.1f' % mat_hard)
1171 fw('\n\t\t\tProperty: "ReflectionColor", "ColorRGB", "",0,0,0')
1172 fw('\n\t\t\tProperty: "ReflectionFactor", "double", "",1')
1173 fw('\n\t\t\tProperty: "Emissive", "ColorRGB", "",0,0,0')
1174 fw('\n\t\t\tProperty: "Ambient", "ColorRGB", "",%.1f,%.1f,%.1f' % mat_colamb)
1175 fw('\n\t\t\tProperty: "Diffuse", "ColorRGB", "",%.1f,%.1f,%.1f' % mat_cold)
1176 if not mat_shadeless:
1177 fw('\n\t\t\tProperty: "Specular", "ColorRGB", "",%.1f,%.1f,%.1f' % mat_cols)
1178 fw('\n\t\t\tProperty: "Shininess", "double", "",%.1f' % mat_hard)
1179 fw('\n\t\t\tProperty: "Opacity", "double", "",%.1f' % mat_alpha)
1180 if not mat_shadeless:
1181 fw('\n\t\t\tProperty: "Reflectivity", "double", "",0')
1183 fw('\n\t\t}')
1184 fw('\n\t}')
1186 # tex is an Image (Arystan)
1187 def write_video(texname, tex):
1188 # Same as texture really!
1189 fw('\n\tVideo: "Video::%s", "Clip" {' % texname)
1191 fw('''
1192 Type: "Clip"
1193 Properties60: {
1194 Property: "FrameRate", "double", "",0
1195 Property: "LastFrame", "int", "",0
1196 Property: "Width", "int", "",0
1197 Property: "Height", "int", "",0''')
1198 if tex:
1199 fname_rel = bpy_extras.io_utils.path_reference(tex.filepath, base_src, base_dst, path_mode, "", copy_set, tex.library)
1200 fname_strip = bpy.path.basename(fname_rel)
1201 else:
1202 fname_strip = fname_rel = ""
1204 fw('\n\t\t\tProperty: "Path", "charptr", "", "%s"' % fname_strip)
1206 fw('''
1207 Property: "StartFrame", "int", "",0
1208 Property: "StopFrame", "int", "",0
1209 Property: "PlaySpeed", "double", "",1
1210 Property: "Offset", "KTime", "",0
1211 Property: "InterlaceMode", "enum", "",0
1212 Property: "FreeRunning", "bool", "",0
1213 Property: "Loop", "bool", "",0
1214 Property: "AccessMode", "enum", "",0
1216 UseMipMap: 0''')
1218 fw('\n\t\tFilename: "%s"' % fname_strip)
1219 fw('\n\t\tRelativeFilename: "%s"' % fname_rel) # make relative
1220 fw('\n\t}')
1222 def write_texture(texname, tex, num):
1223 # if tex is None then this is a dummy tex
1224 fw('\n\tTexture: "Texture::%s", "TextureVideoClip" {' % texname)
1225 fw('\n\t\tType: "TextureVideoClip"')
1226 fw('\n\t\tVersion: 202')
1227 # TODO, rare case _empty_ exists as a name.
1228 fw('\n\t\tTextureName: "Texture::%s"' % texname)
1230 fw('''
1231 Properties60: {
1232 Property: "Translation", "Vector", "A+",0,0,0
1233 Property: "Rotation", "Vector", "A+",0,0,0
1234 Property: "Scaling", "Vector", "A+",1,1,1''')
1235 fw('\n\t\t\tProperty: "Texture alpha", "Number", "A+",%i' % num)
1237 # WrapModeU/V 0==rep, 1==clamp, TODO add support
1238 fw('''
1239 Property: "TextureTypeUse", "enum", "",0
1240 Property: "CurrentTextureBlendMode", "enum", "",1
1241 Property: "UseMaterial", "bool", "",0
1242 Property: "UseMipMap", "bool", "",0
1243 Property: "CurrentMappingType", "enum", "",0
1244 Property: "UVSwap", "bool", "",0''')
1246 fw('\n\t\t\tProperty: "WrapModeU", "enum", "",%i' % tex.use_clamp_x)
1247 fw('\n\t\t\tProperty: "WrapModeV", "enum", "",%i' % tex.use_clamp_y)
1249 fw('''
1250 Property: "TextureRotationPivot", "Vector3D", "",0,0,0
1251 Property: "TextureScalingPivot", "Vector3D", "",0,0,0
1252 Property: "VideoProperty", "object", ""
1253 }''')
1255 fw('\n\t\tMedia: "Video::%s"' % texname)
1257 if tex:
1258 fname_rel = bpy_extras.io_utils.path_reference(tex.filepath, base_src, base_dst, path_mode, "", copy_set, tex.library)
1259 fname_strip = bpy.path.basename(fname_rel)
1260 else:
1261 fname_strip = fname_rel = ""
1263 fw('\n\t\tFileName: "%s"' % fname_strip)
1264 fw('\n\t\tRelativeFilename: "%s"' % fname_rel) # need some make relative command
1266 fw('''
1267 ModelUVTranslation: 0,0
1268 ModelUVScaling: 1,1
1269 Texture_Alpha_Source: "None"
1270 Cropping: 0,0,0,0
1271 }''')
1273 def write_deformer_skin(obname):
1275 Each mesh has its own deformer
1277 fw('\n\tDeformer: "Deformer::Skin %s", "Skin" {' % obname)
1278 fw('''
1279 Version: 100
1280 MultiLayer: 0
1281 Type: "Skin"
1282 Properties60: {
1284 Link_DeformAcuracy: 50
1285 }''')
1287 # in the example was 'Bip01 L Thigh_2'
1288 def write_sub_deformer_skin(my_mesh, my_bone, weights):
1291 Each subdeformer is specific to a mesh, but the bone it links to can be used by many sub-deformers
1292 So the SubDeformer needs the mesh-object name as a prefix to make it unique
1294 Its possible that there is no matching vgroup in this mesh, in that case no verts are in the subdeformer,
1295 a but silly but dosnt really matter
1297 fw('\n\tDeformer: "SubDeformer::Cluster %s %s", "Cluster" {' % (my_mesh.fbxName, my_bone.fbxName))
1299 fw('''
1300 Version: 100
1301 MultiLayer: 0
1302 Type: "Cluster"
1303 Properties60: {
1304 Property: "SrcModel", "object", ""
1305 Property: "SrcModelReference", "object", ""
1307 UserData: "", ""''')
1309 # Support for bone parents
1310 if my_mesh.fbxBoneParent:
1311 if my_mesh.fbxBoneParent == my_bone:
1312 # TODO - this is a bit lazy, we could have a simple write loop
1313 # for this case because all weights are 1.0 but for now this is ok
1314 # Parent Bones arent used all that much anyway.
1315 vgroup_data = [(j, 1.0) for j in range(len(my_mesh.blenData.vertices))]
1316 else:
1317 # This bone is not a parent of this mesh object, no weights
1318 vgroup_data = []
1320 else:
1321 # Normal weight painted mesh
1322 if my_bone.blenName in weights[0]:
1323 # Before we used normalized weight list
1324 group_index = weights[0].index(my_bone.blenName)
1325 vgroup_data = [(j, weight[group_index]) for j, weight in enumerate(weights[1]) if weight[group_index]]
1326 else:
1327 vgroup_data = []
1329 fw('\n\t\tIndexes: ')
1331 i = -1
1332 for vg in vgroup_data:
1333 if i == -1:
1334 fw('%i' % vg[0])
1335 i = 0
1336 else:
1337 if i == 23:
1338 fw('\n\t\t')
1339 i = 0
1340 fw(',%i' % vg[0])
1341 i += 1
1343 fw('\n\t\tWeights: ')
1344 i = -1
1345 for vg in vgroup_data:
1346 if i == -1:
1347 fw('%.8f' % vg[1])
1348 i = 0
1349 else:
1350 if i == 38:
1351 fw('\n\t\t')
1352 i = 0
1353 fw(',%.8f' % vg[1])
1354 i += 1
1356 # Set TransformLink to the global transform of the bone and Transform
1357 # equal to the mesh's transform in bone space.
1358 # http://area.autodesk.com/forum/autodesk-fbx/fbx-sdk/why-the-values-return-by-fbxcluster-gettransformmatrix-x-not-same-with-the-value-in-ascii-fbx-file/
1360 global_bone_matrix = (my_bone.fbxArm.matrixWorld * my_bone.restMatrix) * mtx4_z90
1361 global_mesh_matrix = my_mesh.matrixWorld
1362 transform_matrix = (global_bone_matrix.inverted() * global_mesh_matrix)
1364 global_bone_matrix_string = mat4x4str(global_bone_matrix )
1365 transform_matrix_string = mat4x4str(transform_matrix )
1367 fw('\n\t\tTransform: %s' % transform_matrix_string)
1368 fw('\n\t\tTransformLink: %s' % global_bone_matrix_string)
1369 fw('\n\t}')
1371 def write_mesh(my_mesh):
1372 me = my_mesh.blenData
1374 # if there are non None materials on this mesh
1375 do_materials = bool([m for m in my_mesh.blenMaterials if m is not None])
1376 do_textures = bool([t for t in my_mesh.blenTextures if t is not None])
1377 do_uvs = bool(me.uv_layers)
1378 do_shapekeys = (my_mesh.blenObject.type == 'MESH' and
1379 my_mesh.blenObject.data.shape_keys and
1380 len(my_mesh.blenObject.data.vertices) == len(me.vertices))
1381 # print(len(my_mesh.blenObject.data.vertices), len(me.vertices)) # XXX does not work when org obj is no mesh!
1383 fw('\n\tModel: "Model::%s", "Mesh" {' % my_mesh.fbxName)
1384 fw('\n\t\tVersion: 232') # newline is added in write_object_props
1386 # convert into lists once.
1388 poseMatrix = write_object_props(my_mesh.blenObject, None, my_mesh.parRelMatrix())[3]
1390 # Calculate the global transform for the mesh in the bind pose the same way we do
1391 # in write_sub_deformer_skin
1392 globalMeshBindPose = my_mesh.matrixWorld * mtx4_z90
1393 pose_items.append((my_mesh.fbxName, globalMeshBindPose))
1395 if do_shapekeys:
1396 for kb in my_mesh.blenObject.data.shape_keys.key_blocks[1:]:
1397 fw('\n\t\t\tProperty: "%s", "Number", "AN",0' % kb.name)
1399 fw('\n\t\t}')
1401 fw('\n\t\tMultiLayer: 0'
1402 '\n\t\tMultiTake: 1'
1403 '\n\t\tShading: Y'
1404 '\n\t\tCulling: "CullingOff"'
1412 # Write the Real Mesh data here
1413 fw('\n\t\tVertices: ')
1414 _nchunk = 12 # Number of coordinates per line.
1415 t_co = [None] * len(me.vertices) * 3
1416 me.vertices.foreach_get("co", t_co)
1417 fw(',\n\t\t '.join(','.join('%.6f' % co for co in chunk) for chunk in grouper_exact(t_co, _nchunk)))
1418 del t_co
1420 fw('\n\t\tPolygonVertexIndex: ')
1421 _nchunk = 32 # Number of indices per line.
1422 # A bit more complicated, as we have to ^-1 last index of each loop.
1423 # NOTE: Here we assume that loops order matches polygons order!
1424 t_vi = [None] * len(me.loops)
1425 me.loops.foreach_get("vertex_index", t_vi)
1426 t_ls = [None] * len(me.polygons)
1427 me.polygons.foreach_get("loop_start", t_ls)
1428 if t_ls != sorted(t_ls):
1429 print("Error: polygons and loops orders do not match!")
1430 for ls in t_ls:
1431 t_vi[ls - 1] ^= -1
1432 prep = ',\n\t\t '
1433 fw(prep.join(','.join('%i' % vi for vi in chunk) for chunk in grouper_exact(t_vi, _nchunk)))
1434 del t_vi
1435 del t_ls
1437 if use_mesh_edges:
1438 t_vi = [None] * len(me.edges) * 2
1439 me.edges.foreach_get("vertices", t_vi)
1441 # write loose edges as faces.
1442 t_el = [None] * len(me.edges)
1443 me.edges.foreach_get("is_loose", t_el)
1444 num_lose = sum(t_el)
1445 if num_lose != 0:
1446 it_el = ((vi ^ -1) if (idx % 2) else vi for idx, vi in enumerate(t_vi) if t_el[idx // 2])
1447 if (len(me.loops)):
1448 fw(prep)
1449 fw(prep.join(','.join('%i' % vi for vi in chunk) for chunk in grouper_exact(it_el, _nchunk)))
1451 fw('\n\t\tEdges: ')
1452 fw(',\n\t\t '.join(','.join('%i' % vi for vi in chunk) for chunk in grouper_exact(t_vi, _nchunk)))
1453 del t_vi
1454 del t_el
1456 fw('\n\t\tGeometryVersion: 124')
1458 _nchunk = 12 # Number of coordinates per line.
1459 t_vn = [None] * len(me.loops) * 3
1460 me.calc_normals_split()
1461 # NOTE: Here we assume that loops order matches polygons order!
1462 me.loops.foreach_get("normal", t_vn)
1463 fw('\n\t\tLayerElementNormal: 0 {'
1464 '\n\t\t\tVersion: 101'
1465 '\n\t\t\tName: ""'
1466 '\n\t\t\tMappingInformationType: "ByPolygonVertex"'
1467 '\n\t\t\tReferenceInformationType: "Direct"' # We could save some space with IndexToDirect here too...
1468 '\n\t\t\tNormals: ')
1469 fw(',\n\t\t\t '.join(','.join('%.6f' % n for n in chunk) for chunk in grouper_exact(t_vn, _nchunk)))
1470 fw('\n\t\t}')
1471 del t_vn
1472 me.free_normals_split()
1474 # Write Face Smoothing
1475 _nchunk = 64 # Number of bool per line.
1476 if mesh_smooth_type == 'FACE':
1477 t_ps = [None] * len(me.polygons)
1478 me.polygons.foreach_get("use_smooth", t_ps)
1479 fw('\n\t\tLayerElementSmoothing: 0 {'
1480 '\n\t\t\tVersion: 102'
1481 '\n\t\t\tName: ""'
1482 '\n\t\t\tMappingInformationType: "ByPolygon"'
1483 '\n\t\t\tReferenceInformationType: "Direct"'
1484 '\n\t\t\tSmoothing: ')
1485 fw(',\n\t\t\t '.join(','.join('%d' % b for b in chunk) for chunk in grouper_exact(t_ps, _nchunk)))
1486 fw('\n\t\t}')
1487 del t_ps
1488 elif mesh_smooth_type == 'EDGE':
1489 # Write Edge Smoothing
1490 t_es = [None] * len(me.edges)
1491 me.edges.foreach_get("use_edge_sharp", t_es)
1492 fw('\n\t\tLayerElementSmoothing: 0 {'
1493 '\n\t\t\tVersion: 101'
1494 '\n\t\t\tName: ""'
1495 '\n\t\t\tMappingInformationType: "ByEdge"'
1496 '\n\t\t\tReferenceInformationType: "Direct"'
1497 '\n\t\t\tSmoothing: ')
1498 fw(',\n\t\t\t '
1499 ''.join(','.join('%d' % (not b) for b in chunk) for chunk in grouper_exact(t_es, _nchunk)))
1500 fw('\n\t\t}')
1501 del t_es
1502 elif mesh_smooth_type == 'OFF':
1503 pass
1504 else:
1505 raise Exception("invalid mesh_smooth_type: %r" % mesh_smooth_type)
1507 # Write VertexColor Layers
1508 collayers = []
1509 if len(me.vertex_colors):
1510 collayers = me.vertex_colors
1511 t_lc = [None] * len(me.loops) * 3
1512 col2idx = None
1513 _nchunk = 4 # Number of colors per line
1514 _nchunk_idx = 64 # Number of color indices per line
1515 for colindex, collayer in enumerate(collayers):
1516 collayer.data.foreach_get("color", t_lc)
1517 lc = tuple(zip(*[iter(t_lc)] * 3))
1518 fw('\n\t\tLayerElementColor: %i {'
1519 '\n\t\t\tVersion: 101'
1520 '\n\t\t\tName: "%s"'
1521 '\n\t\t\tMappingInformationType: "ByPolygonVertex"'
1522 '\n\t\t\tReferenceInformationType: "IndexToDirect"'
1523 '\n\t\t\tColors: ' % (colindex, collayer.name))
1525 col2idx = tuple(set(lc))
1526 fw(',\n\t\t\t '.join(','.join('%.6f,%.6f,%.6f,1' % c for c in chunk)
1527 for chunk in grouper_exact(col2idx, _nchunk)))
1529 fw('\n\t\t\tColorIndex: ')
1530 col2idx = {col: idx for idx, col in enumerate(col2idx)}
1531 fw(',\n\t\t\t '
1532 ''.join(','.join('%d' % col2idx[c] for c in chunk) for chunk in grouper_exact(lc, _nchunk_idx)))
1533 fw('\n\t\t}')
1534 del t_lc
1536 # Write UV and texture layers.
1537 uvlayers = []
1538 uvtextures = []
1539 if do_uvs:
1540 uvlayers = me.uv_layers
1541 uvtextures = me.uv_textures
1542 t_uv = [None] * len(me.loops) * 2
1543 t_pi = None
1544 uv2idx = None
1545 tex2idx = None
1546 _nchunk = 6 # Number of UVs per line
1547 _nchunk_idx = 64 # Number of UV indices per line
1548 if do_textures:
1549 is_tex_unique = len(my_mesh.blenTextures) == 1
1550 tex2idx = {None: -1}
1551 tex2idx.update({tex: i for i, tex in enumerate(my_mesh.blenTextures)})
1553 for uvindex, (uvlayer, uvtexture) in enumerate(zip(uvlayers, uvtextures)):
1554 uvlayer.data.foreach_get("uv", t_uv)
1555 uvco = tuple(zip(*[iter(t_uv)] * 2))
1556 fw('\n\t\tLayerElementUV: %d {'
1557 '\n\t\t\tVersion: 101'
1558 '\n\t\t\tName: "%s"'
1559 '\n\t\t\tMappingInformationType: "ByPolygonVertex"'
1560 '\n\t\t\tReferenceInformationType: "IndexToDirect"'
1561 '\n\t\t\tUV: ' % (uvindex, uvlayer.name))
1562 uv2idx = tuple(set(uvco))
1563 fw(',\n\t\t\t '
1564 ''.join(','.join('%.6f,%.6f' % uv for uv in chunk) for chunk in grouper_exact(uv2idx, _nchunk)))
1565 fw('\n\t\t\tUVIndex: ')
1566 uv2idx = {uv: idx for idx, uv in enumerate(uv2idx)}
1567 fw(',\n\t\t\t '
1568 ''.join(','.join('%d' % uv2idx[uv] for uv in chunk) for chunk in grouper_exact(uvco, _nchunk_idx)))
1569 fw('\n\t\t}')
1571 if do_textures:
1572 fw('\n\t\tLayerElementTexture: %d {'
1573 '\n\t\t\tVersion: 101'
1574 '\n\t\t\tName: "%s"'
1575 '\n\t\t\tMappingInformationType: "%s"'
1576 '\n\t\t\tReferenceInformationType: "IndexToDirect"'
1577 '\n\t\t\tBlendMode: "Translucent"'
1578 '\n\t\t\tTextureAlpha: 1'
1579 '\n\t\t\tTextureId: '
1580 % (uvindex, uvlayer.name, ('AllSame' if is_tex_unique else 'ByPolygon')))
1581 if is_tex_unique:
1582 fw('0')
1583 else:
1584 t_pi = (d.image for d in uvtexture.data) # Can't use foreach_get here :(
1585 fw(',\n\t\t\t '.join(','.join('%d' % tex2idx[i] for i in chunk)
1586 for chunk in grouper_exact(t_pi, _nchunk_idx)))
1587 fw('\n\t\t}')
1588 if not do_textures:
1589 fw('\n\t\tLayerElementTexture: 0 {'
1590 '\n\t\t\tVersion: 101'
1591 '\n\t\t\tName: ""'
1592 '\n\t\t\tMappingInformationType: "NoMappingInformation"'
1593 '\n\t\t\tReferenceInformationType: "IndexToDirect"'
1594 '\n\t\t\tBlendMode: "Translucent"'
1595 '\n\t\t\tTextureAlpha: 1'
1596 '\n\t\t\tTextureId: '
1597 '\n\t\t}')
1598 del t_uv
1599 del t_pi
1601 # Done with UV/textures.
1602 if do_materials:
1603 is_mat_unique = len(my_mesh.blenMaterials) == 1
1604 fw('\n\t\tLayerElementMaterial: 0 {'
1605 '\n\t\t\tVersion: 101'
1606 '\n\t\t\tName: ""'
1607 '\n\t\t\tMappingInformationType: "%s"'
1608 '\n\t\t\tReferenceInformationType: "IndexToDirect"'
1609 '\n\t\t\tMaterials: ' % ('AllSame' if is_mat_unique else 'ByPolygon',))
1610 if is_mat_unique:
1611 fw('0')
1612 else:
1613 _nchunk = 64 # Number of material indices per line
1614 # Build a material mapping for this
1615 mat2idx = {mt: i for i, mt in enumerate(my_mesh.blenMaterials)} # (local-mat, tex) -> global index.
1616 mats = my_mesh.blenMaterialList
1617 if me.uv_textures.active and do_uvs:
1618 poly_tex = me.uv_textures.active.data
1619 else:
1620 poly_tex = [None] * len(me.polygons)
1621 _it_mat = (mats[p.material_index] for p in me.polygons)
1622 _it_tex = (pt.image if pt else None for pt in poly_tex) # WARNING - MULTI UV LAYER IMAGES NOT SUPPORTED
1623 t_mti = (mat2idx[m, t] for m, t in zip(_it_mat, _it_tex))
1624 fw(',\n\t\t\t '
1625 ''.join(','.join('%d' % i for i in chunk) for chunk in grouper_exact(t_mti, _nchunk)))
1626 fw('\n\t\t}')
1628 fw('\n\t\tLayer: 0 {'
1629 '\n\t\t\tVersion: 100'
1630 '\n\t\t\tLayerElement: {'
1631 '\n\t\t\t\tType: "LayerElementNormal"'
1632 '\n\t\t\t\tTypedIndex: 0'
1633 '\n\t\t\t}')
1635 # Smoothing info
1636 if mesh_smooth_type != 'OFF':
1637 fw('\n\t\t\tLayerElement: {'
1638 '\n\t\t\t\tType: "LayerElementSmoothing"'
1639 '\n\t\t\t\tTypedIndex: 0'
1640 '\n\t\t\t}')
1642 if me.vertex_colors:
1643 fw('\n\t\t\tLayerElement: {'
1644 '\n\t\t\t\tType: "LayerElementColor"'
1645 '\n\t\t\t\tTypedIndex: 0'
1646 '\n\t\t\t}')
1648 if do_uvs: # same as me.faceUV
1649 fw('\n\t\t\tLayerElement: {'
1650 '\n\t\t\t\tType: "LayerElementUV"'
1651 '\n\t\t\t\tTypedIndex: 0'
1652 '\n\t\t\t}')
1654 # Always write this
1655 #if do_textures:
1656 if True:
1657 fw('\n\t\t\tLayerElement: {'
1658 '\n\t\t\t\tType: "LayerElementTexture"'
1659 '\n\t\t\t\tTypedIndex: 0'
1660 '\n\t\t\t}')
1662 if do_materials:
1663 fw('\n\t\t\tLayerElement: {'
1664 '\n\t\t\t\tType: "LayerElementMaterial"'
1665 '\n\t\t\t\tTypedIndex: 0'
1666 '\n\t\t\t}')
1668 fw('\n\t\t}')
1670 if len(uvlayers) > 1:
1671 for i in range(1, len(uvlayers)):
1672 fw('\n\t\tLayer: %d {'
1673 '\n\t\t\tVersion: 100'
1674 '\n\t\t\tLayerElement: {'
1675 '\n\t\t\t\tType: "LayerElementUV"'
1676 '\n\t\t\t\tTypedIndex: %d'
1677 '\n\t\t\t}' % (i, i))
1678 if do_textures:
1679 fw('\n\t\t\tLayerElement: {'
1680 '\n\t\t\t\tType: "LayerElementTexture"'
1681 '\n\t\t\t\tTypedIndex: %d'
1682 '\n\t\t\t}' % i)
1683 else:
1684 fw('\n\t\t\tLayerElement: {'
1685 '\n\t\t\t\tType: "LayerElementTexture"'
1686 '\n\t\t\t\tTypedIndex: 0'
1687 '\n\t\t\t}')
1688 fw('\n\t\t}')
1690 # XXX Col layers are written before UV ones above, why adding them after UV here???
1691 # And why this offset based on len(UV layers) - 1???
1692 # I have the feeling some indices are wrong here!
1693 # --mont29
1694 if len(collayers) > 1:
1695 # Take into account any UV layers
1696 layer_offset = len(uvlayers) - 1 if uvlayers else 0
1697 for i in range(layer_offset, len(collayers) + layer_offset):
1698 fw('\n\t\tLayer: %d {'
1699 '\n\t\t\tVersion: 100'
1700 '\n\t\t\tLayerElement: {'
1701 '\n\t\t\t\tType: "LayerElementColor"'
1702 '\n\t\t\t\tTypedIndex: %d'
1703 '\n\t\t\t}'
1704 '\n\t\t}' % (i, i))
1706 if do_shapekeys:
1707 # Not sure this works really good...
1708 # Aren't key's co already relative if set as such?
1709 # Also, does not handle custom relative option for each key...
1710 # --mont29
1711 import operator
1712 key_blocks = my_mesh.blenObject.data.shape_keys.key_blocks[:]
1713 t_sk_basis = [None] * len(me.vertices) * 3
1714 t_sk = [None] * len(me.vertices) * 3
1715 key_blocks[0].data.foreach_get("co", t_sk_basis)
1716 _nchunk = 4 # Number of delta coordinates per line
1717 _nchunk_idx = 32 # Number of vert indices per line
1719 for kb in key_blocks[1:]:
1720 kb.data.foreach_get("co", t_sk)
1721 _dcos = tuple(zip(*[map(operator.sub, t_sk, t_sk_basis)] * 3))
1722 verts = tuple(i for i, dco in enumerate(_dcos) if sum(map(operator.pow, dco, (2, 2, 2))) > 3e-12)
1723 dcos = (_dcos[i] for i in verts)
1724 fw('\n\t\tShape: "%s" {'
1725 '\n\t\t\tIndexes: ' % kb.name)
1726 fw(',\n\t\t\t '
1727 ''.join(','.join('%d' % i for i in chunk) for chunk in grouper_exact(verts, _nchunk_idx)))
1729 fw('\n\t\t\tVertices: ')
1730 fw(',\n\t\t\t '
1731 ''.join(','.join('%.6f,%.6f,%.6f' % c for c in chunk) for chunk in grouper_exact(dcos, _nchunk)))
1732 # all zero, why? - campbell
1733 # Would need to recompute them I guess... and I assume those are supposed to be delta as well?
1734 fw('\n\t\t\tNormals: ')
1735 fw(',\n\t\t\t '
1736 ''.join(','.join('0,0,0' for c in chunk) for chunk in grouper_exact(range(len(verts)), _nchunk)))
1737 fw('\n\t\t}')
1738 del t_sk_basis
1739 del t_sk
1741 fw('\n\t}')
1743 def write_group(name):
1744 fw('\n\tGroupSelection: "GroupSelection::%s", "Default" {' % name)
1746 fw('''
1747 Properties60: {
1748 Property: "MultiLayer", "bool", "",0
1749 Property: "Pickable", "bool", "",1
1750 Property: "Transformable", "bool", "",1
1751 Property: "Show", "bool", "",1
1753 MultiLayer: 0
1754 }''')
1756 # add meshes here to clear because they are not used anywhere.
1757 meshes_to_clear = []
1759 ob_meshes = []
1760 ob_lights = []
1761 ob_cameras = []
1762 # in fbx we export bones as children of the mesh
1763 # armatures not a part of a mesh, will be added to ob_arms
1764 ob_bones = []
1765 ob_arms = []
1766 ob_null = [] # emptys
1768 # List of types that have blender objects (not bones)
1769 ob_all_typegroups = [ob_meshes, ob_lights, ob_cameras, ob_arms, ob_null]
1771 groups = [] # blender groups, only add ones that have objects in the selections
1772 materials = set() # (mat, image) items
1773 textures = set()
1775 tmp_ob_type = None # in case no objects are exported, so as not to raise an error
1777 ## XXX
1779 if 'ARMATURE' in object_types:
1780 # This is needed so applying modifiers dosnt apply the armature deformation, its also needed
1781 # ...so mesh objects return their rest worldspace matrix when bone-parents are exported as weighted meshes.
1782 # set every armature to its rest, backup the original values so we done mess up the scene
1783 ob_arms_orig_rest = [arm.pose_position for arm in bpy.data.armatures]
1785 for arm in bpy.data.armatures:
1786 arm.pose_position = 'REST'
1788 if ob_arms_orig_rest:
1789 for ob_base in bpy.data.objects:
1790 if ob_base.type == 'ARMATURE':
1791 ob_base.update_tag()
1793 # This causes the makeDisplayList command to effect the mesh
1794 scene.frame_set(scene.frame_current)
1796 for ob_base in context_objects:
1798 # ignore dupli children
1799 if ob_base.parent and ob_base.parent.dupli_type in {'VERTS', 'FACES'}:
1800 continue
1802 obs = [(ob_base, ob_base.matrix_world.copy())]
1803 if ob_base.dupli_type != 'NONE':
1804 ob_base.dupli_list_create(scene)
1805 obs = [(dob.object, dob.matrix.copy()) for dob in ob_base.dupli_list]
1807 for ob, mtx in obs:
1808 tmp_ob_type = ob.type
1809 if tmp_ob_type == 'CAMERA':
1810 if 'CAMERA' in object_types:
1811 ob_cameras.append(my_object_generic(ob, mtx))
1812 elif tmp_ob_type == 'LAMP':
1813 if 'LAMP' in object_types:
1814 ob_lights.append(my_object_generic(ob, mtx))
1815 elif tmp_ob_type == 'ARMATURE':
1816 if 'ARMATURE' in object_types:
1817 # TODO - armatures dont work in dupligroups!
1818 if ob not in ob_arms:
1819 ob_arms.append(ob)
1820 # ob_arms.append(ob) # replace later. was "ob_arms.append(sane_obname(ob), ob)"
1821 elif tmp_ob_type == 'EMPTY':
1822 if 'EMPTY' in object_types:
1823 ob_null.append(my_object_generic(ob, mtx))
1824 elif 'MESH' in object_types:
1825 origData = True
1826 if tmp_ob_type != 'MESH':
1827 try:
1828 me = ob.to_mesh(scene, True, 'PREVIEW')
1829 except:
1830 me = None
1832 if me:
1833 meshes_to_clear.append(me)
1834 mats = me.materials
1835 origData = False
1836 else:
1837 # Mesh Type!
1838 if use_mesh_modifiers:
1839 me = ob.to_mesh(scene, True, 'PREVIEW')
1841 # print ob, me, me.getVertGroupNames()
1842 meshes_to_clear.append(me)
1843 origData = False
1844 mats = me.materials
1845 else:
1846 me = ob.data
1847 me.update()
1848 mats = me.materials
1850 # # Support object colors
1851 # tmp_colbits = ob.colbits
1852 # if tmp_colbits:
1853 # tmp_ob_mats = ob.getMaterials(1) # 1 so we get None's too.
1854 # for i in xrange(16):
1855 # if tmp_colbits & (1<<i):
1856 # mats[i] = tmp_ob_mats[i]
1857 # del tmp_ob_mats
1858 # del tmp_colbits
1860 if me:
1861 # # This WILL modify meshes in blender if use_mesh_modifiers is disabled.
1862 # # so strictly this is bad. but only in rare cases would it have negative results
1863 # # say with dupliverts the objects would rotate a bit differently
1864 # if EXP_MESH_HQ_NORMALS:
1865 # BPyMesh.meshCalcNormals(me) # high quality normals nice for realtime engines.
1867 if not mats:
1868 mats = [None]
1870 texture_set_local = set()
1871 material_set_local = set()
1872 if me.uv_textures:
1873 for uvlayer in me.uv_textures:
1874 for p, p_uv in zip(me.polygons, uvlayer.data):
1875 tex = p_uv.image
1876 texture_set_local.add(tex)
1877 mat = mats[p.material_index]
1879 # Should not be needed anymore.
1880 #try:
1881 #mat = mats[p.material_index]
1882 #except:
1883 #mat = None
1885 material_set_local.add((mat, tex))
1887 else:
1888 for mat in mats:
1889 # 2.44 use mat.lib too for uniqueness
1890 material_set_local.add((mat, None))
1892 textures |= texture_set_local
1893 materials |= material_set_local
1895 if 'ARMATURE' in object_types:
1896 armob = ob.find_armature()
1897 blenParentBoneName = None
1899 # parent bone - special case
1900 if (not armob) and ob.parent and ob.parent.type == 'ARMATURE' and \
1901 ob.parent_type == 'BONE':
1902 armob = ob.parent
1903 blenParentBoneName = ob.parent_bone
1905 if armob and armob not in ob_arms:
1906 ob_arms.append(armob)
1908 # Warning for scaled, mesh objects with armatures
1909 if abs(ob.scale[0] - 1.0) > 0.05 or abs(ob.scale[1] - 1.0) > 0.05 or abs(ob.scale[1] - 1.0) > 0.05:
1910 operator.report(
1911 {'WARNING'},
1912 "Object '%s' has a scale of (%.3f, %.3f, %.3f), "
1913 "Armature deformation will not work as expected "
1914 "(apply Scale to fix)" % (ob.name, *ob.scale))
1916 else:
1917 blenParentBoneName = armob = None
1919 my_mesh = my_object_generic(ob, mtx)
1920 my_mesh.blenData = me
1921 my_mesh.origData = origData
1922 my_mesh.blenMaterials = list(material_set_local)
1923 my_mesh.blenMaterialList = mats
1924 my_mesh.blenTextures = list(texture_set_local)
1926 # sort the name so we get predictable output, some items may be NULL
1927 my_mesh.blenMaterials.sort(key=lambda m: (getattr(m[0], "name", ""), getattr(m[1], "name", "")))
1928 my_mesh.blenTextures.sort(key=lambda m: getattr(m, "name", ""))
1930 # if only 1 null texture then empty the list
1931 if len(my_mesh.blenTextures) == 1 and my_mesh.blenTextures[0] is None:
1932 my_mesh.blenTextures = []
1934 my_mesh.fbxArm = armob # replace with my_object_generic armature instance later
1935 my_mesh.fbxBoneParent = blenParentBoneName # replace with my_bone instance later
1937 ob_meshes.append(my_mesh)
1939 # not forgetting to free dupli_list
1940 if ob_base.dupli_list:
1941 ob_base.dupli_list_clear()
1943 if 'ARMATURE' in object_types:
1944 # now we have the meshes, restore the rest arm position
1945 for i, arm in enumerate(bpy.data.armatures):
1946 arm.pose_position = ob_arms_orig_rest[i]
1948 if ob_arms_orig_rest:
1949 for ob_base in bpy.data.objects:
1950 if ob_base.type == 'ARMATURE':
1951 ob_base.update_tag()
1952 # This causes the makeDisplayList command to effect the mesh
1953 scene.frame_set(scene.frame_current)
1955 del tmp_ob_type, context_objects
1957 # now we have collected all armatures, add bones
1958 for i, ob in enumerate(ob_arms):
1960 ob_arms[i] = my_arm = my_object_generic(ob)
1962 my_arm.fbxBones = []
1963 my_arm.blenData = ob.data
1964 if ob.animation_data:
1965 my_arm.blenAction = ob.animation_data.action
1966 else:
1967 my_arm.blenAction = None
1968 my_arm.blenActionList = []
1970 # fbxName, blenderObject, my_bones, blenderActions
1971 #ob_arms[i] = fbxArmObName, ob, arm_my_bones, (ob.action, [])
1973 if use_armature_deform_only:
1974 # tag non deforming bones that have no deforming children
1975 deform_map = dict.fromkeys(my_arm.blenData.bones, False)
1976 for bone in my_arm.blenData.bones:
1977 if bone.use_deform:
1978 deform_map[bone] = True
1979 # tag all parents, even ones that are not deform since their child _is_
1980 for parent in bone.parent_recursive:
1981 deform_map[parent] = True
1983 for bone in my_arm.blenData.bones:
1985 if use_armature_deform_only:
1986 # if this bone doesnt deform, and none of its children deform, skip it!
1987 if not deform_map[bone]:
1988 continue
1990 my_bone = my_bone_class(bone, my_arm)
1991 my_arm.fbxBones.append(my_bone)
1992 ob_bones.append(my_bone)
1994 if use_armature_deform_only:
1995 del deform_map
1997 # add the meshes to the bones and replace the meshes armature with own armature class
1998 #for obname, ob, mtx, me, mats, arm, armname in ob_meshes:
1999 for my_mesh in ob_meshes:
2000 # Replace
2001 # ...this could be sped up with dictionary mapping but its unlikely for
2002 # it ever to be a bottleneck - (would need 100+ meshes using armatures)
2003 if my_mesh.fbxArm:
2004 for my_arm in ob_arms:
2005 if my_arm.blenObject == my_mesh.fbxArm:
2006 my_mesh.fbxArm = my_arm
2007 break
2009 for my_bone in ob_bones:
2011 # The mesh uses this bones armature!
2012 if my_bone.fbxArm == my_mesh.fbxArm:
2013 if my_bone.blenBone.use_deform:
2014 my_bone.blenMeshes[my_mesh.fbxName] = me
2016 # parent bone: replace bone names with our class instances
2017 # my_mesh.fbxBoneParent is None or a blender bone name initialy, replacing if the names match.
2018 if my_mesh.fbxBoneParent == my_bone.blenName:
2019 my_mesh.fbxBoneParent = my_bone
2021 bone_deformer_count = 0 # count how many bones deform a mesh
2022 my_bone_blenParent = None
2023 for my_bone in ob_bones:
2024 my_bone_blenParent = my_bone.blenBone.parent
2025 if my_bone_blenParent:
2026 for my_bone_parent in ob_bones:
2027 # Note 2.45rc2 you can compare bones normally
2028 if my_bone_blenParent.name == my_bone_parent.blenName and my_bone.fbxArm == my_bone_parent.fbxArm:
2029 my_bone.parent = my_bone_parent
2030 break
2032 # Not used at the moment
2033 # my_bone.calcRestMatrixLocal()
2034 bone_deformer_count += len(my_bone.blenMeshes)
2036 del my_bone_blenParent
2038 # Build blenObject -> fbxObject mapping
2039 # this is needed for groups as well as fbxParenting
2040 bpy.data.objects.tag(False)
2042 # using a list of object names for tagging (Arystan)
2044 tmp_obmapping = {}
2045 for ob_generic in ob_all_typegroups:
2046 for ob_base in ob_generic:
2047 ob_base.blenObject.tag = True
2048 tmp_obmapping[ob_base.blenObject] = ob_base
2050 # Build Groups from objects we export
2051 for blenGroup in bpy.data.groups:
2052 fbxGroupName = None
2053 for ob in blenGroup.objects:
2054 if ob.tag:
2055 if fbxGroupName is None:
2056 fbxGroupName = sane_groupname(blenGroup)
2057 groups.append((fbxGroupName, blenGroup))
2059 tmp_obmapping[ob].fbxGroupNames.append(fbxGroupName) # also adds to the objects fbxGroupNames
2061 groups.sort() # not really needed
2063 # Assign parents using this mapping
2064 for ob_generic in ob_all_typegroups:
2065 for my_ob in ob_generic:
2066 parent = my_ob.blenObject.parent
2067 if parent and parent.tag: # does it exist and is it in the mapping
2068 my_ob.fbxParent = tmp_obmapping[parent]
2070 del tmp_obmapping
2071 # Finished finding groups we use
2073 # == WRITE OBJECTS TO THE FILE ==
2074 # == From now on we are building the FBX file from the information collected above (JCB)
2076 materials = [(sane_matname(mat_tex_pair), mat_tex_pair) for mat_tex_pair in materials]
2077 textures = [(sane_texname(tex), tex) for tex in textures if tex]
2078 materials.sort(key=lambda m: m[0]) # sort by name
2079 textures.sort(key=lambda m: m[0])
2081 camera_count = 8 if 'CAMERA' in object_types else 0
2083 # sanity checks
2084 try:
2085 assert(not (ob_meshes and ('MESH' not in object_types)))
2086 assert(not (materials and ('MESH' not in object_types)))
2087 assert(not (textures and ('MESH' not in object_types)))
2089 assert(not (ob_lights and ('LAMP' not in object_types)))
2091 assert(not (ob_cameras and ('CAMERA' not in object_types)))
2092 except AssertionError:
2093 import traceback
2094 traceback.print_exc()
2096 fw('''
2098 ; Object definitions
2099 ;------------------------------------------------------------------
2101 Definitions: {
2102 Version: 100
2103 Count: %i''' % (
2104 1 + camera_count +
2105 len(ob_meshes) +
2106 len(ob_lights) +
2107 len(ob_cameras) +
2108 len(ob_arms) +
2109 len(ob_null) +
2110 len(ob_bones) +
2111 bone_deformer_count +
2112 len(materials) +
2113 (len(textures) * 2))) # add 1 for global settings
2115 del bone_deformer_count
2117 fw('''
2118 ObjectType: "Model" {
2119 Count: %i
2120 }''' % (
2121 camera_count +
2122 len(ob_meshes) +
2123 len(ob_lights) +
2124 len(ob_cameras) +
2125 len(ob_arms) +
2126 len(ob_null) +
2127 len(ob_bones)))
2129 fw('''
2130 ObjectType: "Geometry" {
2131 Count: %i
2132 }''' % len(ob_meshes))
2134 if materials:
2135 fw('''
2136 ObjectType: "Material" {
2137 Count: %i
2138 }''' % len(materials))
2140 if textures:
2141 fw('''
2142 ObjectType: "Texture" {
2143 Count: %i
2144 }''' % len(textures)) # add 1 for an empty tex
2145 fw('''
2146 ObjectType: "Video" {
2147 Count: %i
2148 }''' % len(textures)) # add 1 for an empty tex
2150 tmp = 0
2151 # Add deformer nodes
2152 for my_mesh in ob_meshes:
2153 if my_mesh.fbxArm:
2154 tmp += 1
2156 # Add subdeformers
2157 for my_bone in ob_bones:
2158 tmp += len(my_bone.blenMeshes)
2160 if tmp:
2161 fw('''
2162 ObjectType: "Deformer" {
2163 Count: %i
2164 }''' % tmp)
2165 del tmp
2167 # Bind pose is essential for XNA if the 'MESH' is included,
2168 # but could be removed now?
2169 fw('''
2170 ObjectType: "Pose" {
2171 Count: 1
2172 }''')
2174 if groups:
2175 fw('''
2176 ObjectType: "GroupSelection" {
2177 Count: %i
2178 }''' % len(groups))
2180 fw('''
2181 ObjectType: "GlobalSettings" {
2182 Count: 1
2184 }''')
2186 fw('''
2188 ; Object properties
2189 ;------------------------------------------------------------------
2191 Objects: {''')
2193 if 'CAMERA' in object_types:
2194 # To comply with other FBX FILES
2195 write_camera_switch()
2197 for my_null in ob_null:
2198 write_null(my_null)
2200 # XNA requires the armature to be a Limb (JCB)
2201 # Note, 2.58 and previous wrote these as normal empties and it worked mostly (except for XNA)
2202 for my_arm in ob_arms:
2203 write_null(my_arm, fbxType="Limb", fbxTypeFlags="Skeleton")
2205 for my_cam in ob_cameras:
2206 write_camera(my_cam)
2208 for my_light in ob_lights:
2209 write_light(my_light)
2211 for my_mesh in ob_meshes:
2212 write_mesh(my_mesh)
2214 #for bonename, bone, obname, me, armob in ob_bones:
2215 for my_bone in ob_bones:
2216 write_bone(my_bone)
2218 if 'CAMERA' in object_types:
2219 write_camera_default()
2221 for matname, (mat, tex) in materials:
2222 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)
2224 # each texture uses a video, odd
2225 for texname, tex in textures:
2226 write_video(texname, tex)
2227 i = 0
2228 for texname, tex in textures:
2229 write_texture(texname, tex, i)
2230 i += 1
2232 for groupname, group in groups:
2233 write_group(groupname)
2235 # NOTE - c4d and motionbuilder dont need normalized weights, but deep-exploration 5 does and (max?) do.
2237 # Write armature modifiers
2238 # TODO - add another MODEL? - because of this skin definition.
2239 for my_mesh in ob_meshes:
2240 if my_mesh.fbxArm:
2241 write_deformer_skin(my_mesh.fbxName)
2243 # Get normalized weights for temorary use
2244 if my_mesh.fbxBoneParent:
2245 weights = None
2246 else:
2247 weights = meshNormalizedWeights(my_mesh.blenObject, my_mesh.blenData)
2249 #for bonename, bone, obname, bone_mesh, armob in ob_bones:
2250 for my_bone in ob_bones:
2251 if me in iter(my_bone.blenMeshes.values()):
2252 write_sub_deformer_skin(my_mesh, my_bone, weights)
2254 # Write pose is really weird, only needed when an armature and mesh are used together
2255 # each by themselves do not need pose data. For now only pose meshes and bones
2257 # Bind pose is essential for XNA if the 'MESH' is included (JCB)
2258 fw('''
2259 Pose: "Pose::BIND_POSES", "BindPose" {
2260 Type: "BindPose"
2261 Version: 100
2262 Properties60: {
2264 NbPoseNodes: ''')
2265 fw(str(len(pose_items)))
2267 for fbxName, matrix in pose_items:
2268 fw('\n\t\tPoseNode: {')
2269 fw('\n\t\t\tNode: "Model::%s"' % fbxName)
2270 fw('\n\t\t\tMatrix: %s' % mat4x4str(matrix if matrix else Matrix()))
2271 fw('\n\t\t}')
2273 fw('\n\t}')
2275 # Finish Writing Objects
2276 # Write global settings
2277 fw('''
2278 GlobalSettings: {
2279 Version: 1000
2280 Properties60: {
2281 Property: "UpAxis", "int", "",1
2282 Property: "UpAxisSign", "int", "",1
2283 Property: "FrontAxis", "int", "",2
2284 Property: "FrontAxisSign", "int", "",1
2285 Property: "CoordAxis", "int", "",0
2286 Property: "CoordAxisSign", "int", "",1
2287 Property: "UnitScaleFactor", "double", "",1
2290 ''')
2291 fw('}')
2293 fw('''
2295 ; Object relations
2296 ;------------------------------------------------------------------
2298 Relations: {''')
2300 # Nulls are likely to cause problems for XNA
2302 for my_null in ob_null:
2303 fw('\n\tModel: "Model::%s", "Null" {\n\t}' % my_null.fbxName)
2305 # Armature must be a Limb for XNA
2306 # Note, 2.58 and previous wrote these as normal empties and it worked mostly (except for XNA)
2307 for my_arm in ob_arms:
2308 fw('\n\tModel: "Model::%s", "Limb" {\n\t}' % my_arm.fbxName)
2310 for my_mesh in ob_meshes:
2311 fw('\n\tModel: "Model::%s", "Mesh" {\n\t}' % my_mesh.fbxName)
2313 # TODO - limbs can have the same name for multiple armatures, should prefix.
2314 #for bonename, bone, obname, me, armob in ob_bones:
2315 for my_bone in ob_bones:
2316 fw('\n\tModel: "Model::%s", "Limb" {\n\t}' % my_bone.fbxName)
2318 for my_cam in ob_cameras:
2319 fw('\n\tModel: "Model::%s", "Camera" {\n\t}' % my_cam.fbxName)
2321 for my_light in ob_lights:
2322 fw('\n\tModel: "Model::%s", "Light" {\n\t}' % my_light.fbxName)
2324 fw('''
2325 Model: "Model::Producer Perspective", "Camera" {
2327 Model: "Model::Producer Top", "Camera" {
2329 Model: "Model::Producer Bottom", "Camera" {
2331 Model: "Model::Producer Front", "Camera" {
2333 Model: "Model::Producer Back", "Camera" {
2335 Model: "Model::Producer Right", "Camera" {
2337 Model: "Model::Producer Left", "Camera" {
2339 Model: "Model::Camera Switcher", "CameraSwitcher" {
2340 }''')
2342 for matname, (mat, tex) in materials:
2343 fw('\n\tMaterial: "Material::%s", "" {\n\t}' % matname)
2345 if textures:
2346 for texname, tex in textures:
2347 fw('\n\tTexture: "Texture::%s", "TextureVideoClip" {\n\t}' % texname)
2348 for texname, tex in textures:
2349 fw('\n\tVideo: "Video::%s", "Clip" {\n\t}' % texname)
2351 # deformers - modifiers
2352 for my_mesh in ob_meshes:
2353 if my_mesh.fbxArm:
2354 fw('\n\tDeformer: "Deformer::Skin %s", "Skin" {\n\t}' % my_mesh.fbxName)
2356 #for bonename, bone, obname, me, armob in ob_bones:
2357 for my_bone in ob_bones:
2358 for fbxMeshObName in my_bone.blenMeshes: # .keys() - fbxMeshObName
2359 # is this bone effecting a mesh?
2360 fw('\n\tDeformer: "SubDeformer::Cluster %s %s", "Cluster" {\n\t}' % (fbxMeshObName, my_bone.fbxName))
2362 # This should be at the end
2363 # fw('\n\tPose: "Pose::BIND_POSES", "BindPose" {\n\t}')
2365 for groupname, group in groups:
2366 fw('\n\tGroupSelection: "GroupSelection::%s", "Default" {\n\t}' % groupname)
2368 fw('\n}')
2369 fw('''
2371 ; Object connections
2372 ;------------------------------------------------------------------
2374 Connections: {''')
2376 # NOTE - The FBX SDK does not care about the order but some importers DO!
2377 # for instance, defining the material->mesh connection
2378 # before the mesh->parent crashes cinema4d
2380 for ob_generic in ob_all_typegroups: # all blender 'Object's we support
2381 for my_ob in ob_generic:
2382 # for deformed meshes, don't have any parents or they can get twice transformed.
2383 if my_ob.fbxParent and (not my_ob.fbxArm):
2384 fw('\n\tConnect: "OO", "Model::%s", "Model::%s"' % (my_ob.fbxName, my_ob.fbxParent.fbxName))
2385 else:
2386 fw('\n\tConnect: "OO", "Model::%s", "Model::Scene"' % my_ob.fbxName)
2388 if materials:
2389 for my_mesh in ob_meshes:
2390 # Connect all materials to all objects, not good form but ok for now.
2391 for mat, tex in my_mesh.blenMaterials:
2392 mat_name = mat.name if mat else None
2393 tex_name = tex.name if tex else None
2395 fw('\n\tConnect: "OO", "Material::%s", "Model::%s"' % (sane_name_mapping_mat[mat_name, tex_name], my_mesh.fbxName))
2397 if textures:
2398 for my_mesh in ob_meshes:
2399 if my_mesh.blenTextures:
2400 # fw('\n\tConnect: "OO", "Texture::_empty_", "Model::%s"' % my_mesh.fbxName)
2401 for tex in my_mesh.blenTextures:
2402 if tex:
2403 fw('\n\tConnect: "OO", "Texture::%s", "Model::%s"' % (sane_name_mapping_tex[tex.name], my_mesh.fbxName))
2405 for texname, tex in textures:
2406 fw('\n\tConnect: "OO", "Video::%s", "Texture::%s"' % (texname, texname))
2408 if 'MESH' in object_types:
2409 for my_mesh in ob_meshes:
2410 if my_mesh.fbxArm:
2411 fw('\n\tConnect: "OO", "Deformer::Skin %s", "Model::%s"' % (my_mesh.fbxName, my_mesh.fbxName))
2413 for my_bone in ob_bones:
2414 for fbxMeshObName in my_bone.blenMeshes: # .keys()
2415 fw('\n\tConnect: "OO", "SubDeformer::Cluster %s %s", "Deformer::Skin %s"' % (fbxMeshObName, my_bone.fbxName, fbxMeshObName))
2417 # limbs -> deformers
2418 for my_bone in ob_bones:
2419 for fbxMeshObName in my_bone.blenMeshes: # .keys()
2420 fw('\n\tConnect: "OO", "Model::%s", "SubDeformer::Cluster %s %s"' % (my_bone.fbxName, fbxMeshObName, my_bone.fbxName))
2422 #for bonename, bone, obname, me, armob in ob_bones:
2423 for my_bone in ob_bones:
2424 # Always parent to armature now
2425 if my_bone.parent:
2426 fw('\n\tConnect: "OO", "Model::%s", "Model::%s"' % (my_bone.fbxName, my_bone.parent.fbxName))
2427 else:
2428 # the armature object is written as an empty and all root level bones connect to it
2429 fw('\n\tConnect: "OO", "Model::%s", "Model::%s"' % (my_bone.fbxName, my_bone.fbxArm.fbxName))
2431 # groups
2432 if groups:
2433 for ob_generic in ob_all_typegroups:
2434 for ob_base in ob_generic:
2435 for fbxGroupName in ob_base.fbxGroupNames:
2436 fw('\n\tConnect: "OO", "Model::%s", "GroupSelection::%s"' % (ob_base.fbxName, fbxGroupName))
2438 # I think the following always duplicates the armature connection because it is also in ob_all_typegroups above! (JCB)
2439 # for my_arm in ob_arms:
2440 # fw('\n\tConnect: "OO", "Model::%s", "Model::Scene"' % my_arm.fbxName)
2442 fw('\n}')
2444 # Needed for scene footer as well as animation
2445 render = scene.render
2447 # from the FBX sdk
2448 #define KTIME_ONE_SECOND KTime (K_LONGLONG(46186158000))
2449 def fbx_time(t):
2450 # 0.5 + val is the same as rounding.
2451 return int(0.5 + ((t / fps) * 46186158000))
2453 fps = float(render.fps)
2454 start = scene.frame_start
2455 end = scene.frame_end
2456 if end < start:
2457 start, end = end, start
2459 # comment the following line, otherwise we dont get the pose
2460 # if start==end: use_anim = False
2462 # animations for these object types
2463 ob_anim_lists = ob_bones, ob_meshes, ob_null, ob_cameras, ob_lights, ob_arms
2465 if use_anim and [tmp for tmp in ob_anim_lists if tmp]:
2467 frame_orig = scene.frame_current
2469 if use_anim_optimize:
2470 # Do we really want to keep such behavior? User could enter real value directly...
2471 ANIM_OPTIMIZE_PRECISSION_FLOAT = 10 ** (-anim_optimize_precision + 2)
2473 # default action, when no actions are avaioable
2474 tmp_actions = []
2475 blenActionDefault = None
2476 action_lastcompat = None
2478 # instead of tagging
2479 tagged_actions = []
2481 # get the current action first so we can use it if we only export one action (JCB)
2482 for my_arm in ob_arms:
2483 blenActionDefault = my_arm.blenAction
2484 if blenActionDefault:
2485 break
2487 if use_anim_action_all:
2488 tmp_actions = bpy.data.actions[:]
2489 elif not use_default_take:
2490 if blenActionDefault:
2491 # Export the current action (JCB)
2492 tmp_actions.append(blenActionDefault)
2494 if tmp_actions:
2495 # find which actions are compatible with the armatures
2496 tmp_act_count = 0
2497 for my_arm in ob_arms:
2499 arm_bone_names = set([my_bone.blenName for my_bone in my_arm.fbxBones])
2501 for action in tmp_actions:
2503 if arm_bone_names.intersection(action_bone_names(my_arm.blenObject, action)): # at least one channel matches.
2504 my_arm.blenActionList.append(action)
2505 tagged_actions.append(action.name)
2506 tmp_act_count += 1
2508 # in case there are no actions applied to armatures
2509 # for example, when a user deletes the current action.
2510 action_lastcompat = action
2512 if tmp_act_count:
2513 # unlikely to ever happen but if no actions applied to armatures, just use the last compatible armature.
2514 if not blenActionDefault:
2515 blenActionDefault = action_lastcompat
2517 del action_lastcompat
2519 if use_default_take:
2520 tmp_actions.insert(0, None) # None is the default action
2522 fw('''
2523 ;Takes and animation section
2524 ;----------------------------------------------------
2526 Takes: {''')
2528 if blenActionDefault and not use_default_take:
2529 fw('\n\tCurrent: "%s"' % sane_takename(blenActionDefault))
2530 else:
2531 fw('\n\tCurrent: "Default Take"')
2533 for blenAction in tmp_actions:
2534 # we have tagged all actious that are used be selected armatures
2535 if blenAction:
2536 if blenAction.name in tagged_actions:
2537 print('\taction: "%s" exporting...' % blenAction.name)
2538 else:
2539 print('\taction: "%s" has no armature using it, skipping' % blenAction.name)
2540 continue
2542 if blenAction is None:
2543 # Warning, this only accounts for tmp_actions being [None]
2544 take_name = "Default Take"
2545 act_start = start
2546 act_end = end
2547 else:
2548 # use existing name
2549 take_name = sane_name_mapping_take.get(blenAction.name)
2550 if take_name is None:
2551 take_name = sane_takename(blenAction)
2553 act_start, act_end = blenAction.frame_range
2554 act_start = int(act_start)
2555 act_end = int(act_end)
2557 # Set the action active
2558 for my_arm in ob_arms:
2559 if my_arm.blenObject.animation_data and blenAction in my_arm.blenActionList:
2560 my_arm.blenObject.animation_data.action = blenAction
2562 # Use the action name as the take name and the take filename (JCB)
2563 fw('\n\tTake: "%s" {' % take_name)
2564 fw('\n\t\tFileName: "%s.tak"' % take_name.replace(" ", "_"))
2565 fw('\n\t\tLocalTime: %i,%i' % (fbx_time(act_start - 1), fbx_time(act_end - 1))) # ??? - not sure why this is needed
2566 fw('\n\t\tReferenceTime: %i,%i' % (fbx_time(act_start - 1), fbx_time(act_end - 1))) # ??? - not sure why this is needed
2568 fw('''
2570 ;Models animation
2571 ;----------------------------------------------------''')
2573 # set pose data for all bones
2574 # do this here in case the action changes
2576 for my_bone in ob_bones:
2577 my_bone.flushAnimData()
2579 i = act_start
2580 while i <= act_end:
2581 scene.frame_set(i)
2582 for ob_generic in ob_anim_lists:
2583 for my_ob in ob_generic:
2584 #Blender.Window.RedrawAll()
2585 if ob_generic == ob_meshes and my_ob.fbxArm:
2586 # We cant animate armature meshes!
2587 my_ob.setPoseFrame(i, fake=True)
2588 else:
2589 my_ob.setPoseFrame(i)
2591 i += 1
2593 #for bonename, bone, obname, me, armob in ob_bones:
2594 for ob_generic in (ob_bones, ob_meshes, ob_null, ob_cameras, ob_lights, ob_arms):
2596 for my_ob in ob_generic:
2598 if ob_generic == ob_meshes and my_ob.fbxArm:
2599 # do nothing,
2600 pass
2601 else:
2603 fw('\n\t\tModel: "Model::%s" {' % my_ob.fbxName) # ??? - not sure why this is needed
2604 fw('\n\t\t\tVersion: 1.1')
2605 fw('\n\t\t\tChannel: "Transform" {')
2607 context_bone_anim_mats = [(my_ob.getAnimParRelMatrix(frame), my_ob.getAnimParRelMatrixRot(frame)) for frame in range(act_start, act_end + 1)]
2609 # ----------------
2610 # ----------------
2611 for TX_LAYER, TX_CHAN in enumerate('TRS'): # transform, rotate, scale
2613 if TX_CHAN == 'T':
2614 context_bone_anim_vecs = [mtx[0].to_translation() for mtx in context_bone_anim_mats]
2615 elif TX_CHAN == 'S':
2616 context_bone_anim_vecs = [mtx[0].to_scale() for mtx in context_bone_anim_mats]
2617 elif TX_CHAN == 'R':
2618 # Was....
2619 # elif TX_CHAN=='R': context_bone_anim_vecs = [mtx[1].to_euler() for mtx in context_bone_anim_mats]
2621 # ...but we need to use the previous euler for compatible conversion.
2622 context_bone_anim_vecs = []
2623 prev_eul = None
2624 for mtx in context_bone_anim_mats:
2625 if prev_eul:
2626 prev_eul = mtx[1].to_euler('XYZ', prev_eul)
2627 else:
2628 prev_eul = mtx[1].to_euler()
2629 context_bone_anim_vecs.append(tuple_rad_to_deg(prev_eul))
2631 fw('\n\t\t\t\tChannel: "%s" {' % TX_CHAN) # translation
2633 for i in range(3):
2634 # Loop on each axis of the bone
2635 fw('\n\t\t\t\t\tChannel: "%s" {' % ('XYZ'[i])) # translation
2636 fw('\n\t\t\t\t\t\tDefault: %.15f' % context_bone_anim_vecs[0][i])
2637 fw('\n\t\t\t\t\t\tKeyVer: 4005')
2639 if not use_anim_optimize:
2640 # Just write all frames, simple but in-eficient
2641 fw('\n\t\t\t\t\t\tKeyCount: %i' % (1 + act_end - act_start))
2642 fw('\n\t\t\t\t\t\tKey: ')
2643 frame = act_start
2644 while frame <= act_end:
2645 if frame != act_start:
2646 fw(',')
2648 # Curve types are 'C,n' for constant, 'L' for linear
2649 # C,n is for bezier? - linear is best for now so we can do simple keyframe removal
2650 fw('\n\t\t\t\t\t\t\t%i,%.15f,L' % (fbx_time(frame - 1), context_bone_anim_vecs[frame - act_start][i]))
2651 frame += 1
2652 else:
2653 # remove unneeded keys, j is the frame, needed when some frames are removed.
2654 context_bone_anim_keys = [(vec[i], j) for j, vec in enumerate(context_bone_anim_vecs)]
2656 # last frame to fisrt frame, missing 1 frame on either side.
2657 # removeing in a backwards loop is faster
2658 #for j in xrange( (act_end-act_start)-1, 0, -1 ):
2659 # j = (act_end-act_start)-1
2660 j = len(context_bone_anim_keys) - 2
2661 while j > 0 and len(context_bone_anim_keys) > 2:
2662 # print j, len(context_bone_anim_keys)
2663 # Is this key the same as the ones next to it?
2665 # co-linear horizontal...
2666 if abs(context_bone_anim_keys[j][0] - context_bone_anim_keys[j - 1][0]) < ANIM_OPTIMIZE_PRECISSION_FLOAT and \
2667 abs(context_bone_anim_keys[j][0] - context_bone_anim_keys[j + 1][0]) < ANIM_OPTIMIZE_PRECISSION_FLOAT:
2669 del context_bone_anim_keys[j]
2671 else:
2672 frame_range = float(context_bone_anim_keys[j + 1][1] - context_bone_anim_keys[j - 1][1])
2673 frame_range_fac1 = (context_bone_anim_keys[j + 1][1] - context_bone_anim_keys[j][1]) / frame_range
2674 frame_range_fac2 = 1.0 - frame_range_fac1
2676 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:
2677 del context_bone_anim_keys[j]
2678 else:
2679 j -= 1
2681 # keep the index below the list length
2682 if j > len(context_bone_anim_keys) - 2:
2683 j = len(context_bone_anim_keys) - 2
2685 if len(context_bone_anim_keys) == 2 and context_bone_anim_keys[0][0] == context_bone_anim_keys[1][0]:
2687 # This axis has no moton, its okay to skip KeyCount and Keys in this case
2688 # pass
2690 # better write one, otherwise we loose poses with no animation
2691 fw('\n\t\t\t\t\t\tKeyCount: 1')
2692 fw('\n\t\t\t\t\t\tKey: ')
2693 fw('\n\t\t\t\t\t\t\t%i,%.15f,L' % (fbx_time(start), context_bone_anim_keys[0][0]))
2694 else:
2695 # We only need to write these if there is at least one
2696 fw('\n\t\t\t\t\t\tKeyCount: %i' % len(context_bone_anim_keys))
2697 fw('\n\t\t\t\t\t\tKey: ')
2698 for val, frame in context_bone_anim_keys:
2699 if frame != context_bone_anim_keys[0][1]: # not the first
2700 fw(',')
2701 # frame is already one less then blenders frame
2702 fw('\n\t\t\t\t\t\t\t%i,%.15f,L' % (fbx_time(frame), val))
2704 if i == 0:
2705 fw('\n\t\t\t\t\t\tColor: 1,0,0')
2706 elif i == 1:
2707 fw('\n\t\t\t\t\t\tColor: 0,1,0')
2708 elif i == 2:
2709 fw('\n\t\t\t\t\t\tColor: 0,0,1')
2711 fw('\n\t\t\t\t\t}')
2712 fw('\n\t\t\t\t\tLayerType: %i' % (TX_LAYER + 1))
2713 fw('\n\t\t\t\t}')
2715 # ---------------
2717 fw('\n\t\t\t}')
2718 fw('\n\t\t}')
2720 # end the take
2721 fw('\n\t}')
2723 # end action loop. set original actions
2724 # do this after every loop in case actions effect eachother.
2725 for my_arm in ob_arms:
2726 if my_arm.blenObject.animation_data:
2727 my_arm.blenObject.animation_data.action = my_arm.blenAction
2729 fw('\n}')
2731 scene.frame_set(frame_orig)
2733 else:
2734 # no animation
2735 fw('\n;Takes and animation section')
2736 fw('\n;----------------------------------------------------')
2737 fw('\n')
2738 fw('\nTakes: {')
2739 fw('\n\tCurrent: ""')
2740 fw('\n}')
2742 # write meshes animation
2743 #for obname, ob, mtx, me, mats, arm, armname in ob_meshes:
2745 # Clear mesh data Only when writing with modifiers applied
2746 for me in meshes_to_clear:
2747 bpy.data.meshes.remove(me)
2749 # --------------------------- Footer
2750 if world:
2751 m = world.mist_settings
2752 has_mist = m.use_mist
2753 mist_intense = m.intensity
2754 mist_start = m.start
2755 mist_end = m.depth
2756 # mist_height = m.height # UNUSED
2757 world_hor = world.horizon_color
2758 else:
2759 has_mist = mist_intense = mist_start = mist_end = 0
2760 world_hor = 0, 0, 0
2762 fw('\n;Version 5 settings')
2763 fw('\n;------------------------------------------------------------------')
2764 fw('\n')
2765 fw('\nVersion5: {')
2766 fw('\n\tAmbientRenderSettings: {')
2767 fw('\n\t\tVersion: 101')
2768 fw('\n\t\tAmbientLightColor: %.1f,%.1f,%.1f,0' % tuple(world_amb))
2769 fw('\n\t}')
2770 fw('\n\tFogOptions: {')
2771 fw('\n\t\tFogEnable: %i' % has_mist)
2772 fw('\n\t\tFogMode: 0')
2773 fw('\n\t\tFogDensity: %.3f' % mist_intense)
2774 fw('\n\t\tFogStart: %.3f' % mist_start)
2775 fw('\n\t\tFogEnd: %.3f' % mist_end)
2776 fw('\n\t\tFogColor: %.1f,%.1f,%.1f,1' % tuple(world_hor))
2777 fw('\n\t}')
2778 fw('\n\tSettings: {')
2779 fw('\n\t\tFrameRate: "%i"' % int(fps))
2780 fw('\n\t\tTimeFormat: 1')
2781 fw('\n\t\tSnapOnFrames: 0')
2782 fw('\n\t\tReferenceTimeIndex: -1')
2783 fw('\n\t\tTimeLineStartTime: %i' % fbx_time(start - 1))
2784 fw('\n\t\tTimeLineStopTime: %i' % fbx_time(end - 1))
2785 fw('\n\t}')
2786 fw('\n\tRendererSetting: {')
2787 fw('\n\t\tDefaultCamera: "Producer Perspective"')
2788 fw('\n\t\tDefaultViewingMode: 0')
2789 fw('\n\t}')
2790 fw('\n}')
2791 fw('\n')
2793 # XXX, shouldnt be global!
2794 for mapping in (sane_name_mapping_ob,
2795 sane_name_mapping_ob_unique,
2796 sane_name_mapping_mat,
2797 sane_name_mapping_tex,
2798 sane_name_mapping_take,
2799 sane_name_mapping_group,
2801 mapping.clear()
2802 del mapping
2804 del ob_arms[:]
2805 del ob_bones[:]
2806 del ob_cameras[:]
2807 del ob_lights[:]
2808 del ob_meshes[:]
2809 del ob_null[:]
2811 file.close()
2813 # copy all collected files.
2814 bpy_extras.io_utils.path_reference_copy(copy_set)
2816 print('export finished in %.4f sec.' % (time.process_time() - start_time))
2817 return {'FINISHED'}
2820 # defaults for applications, currently only unity but could add others.
2821 def defaults_unity3d():
2822 return dict(global_matrix=Matrix.Rotation(-math.pi / 2.0, 4, 'X'),
2823 use_selection=False,
2824 object_types={'ARMATURE', 'EMPTY', 'MESH'},
2825 use_mesh_modifiers=True,
2826 use_armature_deform_only=True,
2827 use_anim=True,
2828 use_anim_optimize=False,
2829 use_anim_action_all=True,
2830 batch_mode='OFF',
2831 use_default_take=True,
2835 def save(operator, context,
2836 filepath="",
2837 use_selection=False,
2838 batch_mode='OFF',
2839 use_batch_own_dir=False,
2840 **kwargs
2843 if bpy.ops.object.mode_set.poll():
2844 bpy.ops.object.mode_set(mode='OBJECT')
2846 if batch_mode == 'OFF':
2847 kwargs_mod = kwargs.copy()
2848 if use_selection:
2849 kwargs_mod["context_objects"] = context.selected_objects
2850 else:
2851 kwargs_mod["context_objects"] = context.scene.objects
2853 return save_single(operator, context.scene, filepath, **kwargs_mod)
2854 else:
2855 fbxpath = filepath
2857 prefix = os.path.basename(fbxpath)
2858 if prefix:
2859 fbxpath = os.path.dirname(fbxpath)
2861 if not fbxpath.endswith(os.sep):
2862 fbxpath += os.sep
2864 if batch_mode == 'GROUP':
2865 data_seq = tuple(grp for grp in bpy.data.groups if grp.objects)
2866 else:
2867 data_seq = bpy.data.scenes
2869 # call this function within a loop with BATCH_ENABLE == False
2870 # no scene switching done at the moment.
2871 # orig_sce = context.scene
2873 new_fbxpath = fbxpath # own dir option modifies, we need to keep an original
2874 for data in data_seq: # scene or group
2875 newname = prefix + bpy.path.clean_name(data.name)
2877 if use_batch_own_dir:
2878 new_fbxpath = fbxpath + newname + os.sep
2879 # path may already exist
2880 # TODO - might exist but be a file. unlikely but should probably account for it.
2882 if not os.path.exists(new_fbxpath):
2883 os.makedirs(new_fbxpath)
2885 filepath = new_fbxpath + newname + '.fbx'
2887 print('\nBatch exporting %s as...\n\t%r' % (data, filepath))
2889 # XXX don't know what to do with this, probably do the same? (Arystan)
2890 if batch_mode == 'GROUP': # group
2891 # group, so objects update properly, add a dummy scene.
2892 scene = bpy.data.scenes.new(name="FBX_Temp")
2893 scene.layers = [True] * 20
2894 # bpy.data.scenes.active = scene # XXX, cant switch
2895 for ob_base in data.objects:
2896 scene.objects.link(ob_base)
2898 scene.update()
2899 else:
2900 scene = data
2902 # TODO - BUMMER! Armatures not in the group wont animate the mesh
2904 # else: # scene
2905 # data_seq.active = data
2907 # Call self with modified args
2908 # Dont pass batch options since we already usedt them
2909 kwargs_batch = kwargs.copy()
2911 kwargs_batch["context_objects"] = data.objects
2913 save_single(operator, scene, filepath, **kwargs_batch)
2915 if batch_mode == 'GROUP':
2916 # remove temp group scene
2917 bpy.data.scenes.remove(scene)
2919 # no active scene changing!
2920 # bpy.data.scenes.active = orig_sce
2922 return {'FINISHED'} # so the script wont run after we have batch exported.
2925 # NOTE TO Campbell -
2926 # Can any or all of the following notes be removed because some have been here for a long time? (JCB 27 July 2011)
2927 # NOTES (all line numbers correspond to original export_fbx.py (under release/scripts)
2928 # - get rid of bpy.path.clean_name somehow
2929 # + get rid of BPyObject_getObjectArmature, move it in RNA?
2930 # - implement all BPyMesh_* used here with RNA
2931 # - getDerivedObjects is not fully replicated with .dupli* funcs
2932 # - don't know what those colbits are, do we need them? they're said to be deprecated in DNA_object_types.h: 1886-1893
2933 # - no hq normals: 1900-1901