[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2378] trunk/py/scripts/addons/ io_scene_obj/export_obj.py: remove comments, cache file write command.

Campbell Barton ideasman42 at gmail.com
Thu Sep 29 15:03:18 CEST 2011


Revision: 2378
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2378
Author:   campbellbarton
Date:     2011-09-29 13:03:18 +0000 (Thu, 29 Sep 2011)
Log Message:
-----------
remove comments, cache file write command.

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	2011-09-29 12:49:39 UTC (rev 2377)
+++ trunk/py/scripts/addons/io_scene_obj/export_obj.py	2011-09-29 13:03:18 UTC (rev 2378)
@@ -46,9 +46,11 @@
     dest_dir = os.path.dirname(filepath)
 
     file = open(filepath, "w", encoding="utf8", newline="\n")
-    file.write('# Blender MTL File: %r\n' % os.path.basename(bpy.data.filepath))
-    file.write('# Material Count: %i\n' % len(mtl_dict))
+    fw = file.write
 
+    fw('# Blender MTL File: %r\n' % os.path.basename(bpy.data.filepath))
+    fw('# Material Count: %i\n' % len(mtl_dict))
+
     mtl_dict_values = list(mtl_dict.values())
     mtl_dict_values.sort(key=lambda m: m[0])
 
@@ -59,7 +61,7 @@
         # Get the Blender data for the material and the image.
         # Having an image named None will make a bug, dont do it :)
 
-        file.write('newmtl %s\n' % mtl_mat_name)  # Define a new material: matname_imgname
+        fw('newmtl %s\n' % mtl_mat_name)  # Define a new material: matname_imgname
 
         if mat:
             # convert from blenders spec to 0 - 1000 range.
@@ -67,40 +69,40 @@
                 tspec = (0.4 - mat.specular_slope) / 0.0004
             else:
                 tspec = (mat.specular_hardness - 1) * 1.9607843137254901
-            file.write('Ns %.6f\n' % tspec)
+            fw('Ns %.6f\n' % tspec)
             del tspec
 
-            file.write('Ka %.6f %.6f %.6f\n' % (mat.ambient * world_amb)[:])  # Ambient, uses mirror colour,
-            file.write('Kd %.6f %.6f %.6f\n' % (mat.diffuse_intensity * mat.diffuse_color)[:])  # Diffuse
-            file.write('Ks %.6f %.6f %.6f\n' % (mat.specular_intensity * mat.specular_color)[:])  # Specular
+            fw('Ka %.6f %.6f %.6f\n' % (mat.ambient * world_amb)[:])  # Ambient, uses mirror colour,
+            fw('Kd %.6f %.6f %.6f\n' % (mat.diffuse_intensity * mat.diffuse_color)[:])  # Diffuse
+            fw('Ks %.6f %.6f %.6f\n' % (mat.specular_intensity * mat.specular_color)[:])  # Specular
             if hasattr(mat, "ior"):
-                file.write('Ni %.6f\n' % mat.ior)  # Refraction index
+                fw('Ni %.6f\n' % mat.ior)  # Refraction index
             else:
-                file.write('Ni %.6f\n' % 1.0)
-            file.write('d %.6f\n' % mat.alpha)  # Alpha (obj uses 'd' for dissolve)
+                fw('Ni %.6f\n' % 1.0)
+            fw('d %.6f\n' % mat.alpha)  # Alpha (obj uses 'd' for dissolve)
 
             # 0 to disable lighting, 1 for ambient & diffuse only (specular color set to black), 2 for full lighting.
             if mat.use_shadeless:
-                file.write('illum 0\n')  # ignore lighting
+                fw('illum 0\n')  # ignore lighting
             elif mat.specular_intensity == 0:
-                file.write('illum 1\n')  # no specular.
+                fw('illum 1\n')  # no specular.
             else:
-                file.write('illum 2\n')  # light normaly
+                fw('illum 2\n')  # light normaly
 
         else:
             #write a dummy material here?
