[Bf-extensions-cvs] [a74d599] fbx_io_development: Fixed import un-skinned armatures.

Bastien Montagne noreply at git.blender.org
Sun Jun 29 11:03:59 CEST 2014


Commit: a74d5998c7d4e54cce9e10f2b4a530720326e651
Author: Bastien Montagne
Date:   Sun Jun 29 11:02:58 2014 +0200
https://developer.blender.org/rBAa74d5998c7d4e54cce9e10f2b4a530720326e651

Fixed import un-skinned armatures.

Note in this case, edit pose is same as current pose, since there is no way
to store 'edit' (bind) pose in FBX, but through Deformer nodes (i.e. skinning to a mesh).

===================================================================

M	io_scene_fbx/import_fbx.py

===================================================================

diff --git a/io_scene_fbx/import_fbx.py b/io_scene_fbx/import_fbx.py
index 3d64873..193f5df 100644
--- a/io_scene_fbx/import_fbx.py
+++ b/io_scene_fbx/import_fbx.py
@@ -148,10 +148,6 @@ def elem_props_find_first(elem, elem_prop_id):
         for e in elem:
             result = elem_props_find_first(e, elem_prop_id)
             if result is not None:
-                '''
-                if e is elem[1]:
-                    print("Using templ!!!", elem_prop_id)
-                '''
                 return result
         assert(len(elem) > 0)
         return None
@@ -175,7 +171,6 @@ def elem_props_get_color_rgb(elem, elem_prop_id, default=None):
         else:
             assert(elem_prop.props[1] == b'ColorRGB')
             assert(elem_prop.props[2] == b'Color')
-            #print(elem_prop.props_type[4:7])
         assert(elem_prop.props_type[4:7] == bytes((data_types.FLOAT64,)) * 3)
         return elem_prop.props[4:7]
     return default
@@ -423,7 +418,7 @@ def blen_read_object(fbx_tmpl, fbx_obj, object_data):
 # --------
 # Armature
 
-def blen_read_armatures_add_bone(bl_obj, bl_arm, bones, b_uuid, matrices):
+def blen_read_armatures_add_bone(bl_obj, bl_arm, bones, b_uuid, matrices, fbx_tmpl_model):
     from mathutils import Matrix, Vector
 
     b_item, bsize, p_uuid, clusters = bones[b_uuid]
@@ -434,23 +429,39 @@ def blen_read_armatures_add_bone(bl_obj, bl_arm, bones, b_uuid, matrices):
     p_ebo = None
     if p_uuid is not None:
         # Recurse over parents!
-        p_ebo = blen_read_armatures_add_bone(bl_obj, bl_arm, bones, p_uuid, matrices)
-
-    # Remember we assume (for now) that one bone only has one cluster, this will have to be checked ultimately.
-    if not clusters:
-        return None
-    fbx_cdata, meshes, objects = clusters[0]
-    objects = {blen_o for fbx_o, blen_o in objects}
-
-    # We assume matrices in cluster are rest pose of bones (they are in Global space!).
-    # TransformLink is matrix of bone, in global space.
-    # TransformAssociateModel is matrix of armature, in global space (at bind time).
-    elm = elem_find_first(fbx_cdata, b'Transform', default=None)
-    mmat_bone = array_to_matrix4(elm.props[0]) if elm is not None else None
-    elm = elem_find_first(fbx_cdata, b'TransformLink', default=None)
-    bmat_glob = array_to_matrix4(elm.props[0]) if elm is not None else Matrix()
-    elm = elem_find_first(fbx_cdata, b'TransformAssociateModel', default=None)
-    amat_glob = array_to_matrix4(elm.props[0]) if elm is not None else Matrix()
+        p_ebo = blen_read_armatures_add_bone(bl_obj, bl_arm, bones, p_uuid, matrices, fbx_tmpl_model)
+
+    if clusters:
+        # Remember we assume (for now) that one bone only has one cluster, this will have to be checked ultimately.
+        fbx_cdata, meshes, objects = clusters[0]
+        objects = {blen_o for fbx_o, blen_o in objects}
+
+        # We assume matrices in cluster are rest pose of bones (they are in Global space!).
+        # TransformLink is matrix of bone, in global space.
+        # TransformAssociateModel is matrix of armature, in global space (at bind time).
+        elm = elem_find_first(fbx_cdata, b'Transform', default=None)
+        mmat_bone = array_to_matrix4(elm.props[0]) if elm is not None else None
+        elm = elem_find_first(fbx_cdata, b'TransformLink', default=None)
+        bmat_glob = array_to_matrix4(elm.props[0]) if elm is not None else Matrix()
+        elm = elem_find_first(fbx_cdata, b'TransformAssociateModel', default=None)
+        amat_glob = array_to_matrix4(elm.props[0]) if elm is not None else Matrix()
+    else:
+        # Armature bound to no mesh...
+        fbx_cdata, meshes, objects = (None, (), ())
+        mmat_bone = None
+        amat_glob = bl_obj.matrix_world
+
+        fbx_props = (elem_find_first(fbx_bdata, b'Properties70'),
+                     elem_find_first(fbx_tmpl_model, b'Properties70', fbx_elem_nil))
+        assert(fbx_props[0] is not None)
+
+        transform_data = blen_read_object_transform_preprocess(fbx_props, fbx_bdata, Matrix())
+        bmat_glob = blen_read_object_transform_do(transform_data)
+        if p_ebo:
+            # Bring back matrix in armature space.
+            bmat_glob = p_ebo.matrix * bmat_glob
+        # And back into global space...
+        bmat_glob = amat_glob * bmat_glob
 
     # ----
     # Now, create the (edit)bone.
