[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1531] trunk/py/scripts/addons/ io_scene_obj/import_obj.py: use bytes rather then unicode for importing OBJ 's

Campbell Barton ideasman42 at gmail.com
Tue Feb 1 05:26:26 CET 2011


Revision: 1531
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=1531
Author:   campbellbarton
Date:     2011-02-01 04:26:26 +0000 (Tue, 01 Feb 2011)
Log Message:
-----------
use bytes rather then unicode for importing OBJ's

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

Modified: trunk/py/scripts/addons/io_scene_obj/import_obj.py
===================================================================
--- trunk/py/scripts/addons/io_scene_obj/import_obj.py	2011-02-01 03:11:16 UTC (rev 1530)
+++ trunk/py/scripts/addons/io_scene_obj/import_obj.py	2011-02-01 04:26:26 UTC (rev 1531)
@@ -236,12 +236,12 @@
         return line_split[1]
 
     elif length > 2:
-        return ' '.join(line_split[1:])
+        return b' '.join(line_split[1:])
 
 
 def obj_image_load(imagepath, DIR, IMAGE_SEARCH):
-    if '_' in imagepath:
-        image = load_image(imagepath.replace('_', ' '), DIR)
+    if b'_' in imagepath:
+        image = load_image(imagepath.replace(b'_', b' '), DIR)
         if image:
             return image
 
@@ -351,12 +351,12 @@
             mtex.texture = texture
             mtex.texture_coords = 'UV'
             mtex.use_map_reflect = True
-            
+
         else:
-            raise Exception("invalid type '%s'" % type)
+            raise Exception("invalid type %r" % type)
 
     # Add an MTL with the same name as the obj if no MTLs are spesified.
-    temp_mtl = os.path.splitext((os.path.basename(filepath)))[0] + '.mtl'
+    temp_mtl = os.path.splitext((os.path.basename(filepath)))[0] + b'.mtl'
 
     if os.path.exists(os.path.join(DIR, temp_mtl)) and temp_mtl not in material_libs:
         material_libs.append(temp_mtl)
@@ -365,22 +365,23 @@
     #Create new materials
     for name in unique_materials:  # .keys()
         if name != None:
-            unique_materials[name] = bpy.data.materials.new(name)
+            unique_materials[name] = bpy.data.materials.new(name.decode('utf-8', "surrogateescape"))
             unique_material_images[name] = None, False  # assign None to all material images to start with, add to later.
 
     unique_materials[None] = None
     unique_material_images[None] = None, False
 
     for libname in material_libs:
+        # print(libname)
         mtlpath = os.path.join(DIR, libname)
         if not os.path.exists(mtlpath):
-            print ("\tError Missing MTL: '%s'" % mtlpath)
+            print ("\tError Missing MTL: %r" % mtlpath)
         else:
-            #print '\t\tloading mtl: "%s"' % mtlpath
+            #print('\t\tloading mtl: %e' % mtlpath)
             context_material = None
-            mtl = open(mtlpath, 'rU')
+            mtl = open(mtlpath, 'rb')
             for line in mtl:  # .readlines():
-                if line.startswith('newmtl'):
+                if line.startswith(b'newmtl'):
                     context_material_name = line_value(line.split())
                     if context_material_name in unique_materials:
                         context_material = unique_materials[context_material_name]
@@ -391,42 +392,42 @@
                     # we need to make a material to assign properties to it.
                     line_split = line.split()
                     line_lower = line.lower().lstrip()
-                    if line_lower.startswith('ka'):
+                    if line_lower.startswith(b'ka'):
                         context_material.mirror_color = float(line_split[1]), float(line_split[2]), float(line_split[3])
-                    elif line_lower.startswith('kd'):
+                    elif line_lower.startswith(b'kd'):
                         context_material.diffuse_color = float(line_split[1]), float(line_split[2]), float(line_split[3])
-                    elif line_lower.startswith('ks'):
+                    elif line_lower.startswith(b'ks'):
                         context_material.specular_color = float(line_split[1]), float(line_split[2]), float(line_split[3])
-                    elif line_lower.startswith('ns'):
+                    elif line_lower.startswith(b'ns'):
                         context_material.specular_hardness = int((float(line_split[1]) * 0.51))
-                    elif line_lower.startswith('ni'):  # Refraction index
+                    elif line_lower.startswith(b'ni'):  # Refraction index
                         context_material.raytrace_transparency.ior = max(1, min(float(line_split[1]), 3))  # between 1 and 3
-                    elif line_lower.startswith('d') or line_lower.startswith('tr'):
+                    elif line_lower.startswith(b'd') or line_lower.startswith(b'tr'):
                         context_material.alpha = float(line_split[1])
                         context_material.use_transparency = True
                         context_material.transparency_method = 'Z_TRANSPARENCY'
-                    elif line_lower.startswith('map_ka'):
+                    elif line_lower.startswith(b'map_ka'):
                         img_filepath = line_value(line.split())
                         if img_filepath:
                             load_material_image(context_material, context_material_name, img_filepath, 'Ka')
-                    elif line_lower.startswith('map_ks'):
+                    elif line_lower.startswith(b'map_ks'):
                         img_filepath = line_value(line.split())
                         if img_filepath:
                             load_material_image(context_material, context_material_name, img_filepath, 'Ks')
