~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to release/scripts/addons/io_scene_fbx/export_fbx.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import bpy
29
29
from mathutils import Vector, Matrix
30
30
 
31
 
 
32
31
# I guess FBX uses degrees instead of radians (Arystan).
33
32
# Call this function just before writing to FBX.
34
33
# 180 / math.pi == 57.295779513
158
157
 
159
158
# ob must be OB_MESH
160
159
def BPyMesh_meshWeight2List(ob, me):
161
 
    ''' Takes a mesh and return its group names and a list of lists, one list per vertex.
 
160
    """ Takes a mesh and return its group names and a list of lists, one list per vertex.
162
161
    aligning the each vert list with the group names, each list contains float value for the weight.
163
162
    These 2 lists can be modified and then used with list2MeshWeight to apply the changes.
164
 
    '''
 
163
    """
165
164
 
166
165
    # Clear the vert group.
167
166
    groupNames = [g.name for g in ob.vertex_groups]
421
420
    except:
422
421
        import traceback
423
422
        traceback.print_exc()
424
 
        operator.report({'ERROR'}, "Could'nt open file %r" % filepath)
 
423
        operator.report({'ERROR'}, "Couldn't open file %r" % filepath)
425
424
        return {'CANCELLED'}
426
425
 
427
426
    # convenience
464
463
 
465
464
    # --------------- funcs for exporting
466
465
    def object_tx(ob, loc, matrix, matrix_mod=None):
467
 
        '''
 
466
        """
468
467
        Matrix mod is so armature objects can modify their bone matrices
469
 
        '''
 
468
        """
470
469
        if isinstance(ob, bpy.types.Bone):
471
470
 
472
471
            # we know we have a matrix
524
523
        return loc, rot, scale, matrix, matrix_rot
525
524
 
526
525
    def write_object_tx(ob, loc, matrix, matrix_mod=None):
527
 
        '''
 
526
        """
528
527
        We have loc to set the location if non blender objects that have a location
529
528
 
530
529
        matrix_mod is only used for bones at the moment
531
 
        '''
 
530
        """
532
531
        loc, rot, scale, matrix, matrix_rot = object_tx(ob, loc, matrix, matrix_mod)
533
532
 
534
533
        fw('\n\t\t\tProperty: "Lcl Translation", "Lcl Translation", "A+",%.15f,%.15f,%.15f' % loc)
686
685
 
687
686
        #~ poseMatrix = write_object_props(my_bone.blenBone, None, None, my_bone.fbxArm.parRelMatrix())[3]
688
687
        poseMatrix = write_object_props(my_bone.blenBone, pose_bone=my_bone.getPoseBone())[3]  # dont apply bone matrices anymore
689
 
        pose_items.append((my_bone.fbxName, poseMatrix))
 
688
 
 
689
        # Use the same calculation as in write_sub_deformer_skin to compute the global
 
690
        # transform of the bone for the bind pose.
 
691
        global_matrix_bone = (my_bone.fbxArm.matrixWorld * my_bone.restMatrix) * mtx4_z90
 
692
        pose_items.append((my_bone.fbxName, global_matrix_bone))
690
693
 
691
694
        # fw('\n\t\t\tProperty: "Size", "double", "",%.6f' % ((my_bone.blenData.head['ARMATURESPACE'] - my_bone.blenData.tail['ARMATURESPACE']) * my_bone.fbxArm.parRelMatrix()).length)
692
695
        fw('\n\t\t\tProperty: "Size", "double", "",1')
846
849
        write_camera_dummy('Producer Left', (-4000, 0, 0), 1, 30000, 1, (0, 1, 0))
847
850
 
848
851
    def write_camera(my_cam):
849
 
        '''
 
852
        """
850
853
        Write a blender camera
851
 
        '''
 
854
        """
852
855
        render = scene.render
853
856
        width = render.resolution_x
854
857
        height = render.resolution_y
893
896
        fw('\n\t\t\tProperty: "AspectW", "double", "",%i' % width)
894
897
        fw('\n\t\t\tProperty: "AspectH", "double", "",%i' % height)
895
898
 
896
 
        '''Camera aspect ratio modes.
 
899
        """Camera aspect ratio modes.
897
900
            0 If the ratio mode is eWINDOW_SIZE, both width and height values aren't relevant.
898
901
            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.
899
902
            2 If the ratio mode is eFIXED_RESOLUTION, both width and height values are in pixels.
900
903
            3 If the ratio mode is eFIXED_WIDTH, the width value is in pixels and the height value is relative to the width value.
901
904
            4 If the ratio mode is eFIXED_HEIGHT, the height value is in pixels and the width value is relative to the height value.
902
905
 
903
 
        Definition at line 234 of file kfbxcamera.h. '''
 
906
        Definition at line 234 of file kfbxcamera.h. """
904
907
 
905
908
        fw('\n\t\t\tProperty: "PixelAspectRatio", "double", "",1'
906
909
           '\n\t\t\tProperty: "UseFrameColor", "bool", "",0'
1249
1252
        }''')
