[Bf-extensions-cvs] [84a3e8f] fbx_io_development: Some cleanup...

Bastien Montagne noreply at git.blender.org
Sun Jul 6 19:16:01 CEST 2014


Commit: 84a3e8f05dcd09e9cb6e86e537b91995fe15ec7e
Author: Bastien Montagne
Date:   Sun Jul 6 16:00:00 2014 +0200
https://developer.blender.org/rBA84a3e8f05dcd09e9cb6e86e537b91995fe15ec7e

Some cleanup...

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

M	io_scene_fbx/export_fbx_bin.py
M	io_scene_fbx/fbx_utils.py
M	io_scene_fbx/import_fbx.py

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

diff --git a/io_scene_fbx/export_fbx_bin.py b/io_scene_fbx/export_fbx_bin.py
index 32ba238..c79e588 100644
--- a/io_scene_fbx/export_fbx_bin.py
+++ b/io_scene_fbx/export_fbx_bin.py
@@ -61,7 +61,7 @@ from .fbx_utils import (
     FBX_LIGHT_TYPES, FBX_LIGHT_DECAY_TYPES,
     RIGHT_HAND_AXES, FBX_FRAMERATES,
     # Miscellaneous utils.
-    units_convert, units_convert_iter, matrix_to_array, similar_values, similar_values_iter,
+    units_convert, units_convert_iter, matrix4_to_array, similar_values, similar_values_iter,
     # UUID from key.
     get_fbx_uuid_from_key,
     # Key generators.
@@ -699,7 +699,7 @@ def fbx_data_bindpose_element(root, me_obj, me, scene_data, arm_obj=None, bones=
     mat_world_obj = me_obj.fbx_object_matrix(scene_data, global_space=True)
     fbx_posenode = elem_empty(fbx_pose, b"PoseNode")
     elem_data_single_int64(fbx_posenode, b"Node", me_obj.fbx_uuid)
-    elem_data_single_float64_array(fbx_posenode, b"Matrix", matrix_to_array(mat_world_obj))
+    elem_data_single_float64_array(fbx_posenode, b"Matrix", matrix4_to_array(mat_world_obj))
     # And all bones of armature!
     mat_world_bones = {}
     for bo_obj in bones:
@@ -707,7 +707,7 @@ def fbx_data_bindpose_element(root, me_obj, me, scene_data, arm_obj=None, bones=
         mat_world_bones[bo_obj] = bomat
         fbx_posenode = elem_empty(fbx_pose, b"PoseNode")
         elem_data_single_int64(fbx_posenode, b"Node", bo_obj.fbx_uuid)
-        elem_data_single_float64_array(fbx_posenode, b"Matrix", matrix_to_array(bomat))
+        elem_data_single_float64_array(fbx_posenode, b"Matrix", matrix4_to_array(bomat))
 
     return mat_world_obj, mat_world_bones
 
@@ -1375,32 +1375,6 @@ def fbx_data_armature_elements(root, arm_obj, scene_data):
     if deformer is not None:
         for me, (skin_key, ob_obj, clusters) in deformer.items():
             # BindPose.
-            """
-            # We assume bind pose for our bones are their "Editmode" pose...
-            # All matrices are expected in global (world) space.
-            bindpose_key = get_blender_armature_bindpose_key(arm_obj.bdata, me)
-            fbx_pose = elem_data_single_int64(root, b"Pose", get_fbx_uuid_from_key(bindpose_key))
-            fbx_pose.add_string(fbx_name_class(me.name.encode(), b"Pose"))
-            fbx_pose.add_string(b"BindPose")
-
-            elem_data_single_string(fbx_pose, b"Type", b"BindPose")
-            elem_data_single_int32(fbx_pose, b"Version", FBX_POSE_BIND_VERSION)
-            elem_data_single_int32(fbx_pose, b"NbPoseNodes", 1 + len(bones))
-
-            # First node is mesh/object.
-            mat_world_obj = ob_obj.fbx_object_matrix(scene_data, global_space=True)
-            fbx_posenode = elem_empty(fbx_pose, b"PoseNode")
-            elem_data_single_int64(fbx_posenode, b"Node", ob_obj.fbx_uuid)
-            elem_data_single_float64_array(fbx_posenode, b"Matrix", matrix_to_array(mat_world_obj))
-            # And all bones of armature!
-            mat_world_bones = {}
-            for bo_obj in bones:
-                bomat = bo_obj.fbx_object_matrix(scene_data, rest=True, global_space=True)
-                mat_world_bones[bo_obj] = bomat
-                fbx_posenode = elem_empty(fbx_pose, b"PoseNode")
-                elem_data_single_int64(fbx_posenode, b"Node", bo_obj.fbx_uuid)
-                elem_data_single_float64_array(fbx_posenode, b"Matrix", matrix_to_array(bomat))
-            """
 
             mat_world_obj, mat_world_bones = fbx_data_bindpose_element(root, ob_obj, me, scene_data, arm_obj, bones)
 
@@ -1451,9 +1425,9 @@ def fbx_data_armature_elements(root, arm_obj, scene_data):
                 #          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/
                 elem_data_single_float64_array(fbx_clstr, b"Transform",
-                                               matrix_to_array(mat_world_bones[bo_obj].inverted() * mat_world_obj))
-                elem_data_single_float64_array(fbx_clstr, b"TransformLink", matrix_to_array(mat_world_bones[bo_obj]))
-                elem_data_single_float64_array(fbx_clstr, b"TransformAssociateModel", matrix_to_array(mat_world_arm))
+                                               matrix4_to_array(mat_world_bones[bo_obj].inverted() * mat_world_obj))
+                elem_data_single_float64_array(fbx_clstr, b"TransformLink", matrix4_to_array(mat_world_bones[bo_obj]))
+                elem_data_single_float64_array(fbx_clstr, b"TransformAssociateModel", matrix4_to_array(mat_world_arm))
 
 
 def fbx_data_object_elements(root, ob_obj, scene_data):
diff --git a/io_scene_fbx/fbx_utils.py b/io_scene_fbx/fbx_utils.py
index 70606b8..cf1349e 100644
--- a/io_scene_fbx/fbx_utils.py
+++ b/io_scene_fbx/fbx_utils.py
@@ -175,12 +175,18 @@ def units_convert_iter(it, u_from, u_to):
     return (v * conv for v in it)
 
 
-def matrix_to_array(mat):
+def matrix4_to_array(mat):
     """Concatenate matrix's columns into a single, flat tuple"""
     # blender matrix is row major, fbx is col major so transpose on write
     return tuple(f for v in mat.transposed() for f in v)
 
 
+def array_to_matrix4(arr):
+    """Convert a single 16-len tuple into a valid 4D Blender matrix"""
+    # Blender matrix is row major, fbx is col major so transpose on read
+    return Matrix(tuple(zip(*[iter(arr)]*4))).transposed()
+
+
 def similar_values(v1, v2, e=1e-6):
     """Return True if v1 and v2 are nearly the same."""
     if v1 == v2:
diff --git a/io_scene_fbx/import_fbx.py b/io_scene_fbx/import_fbx.py
index 40515fd..866c3f1 100644
--- a/io_scene_fbx/import_fbx.py
+++ b/io_scene_fbx/import_fbx.py
@@ -34,43 +34,20 @@ import bpy
 
 # -----
 # Utils
-from . import parse_fbx
+from . import parse_fbx, fbx_utils
 
 from .parse_fbx import data_types, FBXElem
+from .fbx_utils import (
+    units_convert_iter,
+    array_to_matrix4,
+    similar_values,
+    similar_values_iter,
+)
 
 # global singleton, assign on execution
 fbx_elem_nil = None
 
 
-def tuple_deg_to_rad(eul):
-    return (eul[0] / 57.295779513,
-            eul[1] / 57.295779513,
-            eul[2] / 57.295779513)
-
-
-def array_to_matrix4(arr):
-    """Convert a single 16-len tuple into a valid 4D Blender matrix"""
-    from mathutils import Matrix
-
-    # Blender matrix is row major, fbx is col major so transpose on read
-    return Matrix(tuple(zip(*[iter(arr)]*4))).transposed()
-
-
-def similar_values(v1, v2, e=1e-6):
-    """Return True if v1 and v2 are nearly the same."""
-    if v1 == v2:
-        return True
-    return ((abs(v1 - v2) / max(abs(v1), abs(v2))) <= e)
-
-
-def similar_vectors(vec1, vec2, e=1e-6):
-    """Return True if vec1 and vec2 are nearly the same."""
-    for v1, v2 in zip(vec1, vec2):
-        if not similar_values(v1, v2, e):
-            return False
-    return True
-
-
 def elem_find_first(elem, id_search, default=None):
     for fbx_item in elem.elems:
         if fbx_item.id == id_search:
@@ -298,10 +275,10 @@ def blen_read_object_transform_do(transform_data):
     lcl_translation = Matrix.Translation(transform_data.loc)
 
     # rotation
-    lcl_rot = Euler(tuple_deg_to_rad(transform_data.rot), transform_data.rot_ord).to_matrix().to_4x4()
-    lcl_rot = lcl_rot * transform_data.rot_alt_mat
-    pre_rot = Euler(tuple_deg_to_rad(transform_data.pre_rot), transform_data.rot_ord).to_matrix().to_4x4()
-    pst_rot = Euler(tuple_deg_to_rad(transform_data.pst_rot), transform_data.rot_ord).to_matrix().to_4x4()
+    to_rot = lambda rot, rot_ord: Euler(units_convert_iter(rot, "degree", "radian"), rot_ord).to_matrix().to_4x4()
+    lcl_rot = to_rot(transform_data.rot, transform_data.rot_ord) * transform_data.rot_alt_mat
+    pre_rot = to_rot(transform_data.pre_rot, transform_data.rot_ord)
+    pst_rot = to_rot(transform_data.pst_rot, transform_data.rot_ord)
 
     rot_ofs = Matrix.Translation(transform_data.rot_ofs)
     rot_piv = Matrix.Translation(transform_data.rot_piv)
@@ -488,7 +465,7 @@ def blen_read_armatures_add_bone(bl_obj, bl_arm, bones, b_uuid, matrices, fbx_tm
     # Connection to parent.
     if p_ebo is not None:
         ebo.parent = p_ebo
-        if similar_vectors(p_ebo.tail, ebo.head):
+        if similar_values_iter(p_ebo.tail, ebo.head):
             ebo.use_connect = True
 
     if fbx_cdata is not None:



More information about the Bf-extensions-cvs mailing list