-            file.write('Ns 0\n')
-            file.write('Ka %.6f %.6f %.6f\n' % world_amb[:])  # Ambient, uses mirror colour,
-            file.write('Kd 0.8 0.8 0.8\n')
-            file.write('Ks 0.8 0.8 0.8\n')
-            file.write('d 1\n')  # No alpha
-            file.write('illum 2\n')  # light normaly
+            fw('Ns 0\n')
+            fw('Ka %.6f %.6f %.6f\n' % world_amb[:])  # Ambient, uses mirror colour,
+            fw('Kd 0.8 0.8 0.8\n')
+            fw('Ks 0.8 0.8 0.8\n')
+            fw('d 1\n')  # No alpha
+            fw('illum 2\n')  # light normaly
 
         # Write images!
         if face_img:  # We have an image on the face!
             # write relative image path
             rel = bpy_extras.io_utils.path_reference(face_img.filepath, source_dir, dest_dir, path_mode, "", copy_set)
-            file.write('map_Kd %s\n' % rel)  # Diffuse mapping image
+            fw('map_Kd %s\n' % rel)  # Diffuse mapping image
 
         if mat:  # No face image. if we havea material search for MTex image.
             image_map = {}
@@ -127,9 +129,9 @@
 
             for key, image in image_map.items():
                 filepath = bpy_extras.io_utils.path_reference(image.filepath, source_dir, dest_dir, path_mode, "", copy_set)
-                file.write('%s %s\n' % (key, repr(filepath)[1:-1]))
+                fw('%s %s\n' % (key, repr(filepath)[1:-1]))
 
-        file.write('\n\n')
+        fw('\n\n')
 
     file.close()
 
@@ -173,13 +175,13 @@
         do_endpoints = (do_closed == 0) and nu.use_endpoint_u
 
         for pt in nu.points:
-            file.write('v %.6f %.6f %.6f\n' % (ob_mat * pt.co.to_3d())[:])
+            fw('v %.6f %.6f %.6f\n' % (ob_mat * pt.co.to_3d())[:])
             pt_num += 1
         tot_verts += pt_num
 
-        file.write('g %s\n' % (name_compat(ob.name)))  # name_compat(ob.getData(1)) could use the data name too
-        file.write('cstype bspline\n')  # not ideal, hard coded
-        file.write('deg %d\n' % DEG_ORDER_U)  # not used for curves but most files have it still
+        fw('g %s\n' % (name_compat(ob.name)))  # name_compat(ob.getData(1)) could use the data name too
+        fw('cstype bspline\n')  # not ideal, hard coded
+        fw('deg %d\n' % DEG_ORDER_U)  # not used for curves but most files have it still
 
         curve_ls = [-(i + 1) for i in range(pt_num)]
 
@@ -192,7 +194,7 @@
                 pt_num += DEG_ORDER_U
                 curve_ls = curve_ls + curve_ls[0:DEG_ORDER_U]
 
-        file.write('curv 0.0 1.0 %s\n' % (" ".join([str(i) for i in curve_ls])))  # Blender has no U and V values for the curve
+        fw('curv 0.0 1.0 %s\n' % (" ".join([str(i) for i in curve_ls])))  # Blender has no U and V values for the curve
 
         # 'parm' keyword
         tot_parm = (DEG_ORDER_U + 1) + pt_num
@@ -204,9 +206,9 @@
                 parm_ls[i] = 0.0
                 parm_ls[-(1 + i)] = 1.0
 
-        file.write("parm u %s\n" % " ".join(["%.6f" % i for i in parm_ls]))
+        fw("parm u %s\n" % " ".join(["%.6f" % i for i in parm_ls]))
 
-        file.write('end\n')
+        fw('end\n')
 
     return tot_verts
 
@@ -265,20 +267,19 @@
 
     print('OBJ Export path: %r' % filepath)
 
-    time1 = time.clock()
-#   time1 = sys.time()
-#   scn = Scene.GetCurrent()
+    time1 = time.time()
 
     file = open(filepath, "w", encoding="utf8", newline="\n")