1250
1253
 
1251
1254
    def write_deformer_skin(obname):
1252
 
        '''
 
1255
        """
1253
1256
        Each mesh has its own deformer
1254
 
        '''
 
1257
        """
1255
1258
        fw('\n\tDeformer: "Deformer::Skin %s", "Skin" {' % obname)
1256
1259
        fw('''
1257
1260
                Version: 100
1265
1268
    # in the example was 'Bip01 L Thigh_2'
1266
1269
    def write_sub_deformer_skin(my_mesh, my_bone, weights):
1267
1270
 
1268
 
        '''
 
1271
        """
1269
1272
        Each subdeformer is specific to a mesh, but the bone it links to can be used by many sub-deformers
1270
1273
        So the SubDeformer needs the mesh-object name as a prefix to make it unique
1271
1274
 
1272
1275
        Its possible that there is no matching vgroup in this mesh, in that case no verts are in the subdeformer,
1273
1276
        a but silly but dosnt really matter
1274
 
        '''
 
1277
        """
1275
1278
        fw('\n\tDeformer: "SubDeformer::Cluster %s %s", "Cluster" {' % (my_mesh.fbxName, my_bone.fbxName))
1276
1279
 
1277
1280
        fw('''
1331
1334
                fw(',%.8f' % vg[1])
1332
1335
            i += 1
1333
1336
 
1334
 
        if my_mesh.fbxParent:
1335
 
            # TODO FIXME, this case is broken in some cases. skinned meshes just shouldnt have parents where possible!
1336
 
            m = (my_mesh.matrixWorld.inverted() * my_bone.fbxArm.matrixWorld.copy() * my_bone.restMatrix) * mtx4_z90
1337
 
        else:
1338
 
            # Yes! this is it...  - but dosnt work when the mesh is a.
1339
 
            m = (my_mesh.matrixWorld.inverted() * my_bone.fbxArm.matrixWorld.copy() * my_bone.restMatrix) * mtx4_z90
1340
 
 
1341
 
        #m = mtx4_z90 * my_bone.restMatrix
1342
 
        matstr = mat4x4str(m)
1343
 
        matstr_i = mat4x4str(m.inverted())
1344
 
 
1345
 
        fw('\n\t\tTransform: %s' % matstr_i)  # THIS IS __NOT__ THE GLOBAL MATRIX AS DOCUMENTED :/
1346
 
        fw('\n\t\tTransformLink: %s' % matstr)
 
1337
        # Set TransformLink to the global transform of the bone and Transform
 
1338
        # equal to the mesh's transform in bone space.
 
1339
        # 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/
 
1340
 
 
1341
        global_bone_matrix = (my_bone.fbxArm.matrixWorld * my_bone.restMatrix) * mtx4_z90
 
1342
        global_mesh_matrix = my_mesh.matrixWorld
 
1343
        transform_matrix = (global_bone_matrix.inverted() * global_mesh_matrix)
 
1344
 
 
1345
        global_bone_matrix_string = mat4x4str(global_bone_matrix )
 
1346
        transform_matrix_string = mat4x4str(transform_matrix )
 
1347
 
 
1348
        fw('\n\t\tTransform: %s' % transform_matrix_string)
 
1349
        fw('\n\t\tTransformLink: %s' % global_bone_matrix_string)
1347
1350
        fw('\n\t}')
1348
1351
 
1349
1352
    def write_mesh(my_mesh):
1367
1370
        me_faces = me.tessfaces[:]
1368
1371
 
1369
1372
        poseMatrix = write_object_props(my_mesh.blenObject, None, my_mesh.parRelMatrix())[3]
1370
 
        pose_items.append((my_mesh.fbxName, poseMatrix))
 
1373
 
 
1374
        # Calculate the global transform for the mesh in the bind pose the same way we do
 
1375
        # in write_sub_deformer_skin
 
1376
        globalMeshBindPose = my_mesh.matrixWorld * mtx4_z90
 
1377
        pose_items.append((my_mesh.fbxName, globalMeshBindPose))
1371
1378
 
1372
1379
        if do_shapekeys:
1373
1380
            for kb in my_mesh.blenObject.data.shape_keys.key_blocks[1:]:
2920
2927
        mapping.clear()
2921
2928
    del mapping
2922
2929
 
2923
 
    ob_arms[:] = []
2924
 
    ob_bones[:] = []
2925
 
    ob_cameras[:] = []
2926
 
    ob_lights[:] = []
2927
 
    ob_meshes[:] = []
2928
 
    ob_null[:] = []
 
2930
    del ob_arms[:]
 
2931
    del ob_bones[:]
 
2932
    del ob_cameras[:]
 
2933
    del ob_lights[:]
 
2934
    del ob_meshes[:]
 
2935
    del ob_null[:]
2929
2936
 
2930
2937
    file.close()
2931
2938