[Bf-extensions-cvs] [57a8294] temp-x3d_import-T44758: Cleanup

Seva Alekseyev noreply at git.blender.org
Sat May 23 06:00:04 CEST 2015


Commit: 57a82943aa02b9048017adeda4695d073a6c99e0
Author: Seva Alekseyev
Date:   Sat May 23 13:56:55 2015 +1000
Branches: temp-x3d_import-T44758
https://developer.blender.org/rBA57a82943aa02b9048017adeda4695d073a6c99e0

Cleanup

- PEP8 compliance in my code, legacy code left intact
- Fixed the straight spine extrusion bug (exception in the airplane model)
- Texture/material caching

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

M	io_scene_x3d/import_x3d.py

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

diff --git a/io_scene_x3d/import_x3d.py b/io_scene_x3d/import_x3d.py
index 7fbe381..e183599 100644
--- a/io_scene_x3d/import_x3d.py
+++ b/io_scene_x3d/import_x3d.py
@@ -26,7 +26,11 @@ import shlex
 import math
 from math import sin, cos, pi
 
-EPSILON = 0.0000001 # Very crude.
+texture_cache = {}
+material_cache = {}
+
+EPSILON = 0.0000001  # Very crude.
+
 
 def imageConvertCompat(path):
 
@@ -1566,67 +1570,102 @@ def getFinalMatrix(node, mtx, ancestry, global_matrix):
 
     return mtx
 
+
 # -----------------------------------------------------------------------------------
 # Mesh import utilities
 
 # Assumes that the mesh has tessfaces - doesn't support polygons.
 # Also assumes that tessfaces are all triangles.
-# Assumes that the sequence of the mesh vertices array matches the source file.
-# For indexed meshes, that's almost a given; for nonindexed ones, this is a consideration.
+# Assumes that the sequence of the mesh vertices array matches
+# the source file. For indexed meshes, that's almost a given;
+# for nonindexed ones, this is a consideration.
+
+
 def importMesh_ApplyColors(bpymesh, geom, ancestry):
     colors = geom.getChildBySpec(['ColorRGBA', 'Color'])
     if colors:
-        if colors.getSpec() == 'ColorRGBA': # Array of arrays; no need to flatten
-            rgb = [c[:3] for c in colors.getFieldAsArray('color', 4, ancestry)]
+        if colors.getSpec() == 'ColorRGBA':
+            # Array of arrays; no need to flatten
+            rgb = [c[:3] for c
+                   in colors.getFieldAsArray('color', 4, ancestry)]
         else:
             rgb = colors.getFieldAsArray('color', 3, ancestry)
         tc = bpymesh.tessface_vertex_colors.new()
-        tc.data.foreach_set("color1", [i for face in bpymesh.tessfaces for i in rgb[face.vertices[0]]])
-        tc.data.foreach_set("color2", [i for face in bpymesh.tessfaces for i in rgb[face.vertices[1]]])
-        tc.data.foreach_set("color3", [i for face in bpymesh.tessfaces for i in rgb[face.vertices[2]]])
-
-# Assumes that the vertices have not been rearranged compared to the source file order
-# or in the order assumed by the spec (e. g. in Elevation, in rows by x).
+        tc.data.foreach_set("color1", [i for face
+                                       in bpymesh.tessfaces
+                                       for i in rgb[face.vertices[0]]])
+        tc.data.foreach_set("color2", [i for face
+                                       in bpymesh.tessfaces
+                                       for i in rgb[face.vertices[1]]])
+        tc.data.foreach_set("color3", [i for face
+                                       in bpymesh.tessfaces
+                                       for i in rgb[face.vertices[2]]])
+
+
+# Assumes that the vertices have not been rearranged compared to the
+# source file order # or in the order assumed by the spec (e. g. in
+# Elevation, in rows by x).
 # Assumes tessfaces have been set, doesn't support polygons.
 def importMesh_ApplyNormals(bpymesh, geom, ancestry):
     normals = geom.getChildBySpec('Normal')
