[Bf-extensions-cvs] [2b10261] master: FBX import: Cleanup.

Bastien Montagne noreply at git.blender.org
Tue Jan 6 21:20:30 CET 2015


Commit: 2b1026150c5943c02c80422f1f33976a80977c36
Author: Bastien Montagne
Date:   Tue Jan 6 21:02:14 2015 +0100
Branches: master
https://developer.blender.org/rBA2b1026150c5943c02c80422f1f33976a80977c36

FBX import: Cleanup.

Shorten some too long lines, factorize some imports on top of file (!!!),
also done a few optimizations in some places where code was rather stupid.

Nothing expected to change in behavior in this commit!

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

M	io_scene_fbx/import_fbx.py

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

diff --git a/io_scene_fbx/import_fbx.py b/io_scene_fbx/import_fbx.py
index 384f104..fa681f8 100644
--- a/io_scene_fbx/import_fbx.py
+++ b/io_scene_fbx/import_fbx.py
@@ -33,6 +33,7 @@ if "bpy" in locals():
         importlib.reload(fbx_utils)
 
 import bpy
+from mathutils import Matrix, Euler, Vector
 
 # -----
 # Utils
@@ -333,8 +334,6 @@ def blen_read_custom_properties(fbx_obj, blen_obj, settings):
 
 
 def blen_read_object_transform_do(transform_data):
-    from mathutils import Matrix, Euler
-
     # translation
     lcl_translation = Matrix.Translation(transform_data.loc)
 