-                    elif line_lower.startswith('map_kd'):
+                    elif line_lower.startswith(b'map_kd'):
                         img_filepath = line_value(line.split())
                         if img_filepath:
                             load_material_image(context_material, context_material_name, img_filepath, 'Kd')
-                    elif line_lower.startswith('map_bump') or line_lower.startswith('bump'): # 'bump' is incorrect but some files use it.
+                    elif line_lower.startswith(b'map_bump') or line_lower.startswith(b'bump'):  # 'bump' is incorrect but some files use it.
                         img_filepath = line_value(line.split())
                         if img_filepath:
                             load_material_image(context_material, context_material_name, img_filepath, 'Bump')
-                    elif line_lower.startswith('map_d') or line_lower.startswith('map_tr'):  # Alpha map - Dissolve
+                    elif line_lower.startswith(b'map_d') or line_lower.startswith(b'map_tr'):  # Alpha map - Dissolve
                         img_filepath = line_value(line.split())
                         if img_filepath:
                             load_material_image(context_material, context_material_name, img_filepath, 'D')
 
-                    elif line_lower.startswith('refl'):  # reflectionmap
+                    elif line_lower.startswith(b'refl'):  # reflectionmap
                         img_filepath = line_value(line.split())
                         if img_filepath:
                             load_material_image(context_material, context_material_name, img_filepath, 'refl')
@@ -608,7 +609,7 @@
     for name, index in list(material_mapping.items()):
         materials[index] = unique_materials[name]
 
-    me = bpy.data.meshes.new(dataname)
+    me = bpy.data.meshes.new(dataname.decode('utf-8', "surrogateescape"))
 
     # make sure the list isnt too big
     for material in materials:
@@ -757,18 +758,18 @@
     '''
     Add nurbs object to blender, only support one type at the moment
     '''
-    deg = context_nurbs.get('deg', (3,))
-    curv_range = context_nurbs.get('curv_range')
-    curv_idx = context_nurbs.get('curv_idx', [])
-    parm_u = context_nurbs.get('parm_u', [])
-    parm_v = context_nurbs.get('parm_v', [])
-    name = context_nurbs.get('name', 'ObjNurb')
-    cstype = context_nurbs.get('cstype')
+    deg = context_nurbs.get(b'deg', (3,))
+    curv_range = context_nurbs.get(b'curv_range')
+    curv_idx = context_nurbs.get(b'curv_idx', [])
+    parm_u = context_nurbs.get(b'parm_u', [])
+    parm_v = context_nurbs.get(b'parm_v', [])
+    name = context_nurbs.get(b'name', b'ObjNurb')
+    cstype = context_nurbs.get(b'cstype')
 
     if cstype is None:
         print('\tWarning, cstype not found')
         return
-    if cstype != 'bspline':
+    if cstype != b'bspline':
         print('\tWarning, cstype is not supported (only bspline)')
         return
     if not curv_idx:
@@ -778,7 +779,7 @@
         print('\tWarning, surfaces not supported')
         return
 
-    cu = bpy.data.curves.new(name, 'CURVE')
+    cu = bpy.data.curves.new(name.decode('utf-8', "surrogateescape"), 'CURVE')
     cu.dimensions = '3D'
 
     nu = cu.splines.new('NURBS')
@@ -821,13 +822,13 @@
         nu.use_cyclic_u = True
     '''
 
-    ob = bpy.data.objects.new("Nurb", cu)
+    ob = bpy.data.objects.new(name.decode('utf-8', "surrogateescape"), cu)
 
     new_objects.append(ob)
 
 
 def strip_slash(line_split):
-    if line_split[-1][-1] == "\\":
+    if line_split[-1][-1] == 92:  # '\' char
         if len(line_split[-1]) == 1:
             line_split.pop()  # remove the \ item
         else:
@@ -841,13 +842,13 @@
     find the float function for this obj file
     - whether to replace commas or not
     '''
-    file = open(filepath, 'rU')
+    file = open(filepath, 'rb')
     for line in file:  # .readlines():
         line = line.lstrip()
-        if line.startswith('v'):  # vn vt v
-            if ',' in line:
-                return lambda f: float(f.replace(',', '.'))
-            elif '.' in line:
+        if line.startswith(b'v'):  # vn vt v
+            if b',' in line:
+                return lambda f: float(f.replace(b',', b'.'))
+            elif b'.' in line:
                 return float
 
     # incase all vert values were ints
@@ -872,6 +873,8 @@
     '''
     print('\nimporting obj %r' % filepath)
 
+    filepath = filepath.encode()
+
     if SPLIT_OBJECTS or SPLIT_GROUPS:
         POLYGROUPS = False
 
@@ -895,7 +898,7 @@
     # Nurbs
     context_nurbs = {}
     nurbs = []
-    context_parm = ''  # used by nurbs too but could be used elsewhere
+    context_parm = b''  # used by nurbs too but could be used elsewhere
 
     has_ngons = False
     # has_smoothgroups= False - is explicit with len(unique_smooth_groups) being > 0
@@ -910,31 +913,31 @@
     # it means they are multiline-
     # since we use xreadline we cant skip to the next line
     # so we need to know whether
-    context_multi_line = ""
+    context_multi_line = b''
 
     print("\tparsing obj file...")
     time_sub = time.time()
 #     time_sub= sys.time()
 
-    file = open(filepath, 'rU')
+    file = open(filepath, 'rb')

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list