From 37b18867691135c0845804fc99ea1b4d204063bf Mon Sep 17 00:00:00 2001 From: Aras Pranckevicius Date: Wed, 3 Sep 2025 10:26:10 +0200 Subject: [PATCH] Fix #145244: FBX importer does not import textures from some files Some FBX files contain somewhat incorrect relative paths for their material textures. The old Python importer was "working" only as a side effect that it was exhaustively searching the subfolder hierarchy. Try to do a smaller version of that here too: if referenced image is still not found after trying the regular relative/absolute paths, try taking longer path parent chains from the absolute path coming from FBX file, and using that as relative path wrt the FBX file itself. Pull Request: https://projects.blender.org/blender/blender/pulls/145641 --- .../io/fbx/importer/fbx_import_material.cc | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/source/blender/io/fbx/importer/fbx_import_material.cc b/source/blender/io/fbx/importer/fbx_import_material.cc index 870eaa45d80..03d2dc959ca 100644 --- a/source/blender/io/fbx/importer/fbx_import_material.cc +++ b/source/blender/io/fbx/importer/fbx_import_material.cc @@ -214,14 +214,31 @@ static Image *load_texture_image(Main *bmain, const std::string &file_dir, const { /* Check with filename directly. */ Image *image = BKE_image_load_exists(bmain, tex.filename.data); + /* Try loading as a relative path. */ if (image == nullptr) { - /* Try loading as a relative path. */ std::string path = file_dir + "/" + tex.filename.data; image = BKE_image_load_exists(bmain, path.c_str()); - if (image == nullptr) { - /* Try loading with absolute path from FBX. */ - image = BKE_image_load_exists(bmain, tex.absolute_filename.data); - } + } + /* Try loading with absolute path from FBX. */ + if (image == nullptr) { + image = BKE_image_load_exists(bmain, tex.absolute_filename.data); + } + + /* If still not found, try taking progressively longer parts of the absolute path, + * as relative to the file. */ + if (image == nullptr) { + size_t pos = tex.absolute_filename.length; + do { + const char *parent_path = BLI_path_parent_dir_end(tex.absolute_filename.data, pos); + if (parent_path == nullptr) { + break; + } + char path[FILE_MAX]; + BLI_path_join(path, sizeof(path), file_dir.c_str(), parent_path); + BLI_path_normalize(path); + image = BKE_image_load_exists(bmain, path); + pos = parent_path - tex.absolute_filename.data; + } while (image == nullptr); } /* Create dummy/placeholder image. */