@@ -473,12 +484,13 @@ def blen_read_armatures_add_bone(bl_obj, bl_arm, bones, b_uuid, matrices):
         if similar_vectors(p_ebo.tail, ebo.head):
             ebo.use_connect = True
 
-    # ----
-    # Add a new vgroup to the meshes (their objects, actually!).
-    # Quite obviously, only one mesh is expected...
-    indices = elem_prop_first(elem_find_first(fbx_cdata, b'Indexes', default=None), default=())
-    weights = elem_prop_first(elem_find_first(fbx_cdata, b'Weights', default=None), default=())
-    add_vgroup_to_objects(indices, weights, bone_name, objects)
+    if fbx_cdata is not None:
+        # ----
+        # Add a new vgroup to the meshes (their objects, actually!).
+        # Quite obviously, only one mesh is expected...
+        indices = elem_prop_first(elem_find_first(fbx_cdata, b'Indexes', default=None), default=())
+        weights = elem_prop_first(elem_find_first(fbx_cdata, b'Weights', default=None), default=())
+        add_vgroup_to_objects(indices, weights, bone_name, objects)
 
     # ----
     # If we get a valid mesh matrix (in bone space), store armature and mesh global matrices, we need to set temporarily
@@ -523,7 +535,7 @@ def blen_read_armatures(fbx_tmpl, armatures, scene, global_matrix):
         bpy.ops.object.mode_set(mode='EDIT')
 
         for b_uuid in bones:
-            blen_read_armatures_add_bone(bl_adata, bl_arm, bones, b_uuid, matrices)
+            blen_read_armatures_add_bone(bl_adata, bl_arm, bones, b_uuid, matrices, fbx_tmpl)
 
         bpy.ops.object.mode_set(mode='OBJECT')
 
@@ -1459,7 +1471,6 @@ def load(operator, context, filepath="",
 
     def _():
         for fbx_link in fbx_connections.elems:
-            # print(fbx_link)
             c_type = fbx_link.props[0]
             if fbx_link.props_type[1:3] == b'LL':
                 c_src, c_dst = fbx_link.props[1:3]
@@ -1672,7 +1683,6 @@ def load(operator, context, filepath="",
                     ok = True
                     break
             if ok:
-                # print(fbx_lnk_type)
                 # create when linking since we need object data
                 obj = blen_read_object(fbx_tmpl, fbx_obj, fbx_lnk_item)
                 assert(fbx_item[1] is None)
@@ -2116,5 +2126,4 @@ def load(operator, context, filepath="",
                                 material.use_raytrace = False
     _(); del _
 
-    # print(list(sorted(locals().keys())))
     return {'FINISHED'}



More information about the Bf-extensions-cvs mailing list