diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2016-09-15 14:05:15 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2016-09-15 14:05:15 +0300 |
commit | 959f9d28d26cd2de4c0db2fb4f65607e9ec7e2bf (patch) | |
tree | 06c49e6feb429096cef973610d6c6cb85ed14d9a | |
parent | 71a8a8961a327fd328adaac6b94330971658e056 (diff) |
Fix T49357: OBJ importer cannot deal with texture names with spaces.
OBJ/MTL does not support spaces in filenames, but... users keep using them - and other exporters
keep failing at being smart for the user here (like, replacing spaces with underscore etc.)...
So now, when last part of a texture line is not giving any valid found image, we try backward
adding more pieces to image filename until we find some existing file (hopefully).
But again: please do not use spaces in filenames for OBJ, **THIS IS NOT SUPPORTED**!
-rw-r--r-- | io_scene_obj/import_obj.py | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py index b0889d15..24a3b04f 100644 --- a/io_scene_obj/import_obj.py +++ b/io_scene_obj/import_obj.py @@ -58,17 +58,32 @@ def line_value(line_split): return b' '.join(line_split[1:]) -def obj_image_load(imagepath, DIR, recursive, relpath): +def obj_image_load(context_imagepath_map, line, DIR, recursive, relpath): """ Mainly uses comprehensiveImageLoad - but tries to replace '_' with ' ' for Max's exporter replaces spaces with underscores. + But we try all space-separated items from current line when file is not found with last one + (users keep generating/using image files with spaces in a format that does not support them, sigh...) + Also tries to replace '_' with ' ' for Max's exporter replaces spaces with underscores. """ - if "_" in imagepath: - image = load_image(imagepath.replace("_", " "), DIR, recursive=recursive, relpath=relpath) - if image: - return image + filepath_parts = line.split(b' ') + image = None + for i in range(-1, -len(filepath_parts), -1): + imagepath = os.fsdecode(b" ".join(filepath_parts[i:])) + image = context_imagepath_map.get(imagepath, ...) + if image is ...: + image = load_image(imagepath, DIR, recursive=recursive, relpath=relpath) + if image is None and "_" in imagepath: + image = load_image(imagepath.replace("_", " "), DIR, recursive=recursive, relpath=relpath) + if image is not None: + context_imagepath_map[imagepath] = image + break; + + if image is None: + imagepath = os.fsdecode(filepath_parts[-1]) + image = load_image(imagepath, DIR, recursive=recursive, place_holder=True, relpath=relpath) + context_imagepath_map[imagepath] = image - return load_image(imagepath, DIR, recursive=recursive, place_holder=True, relpath=relpath) + return image def create_materials(filepath, relpath, @@ -84,11 +99,10 @@ def create_materials(filepath, relpath, # Don't load the same image multiple times context_imagepath_map = {} - def load_material_image(blender_material, context_material_name, img_data, type): + def load_material_image(blender_material, context_material_name, img_data, line, type): """ Set textures defined in .mtl file. """ - imagepath = os.fsdecode(img_data[-1]) map_options = {} curr_token = [] @@ -102,10 +116,7 @@ def create_materials(filepath, relpath, texture = bpy.data.textures.new(name=type, type='IMAGE') # Absolute path - c:\.. etc would work here - image = context_imagepath_map.get(imagepath, ...) - if image == ...: - image = context_imagepath_map[imagepath] = \ - obj_image_load(imagepath, DIR, use_image_search, relpath) + image = obj_image_load(context_imagepath_map, line, DIR, use_image_search, relpath) if image is not None: texture.image = image @@ -404,37 +415,37 @@ def create_materials(filepath, relpath, elif line_id == b'map_ka': img_data = line.split()[1:] if img_data: - load_material_image(context_material, context_material_name, img_data, 'Ka') + load_material_image(context_material, context_material_name, img_data, line, 'Ka') elif line_id == b'map_ks': img_data = line.split()[1:] if img_data: - load_material_image(context_material, context_material_name, img_data, 'Ks') + load_material_image(context_material, context_material_name, img_data, line, 'Ks') elif line_id == b'map_kd': img_data = line.split()[1:] if img_data: - load_material_image(context_material, context_material_name, img_data, 'Kd') + load_material_image(context_material, context_material_name, img_data, line, 'Kd') elif line_id == b'map_ke': img_data = line.split()[1:] if img_data: - load_material_image(context_material, context_material_name, img_data, 'Ke') + load_material_image(context_material, context_material_name, img_data, line, 'Ke') elif line_id in {b'map_bump', b'bump'}: # 'bump' is incorrect but some files use it. img_data = line.split()[1:] if img_data: - load_material_image(context_material, context_material_name, img_data, 'Bump') + load_material_image(context_material, context_material_name, img_data, line, 'Bump') elif line_id in {b'map_d', b'map_tr'}: # Alpha map - Dissolve img_data = line.split()[1:] if img_data: - load_material_image(context_material, context_material_name, img_data, 'D') + load_material_image(context_material, context_material_name, img_data, line, 'D') elif line_id in {b'map_disp', b'disp'}: # displacementmap img_data = line.split()[1:] if img_data: - load_material_image(context_material, context_material_name, img_data, 'disp') + load_material_image(context_material, context_material_name, img_data, line, 'disp') elif line_id in {b'map_refl', b'refl'}: # reflectionmap img_data = line.split()[1:] if img_data: - load_material_image(context_material, context_material_name, img_data, 'refl') + load_material_image(context_material, context_material_name, img_data, line, 'refl') else: print("\t%r:%r (ignored)" % (filepath, line)) mtl.close() |