From a2f135e7d725d3a9232dfef1ee19ecae2efb9e31 Mon Sep 17 00:00:00 2001
From: Campbell Barton <ideasman42@gmail.com>
Date: Fri, 11 Feb 2011 12:09:15 +0000
Subject: [PATCH] use mesh.validate() to ensure all meshes of imported data is
 ok and wont crash blender.

---
 io_mesh_ply/import_ply.py    | 1 +
 io_mesh_raw/import_raw.py    | 2 ++
 io_mesh_stl/blender_utils.py | 1 +
 io_scene_3ds/import_3ds.py   | 4 +++-
 io_scene_obj/import_obj.py   | 2 +-
 io_scene_x3d/import_x3d.py   | 4 ++--
 6 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/io_mesh_ply/import_ply.py b/io_mesh_ply/import_ply.py
index 1dac3a8d1..8e5db68ec 100644
--- a/io_mesh_ply/import_ply.py
+++ b/io_mesh_ply/import_ply.py
@@ -307,6 +307,7 @@ def load_ply(filepath):
                     for j, col in enumerate(f_col):
                         col.r, col.g, col.b = ply_col[j]
 
+    mesh.validate()
     mesh.update()
 
     scn = bpy.context.scene
diff --git a/io_mesh_raw/import_raw.py b/io_mesh_raw/import_raw.py
index b8e96209c..4b5b7304c 100644
--- a/io_mesh_raw/import_raw.py
+++ b/io_mesh_raw/import_raw.py
@@ -107,6 +107,8 @@ def addMeshObj(mesh, objName):
         o.select = False
 
     mesh.update()
+    mesh.validate()
+
     nobj = bpy.data.objects.new(objName, mesh)
     scn.objects.link(nobj)
     nobj.select = True
diff --git a/io_mesh_stl/blender_utils.py b/io_mesh_stl/blender_utils.py
index 3d0ef076b..f763929a3 100644
--- a/io_mesh_stl/blender_utils.py
+++ b/io_mesh_stl/blender_utils.py
@@ -34,6 +34,7 @@ def create_and_link_mesh(name, faces, points):
     bpy.context.scene.objects.link(ob)
 
     # update mesh to allow proper display
+    mesh.validate()
     mesh.update()
 
 
diff --git a/io_scene_3ds/import_3ds.py b/io_scene_3ds/import_3ds.py
index e53222bd5..0a86cdc10 100644
--- a/io_scene_3ds/import_3ds.py
+++ b/io_scene_3ds/import_3ds.py
@@ -324,6 +324,9 @@ def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
                     uf.uv3 = contextMeshUV[v3 * 2:(v3 * 2) + 2]
                     # always a tri
 
+        bmesh.validate()
+        bmesh.update()
+
         ob = bpy.data.objects.new(contextObName, bmesh)
         object_dictionary[contextObName] = ob
         SCN.objects.link(ob)
@@ -338,7 +341,6 @@ def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
             object_matrix[ob] = contextMatrix_rot.copy()
 
         importedObjects.append(ob)
-        bmesh.update()
 
     #a spare chunk
     new_chunk = chunk()
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index 37c8df3d8..1056fde00 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -842,8 +842,8 @@ def create_mesh(new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc, v
 #                 me_edges[ed].flag |= SHARP
 #         del SHARP
 
+    me.validate()
     me.update(calc_edges=CREATE_EDGES)
-#     me.calcNormals()
 
     ob = bpy.data.objects.new("Mesh", me)
     new_objects.append(ob)
diff --git a/io_scene_x3d/import_x3d.py b/io_scene_x3d/import_x3d.py
index b4b9fe09f..e9393234e 100644
--- a/io_scene_x3d/import_x3d.py
+++ b/io_scene_x3d/import_x3d.py
@@ -1699,7 +1699,7 @@ def importMesh_IndexedFaceSet(geom, bpyima, ancestry):
         #for f in faces:
         #   bpymesh.faces.extend(faces, smooth=True)
 
-    # bpymesh.calcNormals()
+    bpymesh.validate()
     bpymesh.update()
 
     if len(bpymesh.faces) != len(faces):
@@ -1916,7 +1916,7 @@ def importMesh_PointSet(geom, ancestry):
     bpymesh.vertices.add(len(points))
     bpymesh.vertices.foreach_set("co", [a for v in points for a in v])
 
-    # bpymesh.calcNormals()  # will just be dummy normals
+    # No need to validate
     bpymesh.update()
     return bpymesh
 
-- 
GitLab