@@ -470,10 +469,10 @@ def blen_read_animations_curves_iter(fbx_curves, blen_start_offset, fbx_start_of
 
 def blen_read_animations_action_item(action, item, cnodes, fps):
     """
-    'Bake' loc/rot/scale into the action, taking any pre_ and post_ matrix into account to transform from fbx into blender space.
+    'Bake' loc/rot/scale into the action,
+    taking any pre_ and post_ matrix into account to transform from fbx into blender space.
     """
     from bpy.types import Object, PoseBone, ShapeKey
-    from mathutils import Euler, Matrix
     from itertools import chain
 
     fbx_curves = []
@@ -931,7 +930,6 @@ def blen_read_geom_layer_normal(fbx_obj, mesh, xform=None):
 
 
 def blen_read_geom(fbx_tmpl, fbx_obj, settings):
-    from mathutils import Matrix, Vector
     from itertools import chain
     import array
 
@@ -1046,8 +1044,6 @@ def blen_read_geom(fbx_tmpl, fbx_obj, settings):
 
 
 def blen_read_shape(fbx_tmpl, fbx_sdata, fbx_bcdata, meshes, scene):
-    from mathutils import Vector
-
     elem_name_utf8 = elem_name_ensure_class(fbx_sdata, b'Geometry')
     indices = elem_prop_first(elem_find_first(fbx_sdata, b'Indexes'), default=())
     dvcos = tuple(co for co in zip(*[iter(elem_prop_first(elem_find_first(fbx_sdata, b'Vertices'), default=()))] * 3))
@@ -1259,8 +1255,8 @@ def blen_read_light(fbx_tmpl, fbx_obj, global_scale):
 # ### Import Utility class
 class FbxImportHelperNode:
     """
-    Temporary helper node to store a hierarchy of fbxNode objects before building
-    Objects, Armatures and Bones. It tries to keep the correction data in one place so it can be applied consistently to the imported data.
+    Temporary helper node to store a hierarchy of fbxNode objects before building Objects, Armatures and Bones.
+    It tries to keep the correction data in one place so it can be applied consistently to the imported data.
     """
 
     __slots__ = ('_parent', 'anim_compensation_matrix', 'armature_setup', 'bind_matrix', 'bl_bone', 'bl_data', 'bl_obj', 'bone_child_matrix',
@@ -1337,7 +1333,6 @@ class FbxImportHelperNode:
 
     def find_correction_matrix(self, settings, parent_correction_inv=None):
         from bpy_extras.io_utils import axis_conversion
-        from mathutils import Matrix, Vector
 
         if self.parent and (self.parent.is_root or self.parent.do_bake_transform(settings)):
             self.pre_matrix = settings.global_matrix
@@ -1350,7 +1345,7 @@ class FbxImportHelperNode:
         if self.is_bone:
             if settings.automatic_bone_orientation:
                 # find best orientation to align bone with
-                bone_children = [child for child in self.children if child.is_bone]
+                bone_children = tuple(child for child in self.children if child.is_bone)
                 if len(bone_children) == 0:
                     # no children, inherit the correction from parent (if possible)
                     if self.parent and self.parent.is_bone:
@@ -1368,7 +1363,8 @@ class FbxImportHelperNode:
                             best_axis = Vector((0, 1 if vec[1] >= 0 else -1, 0))
                     else:
                         # get the child directions once because they may be checked several times
-                        child_locs = [loc.normalized() for loc in [bind_matrix.to_translation() for bind_matrix in [child.bind_matrix for child in bone_children]] if loc.magnitude > 0.0]
+                        child_locs = (child.bind_matrix.to_translation() for child in bone_children)
+                        child_locs = tuple(loc.normalized() for loc in child_locs if loc.magnitude > 0.0)
 
                         # I'm not sure which one I like better...
                         if False:
@@ -1465,9 +1461,7 @@ class FbxImportHelperNode:
                 armature.parent = self
 
         for child in self.children:
-            if child.is_armature:
-                continue
-            if child.is_bone:
+            if child.is_armature or child.is_bone:
                 continue
             child.find_armatures()
 
@@ -1502,16 +1496,12 @@ class FbxImportHelperNode:
             child.find_fake_bones(in_armature)
 
     def get_world_matrix(self):
-        from mathutils import Matrix
-
         matrix = self.parent.get_world_matrix() if self.parent else Matrix()
         if self.matrix:
             matrix = matrix * self.matrix
         return matrix
 
     def get_matrix(self):
-        from mathutils import Matrix
-
         matrix = self.matrix if self.matrix else Matrix()
         if self.pre_matrix:
             matrix = self.pre_matrix * matrix
@@ -1520,8 +1510,6 @@ class FbxImportHelperNode:
         return matrix
 
     def get_bind_matrix(self):
-        from mathutils import Matrix
-
         matrix = self.bind_matrix if self.bind_matrix else Matrix()
         if self.pre_matrix:
             matrix = self.pre_matrix * matrix
@@ -1530,8 +1518,6 @@ class FbxImportHelperNode:
         return matrix
 
     def make_bind_pose_local(self, parent_matrix=None):
-        from mathutils import Matrix
-
         if parent_matrix is None:
             parent_matrix = Matrix()
 
@@ -1571,8 +1557,6 @@ class FbxImportHelperNode:
                 child.collect_armature_meshes()
 
     def build_skeleton(self, arm, parent_matrix, parent_bone_size=1):
-        from mathutils import Vector, Matrix
-
         # ----
         # Now, create the (edit)bone.
         bone = arm.bl_data.edit_bones.new(name=self.fbx_name)
@@ -1594,7 +1578,8 @@ class FbxImportHelperNode:
             bone_size = parent_bone_size
 
         # So that our bone gets its final length, but still Y-aligned in armature space.
-        # 0-length bones are automatically collapsed into their parent when you leave edit mode, so this enforces a minimum length
+        # 0-length bones are automatically collapsed into their parent when you leave edit mode,
+        # so this enforces a minimum length.
         bone_tail = Vector((0.0, 1.0, 0.0)) * max(0.01, bone_size)
         bone.tail = bone_tail
 
@@ -1603,7 +1588,8 @@ class FbxImportHelperNode:
 
         bone.matrix = bone_matrix
 
-        # correction for children attached to a bone. Fbx expects to attach to the head of a bone, while blender attaches to the tail.
+        # Correction for children attached to a bone. FBX expects to attach to the head of a bone,
+        # while Blender attaches to the tail.
         self.bone_child_matrix = Matrix.Translation(-bone_tail)
 
         for child in self.children:
@@ -1643,8 +1629,6 @@ class FbxImportHelperNode:
         return obj
 
     def build_skeleton_children(self, fbx_tmpl, settings, scene):
-        from mathutils import Matrix
-
         if self.is_bone:
             for child in self.children:
                 if child.ignore:
@@ -1656,7 +1640,8 @@ class FbxImportHelperNode:
                     child_obj.parent_type = 'BONE'
                     child_obj.matrix_parent_inverse = Matrix()
 
-                    # Blender attaches to the end of a bone, while FBX attaches to the start. bone_child_matrix corrects for that.
+                    # Blender attaches to the end of a bone, while FBX attaches to the start.
+                    # bone_child_matrix corrects for that.
                     if child.pre_matrix:
                         child.pre_matrix = self.bone_child_matrix * child.pre_matrix
                     else:
@@ -1703,13 +1688,13 @@ class FbxImportHelperNode:
                 w.append(weight)
 
     def set_bone_weights(self):
-        ignored_children = [child for child in self.children if child.is_bone and child.ignore and len(child.clusters) > 0]
+        ignored_children = tuple(child for child in self.children
+                                       if child.is_bone and child.ignore and len(child.clusters) > 0)
 
         if len(ignored_children) > 0:
             # If we have an ignored child bone we need to merge their weights into the current bone weights.
-            # (This can happen both intentionally and accidentally when skinning a model. Either way, they
-            # need to be moved into a parent bone or they cause animation glitches.)
-
+            # This can happen both intentionally and accidentally when skinning a model. Either way, they
+            # need to be moved into a parent bone or they cause animation glitches.
             for fbx_cluster, meshes in self.clusters:
                 combined_weights = {}
                 self.merge_weights(combined_weights, fbx_cluster)
@@ -1747,14 +1732,10 @@ class FbxImportHelperNode:
                 add_vgroup_to_objects(indices, weights, self.bl_bone, [node.bl_obj for node in meshes])
 
         for child in self.children:
-            if child.ignore:
-                continue
-            if child.is_bone:
+            if child.is_bone and not child.ignore:
                 child.set_bone_weights()
 
     def build_hierarchy(self, fbx_tmpl, settings, scene):
-        from mathutils import Matrix
-
         if self.is_armature:
             # create when linking since we need object data
             elem_name_utf8 = self.fbx_name
@@ -1854,7 +1835,7 @@ class FbxImportHelperNode:
             return obj
         else:
             for child in self.children:
-                child_obj = child.build_hierarchy(fbx_tmpl, settings, scene)
+                child.build_hierarchy(fbx_tmpl, settings, scene)
 
 
 def is_ascii(filepath, size):
@@ -1892,7 +1873,6 @@ def load(operator, context, filepath="",
     import os
     import time
     from bpy_extras.io_utils import axis_conversion
-    from mathutils import Matrix
 
     from . import parse_fbx
  

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list