-    if normals:
-        normalPerVertex = geom.getFieldAsBool('normalPerVertex', True, ancestry)
-        vectors = normals.getFieldAsArray('vector', 0, ancestry)
-        if normalPerVertex:
-            bpymesh.vertices.foreach_set("normal", vectors)
-        else:
-            bpymesh.tessfaces.foreach_set("normal", vectors)
+    if not normals:
+        return
+
+    per_vertex = geom.getFieldAsBool('normalPerVertex', True, ancestry)
+    vectors = normals.getFieldAsArray('vector', 0, ancestry)
+    if per_vertex:
+        bpymesh.vertices.foreach_set("normal", vectors)
+    else:
+        bpymesh.tessfaces.foreach_set("normal", vectors)
+
 
 # Reads the standard Coordinate object - common for all mesh elements
 # Feeds the vertices in the mesh.
-# Rearranging the vertex order is a bad idea - other elements in X3D might rely on it,
-# if you need to rearrange, please play with vertex indices in the tessfaces/polygons instead.
+# Rearranging the vertex order is a bad idea - other elements
+# in X3D might rely on it,  if you need to rearrange, please play with
+# vertex indices in the tessfaces/polygons instead.
+#
+# Vertex culling that we have in IndexedFaceSet is an unfortunate exception,
+# brought forth by a very specific issue.
 def importMesh_ReadVertices(bpymesh, geom, ancestry):
-    # We want points here as a flat array, but the caching logic in IndexedFaceSet presumes a 2D one.
+    # We want points here as a flat array, but the caching logic in
+    # IndexedFaceSet presumes a 2D one.
     # The case for caching is stronger over there.
-
-    points = geom.getChildBySpec('Coordinate').getFieldAsArray('point', 0, ancestry)
-    bpymesh.vertices.add(len(points)//3)
+    coord = geom.getChildBySpec('Coordinate')
+    points = coord.getFieldAsArray('point', 0, ancestry)
+    bpymesh.vertices.add(len(points) // 3)
     bpymesh.vertices.foreach_set("co", points)
 
+
 # Assumes the mesh only contains triangular tessfaces, and the order
-# of vertices matches the source.
-# Relies upon texture coordinates in the node; if a coordinate generation
-# algorithm for a geometry is in the spec (e. g. for ElevationGrid), it needs to be elsewhere.
-# Texture transform is applied elsewhere.
+# of vertices matches the source file.
+# Relies upon texture coordinates in the X3D node; if a coordinate generation
+# algorithm for a geometry is in the spec (e. g. for ElevationGrid), it needs
+# to be implemeted by the geometry handler.
+#
+# Texture transform is applied in ProcessObject.
 def importMesh_ApplyTextureToTessfaces(bpymesh, geom, ancestry, bpyima):
-    if bpyima:
-        tex_coord = geom.getChildBySpec('TextureCoordinate')
-        if tex_coord:
-            coord_points = tex_coord.getFieldAsArray('point', 2, ancestry)
-            if coord_points:
-                d = bpymesh.tessface_uv_textures.new().data
-                for face in d: # No foreach_set for nonscalars
-                    face.image = bpyima
-                d.foreach_set('uv', [i for face in bpymesh.tessfaces for vno in range(3) for i in coord_points[face.vertices[vno]]])
+    if not bpyima:
+        return
+
+    tex_coord = geom.getChildBySpec('TextureCoordinate')
+    if not tex_coord:
+        return
+
+    coord_points = tex_coord.getFieldAsArray('point', 2, ancestry)
+    if not coord_points:
+        return
+
+    d = bpymesh.tessface_uv_textures.new().data
+    for face in d:  # No foreach_set for nonscalars
+        face.image = bpyima
+    uv = [i for face in bpymesh.tessfaces
+          for vno in range(3) for i in coord_points[face.vertices[vno]]]
+    d.foreach_set('uv', uv)
+
 
-# Common steps for all triangle meshes once the geometry has been set: normals, vertex colors, and texture.
+# Common steps for all triangle meshes once the geometry has been set:
+# normals, vertex colors, and texture.
 def importMesh_FinalizeTriangleMesh(bpymesh, geom, ancestry, bpyima):
     bpymesh.validate()
     importMesh_ApplyNormals(bpymesh, geom, ancestry)
@@ -1635,11 +1674,13 @@ def importMesh_FinalizeTriangleMesh(bpymesh, geom, ancestry, bpyima):
     bpymesh.update()
     return bpymesh
 
-# Assumes that the mesh is stored as polygons and loops, and the premade array of
-# texture coordinates follows the loop array.
+
+# Assumes that the mesh is stored as polygons and loops, and the premade array
+# of texture coordinates follows the loop array.
 # The loops array must be flat.
 def importMesh_ApplyTextureToLoops(bpymesh, bpyima, loops):
-    for f in bpymesh.uv_textures.new().data:
+    d = bpymesh.uv_textures.new().data
+    for f in d:
         f.image = bpyima
     bpymesh.uv_layers[0].data.foreach_set('uv', loops)
 
@@ -1661,13 +1702,15 @@ def importMesh_IndexedTriangleSet(geom, ancestry, bpyima):
 
     # Read the faces
     index = geom.getFieldAsArray('index', 0, ancestry)
+    n = len(index) // 3
     if not ccw:
-        index = [index[3*i+j] for i in range(n//3) for j in (1, 0, 2)]
-    bpymesh.tessfaces.add(len(index)//3)
+        index = [index[3 * i + j] for i in range(n) for j in (1, 0, 2)]
+    bpymesh.tessfaces.add(n)
     bpymesh.tessfaces.foreach_set("vertices", index)
 
     return importMesh_FinalizeTriangleMesh(bpymesh, geom, ancestry, bpyima)
 
+
 def importMesh_IndexedTriangleStripSet(geom, ancestry, bpyima):
     # Ignoring solid
     # colorPerVertex is always true
@@ -1680,24 +1723,26 @@ def importMesh_IndexedTriangleStripSet(geom, ancestry, bpyima):
     while index[-1] == -1:
         del index[-1]
     ngaps = sum(1 for i in index if i == -1)
-    bpymesh.tessfaces.add(len(index) - 2 - 3*ngaps)
+    bpymesh.tessfaces.add(len(index) - 2 - 3 * ngaps)
+
     def triangles():
         i = 0
         odd = cw
         while True:
-            yield index[i+odd]
-            yield index[i+1-odd]
-            yield index[i+2]
-            odd = 1-odd
+            yield index[i + odd]
+            yield index[i + 1 - odd]
+            yield index[i + 2]
+            odd = 1 - odd
             i += 1
             if i + 2 >= len(index):
                 return
-            if index[i+2] == -1:
+            if index[i + 2] == -1:
                 i += 3
                 odd = cw
     bpymesh.tessfaces.foreach_set("vertices", [f for f in triangles()])
     return importMesh_FinalizeTriangleMesh(bpymesh, geom, ancestry, bpyima)
 
+
 def importMesh_IndexedTriangleFanSet(geom, ancestry, bpyima):
     # Ignoring solid
     # colorPerVertex is always true
@@ -1710,14 +1755,15 @@ def importMesh_IndexedTriangleFanSet(geom, ancestry, bpyima):
     while index[-1] == -1:
         del index[-1]
     ngaps = sum(1 for i in index if i == -1)
-    bpymesh.tessfaces.add(len(index) - 2 - 3*ngaps)
+    bpymesh.tessfaces.add(len(index) - 2 - 3 * ngaps)
+
     def triangles():
         i = 0
         j = 1
         while True:
             yield index[i]
-            yield index[i+j+cw]
-            yield index[i+j+1-cw]
+            yield index[i + j + cw]
+            yield index[i + j + 1 - cw]
             j += 1
             if i + j + 1 >= len(index):
                 return
@@ -1727,6 +1773,7 @@ def importMesh_IndexedTriangleFanSet(geom, ancestry, bp

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list