From bea52819190e54da2907e6c367b493db29f6f5c9 Mon Sep 17 00:00:00 2001 From: Aras Pranckevicius Date: Sun, 31 Jul 2022 18:10:48 +0300 Subject: Fix T100075: OBJ import: images loaded multiple times instead of being reused The new OBJ/MTL importer was creating a new image for any referenced texture, even if another material (or another property of the same material) already referenced the same texture. Make it use BKE_image_load_exists function just like Collada or USD importers do. Fixes T100075. Extended test coverage to count imported images; without the fix import_cubes_with_textures_rel would have incorrectly created 5 images instead of 4. --- .../io/wavefront_obj/importer/obj_import_mtl.cc | 2 +- .../io/wavefront_obj/tests/obj_importer_tests.cc | 53 +++++++++++++++++++--- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/source/blender/io/wavefront_obj/importer/obj_import_mtl.cc b/source/blender/io/wavefront_obj/importer/obj_import_mtl.cc index 60e419728f3..f7685ba0b7b 100644 --- a/source/blender/io/wavefront_obj/importer/obj_import_mtl.cc +++ b/source/blender/io/wavefront_obj/importer/obj_import_mtl.cc @@ -65,7 +65,7 @@ static bool load_texture_image_at_path(Main *bmain, bNode *r_node, const std::string &path) { - Image *tex_image = BKE_image_load(bmain, path.c_str()); + Image *tex_image = BKE_image_load_exists(bmain, path.c_str()); if (!tex_image) { fprintf(stderr, "Cannot load image file: '%s'\n", path.c_str()); return false; diff --git a/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc b/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc index 183c543d7e3..ecf1243fa86 100644 --- a/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc +++ b/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc @@ -47,7 +47,8 @@ class obj_importer_test : public BlendfileLoadingBaseTest { void import_and_check(const char *path, const Expectation *expect, size_t expect_count, - int expect_mat_count) + int expect_mat_count, + int expect_image_count = 0) { if (!blendfile_load("io_tests/blend_geometry/all_quads.blend")) { ADD_FAILURE(); @@ -132,12 +133,12 @@ class obj_importer_test : public BlendfileLoadingBaseTest { DEG_OBJECT_ITER_END; EXPECT_EQ(object_index, expect_count); - /* Count number of materials. */ - int mat_count = 0; - LISTBASE_FOREACH (ID *, id, &bfile->main->materials) { - ++mat_count; - } + /* Check number of materials & textures. */ + const int mat_count = BLI_listbase_count(&bfile->main->materials); EXPECT_EQ(mat_count, expect_mat_count); + + const int ima_count = BLI_listbase_count(&bfile->main->images); + EXPECT_EQ(ima_count, expect_image_count); } }; @@ -306,7 +307,45 @@ TEST_F(obj_importer_test, import_materials) {"OBCube", OB_MESH, 8, 12, 6, 24, float3(1, 1, -1), float3(-1, 1, 1)}, {"OBmaterials", OB_MESH, 8, 12, 6, 24, float3(-1, -1, 1), float3(1, -1, -1)}, }; - import_and_check("materials.obj", expect, std::size(expect), 4); + import_and_check("materials.obj", expect, std::size(expect), 4, 1); +} + +TEST_F(obj_importer_test, import_cubes_with_textures_rel) +{ + Expectation expect[] = { + {"OBCube", OB_MESH, 8, 12, 6, 24, float3(1, 1, -1), float3(-1, 1, 1)}, + {"OBCube4Tex", + OB_MESH, + 8, + 12, + 6, + 24, + float3(1, 1, -1), + float3(-1, -1, 1), + float3(0, 1, 0), + float2(0.9935f, 0.0020f)}, + {"OBCubeTiledTex", + OB_MESH, + 8, + 12, + 6, + 24, + float3(4, 1, -1), + float3(2, -1, 1), + float3(0, 1, 0), + float2(0.9935f, 0.0020f)}, + {"OBCubeTiledTexFromAnotherFolder", + OB_MESH, + 8, + 12, + 6, + 24, + float3(7, 1, -1), + float3(5, -1, 1), + float3(0, 1, 0), + float2(0.9935f, 0.0020f)}, + }; + import_and_check("cubes_with_textures_rel.obj", expect, std::size(expect), 3, 4); } TEST_F(obj_importer_test, import_faces_invalid_or_with_holes) -- cgit v1.2.3