[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4252] trunk/py/scripts/addons/ io_scene_obj/export_obj.py: OBJ now exports blenders ngons.

Campbell Barton ideasman42 at gmail.com
Sun Feb 10 13:55:11 CET 2013


Revision: 4252
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4252
Author:   campbellbarton
Date:     2013-02-10 12:55:10 +0000 (Sun, 10 Feb 2013)
Log Message:
-----------
OBJ now exports blenders ngons.

Modified Paths:
--------------
    trunk/py/scripts/addons/io_scene_obj/export_obj.py

Modified: trunk/py/scripts/addons/io_scene_obj/export_obj.py
===================================================================
--- trunk/py/scripts/addons/io_scene_obj/export_obj.py	2013-02-10 05:22:01 UTC (rev 4251)
+++ trunk/py/scripts/addons/io_scene_obj/export_obj.py	2013-02-10 12:55:10 UTC (rev 4252)
@@ -33,6 +33,18 @@
         return name.replace(' ', '_')
 
 
+def mesh_triangulate(me):
+    pass
+    '''
+    import bmesh
+    bm = bmesh.new()
+    bm.from_mesh(me)
+    bmesh.ops.triangulate(bm, faces=bm.faces)
+    bm.to_mesh(me)
+    bm.free()
+    '''
+
+
 def write_mtl(scene, filepath, path_mode, copy_set, mtl_dict):
     from mathutils import Color
 
@@ -362,14 +374,15 @@
             if EXPORT_UV:
                 faceuv = len(me.uv_textures) > 0
                 if faceuv:
-                    uv_layer = me.tessface_uv_textures.active.data[:]
+                    uv_texture = me.uv_textures.active.data[:]
+                    uv_layer = me.uv_layers.active.data[:]
             else:
                 faceuv = False
 
             me_verts = me.vertices[:]
 
             # Make our own list so it can be sorted to reduce context switching
-            face_index_pairs = [(face, index) for index, face in enumerate(me.tessfaces)]
+            face_index_pairs = [(face, index) for index, face in enumerate(me.polygons)]
             # faces = [ f for f in me.tessfaces ]
 
             if EXPORT_EDGES:
@@ -384,6 +397,9 @@
 
                 continue  # dont bother with this mesh.
 
+            if EXPORT_TRI:
+                mesh_triangulate(me)
+
             if EXPORT_NORMALS and face_index_pairs:
                 me.calc_normals()
 
@@ -400,7 +416,7 @@
             if EXPORT_KEEP_VERT_ORDER:
                 pass
             elif faceuv:
-                face_index_pairs.sort(key=lambda a: (a[0].material_index, hash(uv_layer[a[1]].image), a[0].use_smooth))
+                face_index_pairs.sort(key=lambda a: (a[0].material_index, hash(uv_texture[a[1]].image), a[0].use_smooth))
             elif len(materials) > 1:
                 face_index_pairs.sort(key=lambda a: (a[0].material_index, a[0].use_smooth))
             else:
@@ -433,12 +449,13 @@
                 # in case removing some of these dont get defined.
                 uv = uvkey = uv_dict = f_index = uv_index = None
 
-                uv_face_mapping = [[0, 0, 0, 0] for i in range(len(face_index_pairs))]  # a bit of a waste for tri's :/
+                uv_face_mapping = [[0] * a[0].loop_total for i, a in enumerate(face_index_pairs)]
 
                 uv_dict = {}  # could use a set() here
-                uv_layer = me.tessface_uv_textures.active.data
                 for f, f_index in face_index_pairs:
-                    for uv_index, uv in enumerate(uv_layer[f_index].uv):
+                    for uv_index, l_index in enumerate(f.loop_indices):
+                        uv = uv_layer[l_index].uv
+
                         uvkey = veckey2d(uv)
                         try:
                             uv_face_mapping[f_index][uv_index] = uv_dict[uvkey]
@@ -489,7 +506,7 @@
                 f_mat = min(f.material_index, len(materials) - 1)
 
                 if faceuv:
-                    tface = uv_layer[f_index]
+                    tface = uv_texture[f_index]
                     f_image = tface.image
 
                 # MAKE KEY
