Fix T72148: OBJ import could fail with spaces in filenames

Thing here was that files could actually be loaded ('obj_image_load' has
smart code for this), but tokenizing the corresponding line afterwards
could still fail [would have part of a filename still in image_data
list].

Now also correct this 'image_data' list in case 'obj_image_load' found
images with filename spaces.

Maniphest Tasks: T72148

Differential Revision: https://developer.blender.org/D6358
This commit is contained in:
Philipp Oeser 2019-12-04 13:51:17 +01:00
parent a7acf9e1d3
commit bf89c1eb28
Notes: blender-bot 2023-02-14 19:04:24 +01:00
Referenced by issue #72148, Error importing .obj from Photoshop (filename with space)
1 changed files with 11 additions and 4 deletions

View File

@ -87,13 +87,14 @@ def filenames_group_by_ext(line, ext):
i_prev = i
def obj_image_load(context_imagepath_map, line, DIR, recursive, relpath):
def obj_image_load(img_data, context_imagepath_map, line, DIR, recursive, relpath):
"""
Mainly uses comprehensiveImageLoad
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.
Also handle " chars (some software use those to protect filenames with spaces, see T67266... sic).
Also corrects img_data (in case filenames with spaces have been split up in multiple entries, see T72148).
"""
filepath_parts = line.split(b' ')
@ -113,7 +114,13 @@ def obj_image_load(context_imagepath_map, line, DIR, recursive, relpath):
image = load_image(imagepath.replace("_", " "), DIR, recursive=recursive, relpath=relpath)
if image is not None:
context_imagepath_map[imagepath] = image
del img_data[i:]
img_data.append(imagepath)
break;
else:
del img_data[i:]
img_data.append(imagepath)
break;
if image is None:
imagepath = os.fsdecode(filepath_parts[-1])
@ -147,6 +154,9 @@ def create_materials(filepath, relpath,
"""
map_options = {}
# Absolute path - c:\.. etc would work here
image = obj_image_load(img_data, context_imagepath_map, line, DIR, use_image_search, relpath)
curr_token = []
for token in img_data[:-1]:
if token.startswith(b'-') and token[1:].isalpha():
@ -157,9 +167,6 @@ def create_materials(filepath, relpath,
if curr_token:
map_options[curr_token[0]] = curr_token[1:]
# Absolute path - c:\.. etc would work here
image = obj_image_load(context_imagepath_map, line, DIR, use_image_search, relpath)
map_offset = map_options.get(b'-o')
map_scale = map_options.get(b'-s')
if map_offset is not None: