From df4dd70d7bc1f60cb9ea5bbb2da4f0319f6f55b5 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 30 Aug 2010 08:28:48 +0000 Subject: [PATCH] various utf8 compatibility fixes - OBJ import/export now work with non utf8 paths. (all exporters and importers need changes like this) - strip non utf8 chars from new ID blocks (also applies to renaming) - set the file rename button to allow non-utf8 chars. --- release/scripts/io/export_obj.py | 12 ++++++------ release/scripts/io/import_scene_obj.py | 8 ++++---- source/blender/blenkernel/intern/library.c | 14 +++++++++++--- source/blender/editors/space_file/file_draw.c | 1 + source/blender/python/intern/bpy_rna.c | 2 +- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/release/scripts/io/export_obj.py b/release/scripts/io/export_obj.py index 93146922a40..d7599527029 100644 --- a/release/scripts/io/export_obj.py +++ b/release/scripts/io/export_obj.py @@ -122,7 +122,7 @@ def write_mtl(scene, filepath, copy_images, mtl_dict): try: filepath = copy_image(mtex.texture.image) # filepath = mtex.texture.image.filepath.split('\\')[-1].split('/')[-1] - file.write('map_Kd %s\n' % filepath) # Diffuse mapping image + file.write('map_Kd %s\n' % repr(filepath)[1:-1]) # Diffuse mapping image break except: # Texture has no image though its an image type, best ignore. @@ -332,7 +332,7 @@ def write_file(filepath, objects, scene, return ret - print('OBJ Export path: "%s"' % filepath) + print('OBJ Export path: %r' % filepath) temp_mesh_name = '~tmp-mesh' time1 = time.clock() @@ -342,13 +342,13 @@ def write_file(filepath, objects, scene, file = open(filepath, "w") # Write Header - file.write('# Blender v%s OBJ File: %s\n' % (bpy.app.version_string, bpy.data.filepath.split('/')[-1].split('\\')[-1] )) + 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') # Tell the obj file what material file to use. if EXPORT_MTL: - mtlfilepath = '%s.mtl' % '.'.join(filepath.split('.')[:-1]) - file.write('mtllib %s\n' % ( mtlfilepath.split('\\')[-1].split('/')[-1] )) + 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 if EXPORT_ROTX90: mat_xrot90= mathutils.Matrix.Rotation(-math.pi/2, 4, 'X') @@ -864,7 +864,7 @@ class ExportOBJ(bpy.types.Operator): # List of operator properties, the attributes will be assigned # to the class instance from the operator settings before calling. - filepath = StringProperty(name="File Path", description="Filepath used for exporting the OBJ file", maxlen= 1024, default= "") + filepath = StringProperty(name="File Path", description="Filepath used for exporting the OBJ file", maxlen= 1024, default= "", subtype='FILE_PATH') check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'}) # context group diff --git a/release/scripts/io/import_scene_obj.py b/release/scripts/io/import_scene_obj.py index 88caaf7eeb5..9ceb7a04fee 100644 --- a/release/scripts/io/import_scene_obj.py +++ b/release/scripts/io/import_scene_obj.py @@ -892,7 +892,7 @@ def load_obj(filepath, This function passes the file and sends the data off to be split into objects and then converted into mesh objects ''' - print('\nimporting obj "%s"' % filepath) + print('\nimporting obj %r' % filepath) if SPLIT_OBJECTS or SPLIT_GROUPS or SPLIT_MATERIALS: POLYGROUPS = False @@ -935,7 +935,7 @@ def load_obj(filepath, # so we need to know weather context_multi_line= '' - print('\tparsing obj file "%s"...' % filepath) + print("\tparsing obj file...") time_sub= time.time() # time_sub= sys.time() @@ -1220,7 +1220,7 @@ def load_obj(filepath, # time_new= sys.time() print('%.4f sec' % (time_new-time_sub)) - print('finished importing: "%s" in %.4f sec.' % (filepath, (time_new-time_main))) + print('finished importing: %r in %.4f sec.' % (filepath, (time_new-time_main))) DEBUG= True @@ -1467,7 +1467,7 @@ class IMPORT_OT_obj(bpy.types.Operator): # to the class instance from the operator settings before calling. - filepath = StringProperty(name="File Path", description="Filepath used for importing the OBJ file", maxlen= 1024, default= "") + filepath = StringProperty(name="File Path", description="Filepath used for importing the OBJ file", maxlen= 1024, default= "", subtype='FILE_PATH') CREATE_SMOOTH_GROUPS = BoolProperty(name="Smooth Groups", description="Surround smooth groups by sharp edges", default= True) CREATE_FGONS = BoolProperty(name="NGons as FGons", description="Import faces with more then 4 verts as fgons", default= True) diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c index 8c8e4bb034f..64d9a23b6a6 100644 --- a/source/blender/blenkernel/intern/library.c +++ b/source/blender/blenkernel/intern/library.c @@ -1185,8 +1185,15 @@ int new_id(ListBase *lb, ID *id, const char *tname) * easier to assign each time then to check if its needed */ name[sizeof(name)-1]= 0; - if(name[0] == '\0') + if(name[0] == '\0') { + /* disallow empty names */ strcpy(name, ID_FALLBACK_NAME); + } + else { + /* disallow non utf8 chars, + * the interface checks for this but new ID's based on file names dont */ + BLI_utf8_invalid_strip(name, strlen(name)); + } result = check_for_dupid(lb, id, name); strcpy(id->name+2, name); @@ -1377,8 +1384,9 @@ void text_idbutton(struct ID *id, char *text) text[4]= 0; } } - else - strcpy(text, ""); + else { + text[0]= '\0'; + } } void rename_id(ID *id, char *name) diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c index 0867acbfb60..5f435aa0cbe 100644 --- a/source/blender/editors/space_file/file_draw.c +++ b/source/blender/editors/space_file/file_draw.c @@ -544,6 +544,7 @@ void file_draw_list(const bContext *C, ARegion *ar) uiBut *but = uiDefBut(block, TEX, 1, "", spos, sy-layout->tile_h-3, but_width, layout->textheight*2, sfile->params->renameedit, 1.0f, (float)sizeof(sfile->params->renameedit),0,0,""); uiButSetRenameFunc(but, renamebutton_cb, file); + uiButSetFlag(but, UI_BUT_NO_UTF8); /* allow non utf8 names */ if ( 0 == uiButActiveOnly(C, block, but)) { file->flags &= ~EDITING; } diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 2c16d4f2b56..6cc191757ed 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -233,7 +233,7 @@ static const char *py_safe_unicode_to_byte(PyObject *py_str, PyObject **coerce) } } -static PyObject *py_safe_byte_to_unicode(char *str) +static PyObject *py_safe_byte_to_unicode(const char *str) { PyObject *result= PyUnicode_FromString(str); if(result) {