@@ -559,61 +576,53 @@
                         fw('s off\n')
                         contextSmooth = f_smooth
 
-                f_v_orig = [(vi, me_verts[v_idx]) for vi, v_idx in enumerate(f.vertices)]
+                f_v = [(vi, me_verts[v_idx]) for vi, v_idx in enumerate(f.vertices)]
 
-                if not EXPORT_TRI or len(f_v_orig) == 3:
-                    f_v_iter = (f_v_orig, )
-                else:
-                    f_v_iter = (f_v_orig[0], f_v_orig[1], f_v_orig[2]), (f_v_orig[0], f_v_orig[2], f_v_orig[3])
+                fw('f')
+                if faceuv:
+                    if EXPORT_NORMALS:
+                        if f_smooth:  # Smoothed, use vertex normals
+                            for vi, v in f_v:
+                                fw(" %d/%d/%d" %
+                                           (v.index + totverts,
+                                            totuvco + uv_face_mapping[f_index][vi],
+                                            globalNormals[veckey3d(v.normal)],
+                                            ))  # vert, uv, normal
 
-                # support for triangulation
-                for f_v in f_v_iter:
-                    fw('f')
+                        else:  # No smoothing, face normals
+                            no = globalNormals[veckey3d(f.normal)]
+                            for vi, v in f_v:
+                                fw(" %d/%d/%d" %
+                                           (v.index + totverts,
+                                            totuvco + uv_face_mapping[f_index][vi],
+                                            no,
+                                            ))  # vert, uv, normal
+                    else:  # No Normals
+                        for vi, v in f_v:
+                            fw(" %d/%d" % (
+                                       v.index + totverts,
+                                       totuvco + uv_face_mapping[f_index][vi],
+                                       ))  # vert, uv
 
-                    if faceuv:
-                        if EXPORT_NORMALS:
-                            if f_smooth:  # Smoothed, use vertex normals
-                                for vi, v in f_v:
-                                    fw(" %d/%d/%d" %
-                                               (v.index + totverts,
-                                                totuvco + uv_face_mapping[f_index][vi],
-                                                globalNormals[veckey3d(v.normal)],
-                                                ))  # vert, uv, normal
+                    face_vert_index += len(f_v)
 
-                            else:  # No smoothing, face normals
-                                no = globalNormals[veckey3d(f.normal)]
-                                for vi, v in f_v:
-                                    fw(" %d/%d/%d" %
-                                               (v.index + totverts,
-                                                totuvco + uv_face_mapping[f_index][vi],
-                                                no,
-                                                ))  # vert, uv, normal
-                        else:  # No Normals
+                else:  # No UV's
+                    if EXPORT_NORMALS:
+                        if f_smooth:  # Smoothed, use vertex normals
                             for vi, v in f_v:
-                                fw(" %d/%d" % (
+                                fw(" %d//%d" % (
                                            v.index + totverts,
-                                           totuvco + uv_face_mapping[f_index][vi],
-                                           ))  # vert, uv
-
-                        face_vert_index += len(f_v)
-
-                    else:  # No UV's
-                        if EXPORT_NORMALS:
-                            if f_smooth:  # Smoothed, use vertex normals
-                                for vi, v in f_v:
-                                    fw(" %d//%d" % (
-                                               v.index + totverts,
-                                               globalNormals[veckey3d(v.normal)],
-                                               ))
-                            else:  # No smoothing, face normals
-                                no = globalNormals[veckey3d(f.normal)]
-                                for vi, v in f_v:
-                                    fw(" %d//%d" % (v.index + totverts, no))
-                        else:  # No Normals
+                                           globalNormals[veckey3d(v.normal)],
+                                           ))
+                        else:  # No smoothing, face normals
+                            no = globalNormals[veckey3d(f.normal)]
                             for vi, v in f_v:
-                                fw(" %d" % (v.index + totverts))
+                                fw(" %d//%d" % (v.index + totverts, no))
+                    else:  # No Normals
+                        for vi, v in f_v:
+                            fw(" %d" % (v.index + totverts))
 
-                    fw('\n')
+                fw('\n')
 
             # Write edges.
             if EXPORT_EDGES:



More information about the Bf-extensions-cvs mailing list