+    fw = file.write
 
     # Write Header
-    file.write('# Blender v%s OBJ File: %r\n' % (bpy.app.version_string, os.path.basename(bpy.data.filepath)))
-    file.write('# www.blender.org\n')
+    fw('# Blender v%s OBJ File: %r\n' % (bpy.app.version_string, os.path.basename(bpy.data.filepath)))
+    fw('# www.blender.org\n')
 
     # Tell the obj file what material file to use.
     if EXPORT_MTL:
         mtlfilepath = os.path.splitext(filepath)[0] + ".mtl"
-        file.write('mtllib %s\n' % repr(os.path.basename(mtlfilepath))[1:-1])  # filepath can contain non utf8 chars, use repr
+        fw('mtllib %s\n' % repr(os.path.basename(mtlfilepath))[1:-1])  # filepath can contain non utf8 chars, use repr
 
     # Initialize totals, these are updated each object
     totverts = totuvco = totno = 1
@@ -334,11 +335,6 @@
 
             me.transform(EXPORT_GLOBAL_MATRIX * ob_mat)
 
-#           # Will work for non meshes now! :)
-#           me= BPyMesh.getMeshFromObject(ob, containerMesh, EXPORT_APPLY_MODIFIERS, EXPORT_POLYGROUPS, scn)
-#           if not me:
-#               continue
-
             if EXPORT_UV:
                 faceuv = len(me.uv_textures) > 0
                 if faceuv:
@@ -386,18 +382,6 @@
             else:
                 # no materials
                 face_index_pairs.sort(key=lambda a: a[0].use_smooth)
-#           if EXPORT_KEEP_VERT_ORDER:
-#               pass
-#           elif faceuv:
-#               try:    faces.sort(key = lambda a: (a.mat, a.image, a.use_smooth))
-#               except: faces.sort(lambda a,b: cmp((a.mat, a.image, a.use_smooth), (b.mat, b.image, b.use_smooth)))
-#           elif len(materials) > 1:
-#               try:    faces.sort(key = lambda a: (a.mat, a.use_smooth))
-#               except: faces.sort(lambda a,b: cmp((a.mat, a.use_smooth), (b.mat, b.use_smooth)))
-#           else:
-#               # no materials
-#               try:    faces.sort(key = lambda a: a.use_smooth)
-#               except: faces.sort(lambda a,b: cmp(a.use_smooth, b.use_smooth))
 
             # Set the default mat to no material and no image.
             contextMat = 0, 0  # Can never be this, so we will label a new material the first chance we get.
@@ -412,16 +396,19 @@
                     obnamestring = '%s_%s' % (name_compat(name1), name_compat(name2))
 
                 if EXPORT_BLEN_OBS:
-                    file.write('o %s\n' % obnamestring)  # Write Object name
+                    fw('o %s\n' % obnamestring)  # Write Object name
                 else:  # if EXPORT_GROUP_BY_OB:
-                    file.write('g %s\n' % obnamestring)
+                    fw('g %s\n' % obnamestring)
 
             # Vert
             for v in me_verts:
-                file.write('v %.6f %.6f %.6f\n' % v.co[:])
+                fw('v %.6f %.6f %.6f\n' % v.co[:])
 
             # UV
             if faceuv:
+                # incase 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_dict = {}  # could use a set() here
@@ -433,10 +420,11 @@
                             uv_face_mapping[f_index][uv_index] = uv_dict[uvkey]
                         except:
                             uv_face_mapping[f_index][uv_index] = uv_dict[uvkey] = len(uv_dict)
-                            file.write('vt %.6f %.6f\n' % uv[:])
+                            fw('vt %.6f %.6f\n' % uv[:])
 
                 uv_unique_count = len(uv_dict)
-#               del uv, uvkey, uv_dict, f_index, uv_index
+
+                del uv, uvkey, uv_dict, f_index, uv_index
                 # Only need uv_unique_count and uv_face_mapping
 
             # NORMAL, Smooth/Non smoothed.
@@ -449,14 +437,14 @@

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list