From 94323bb427480aaaeea52f6833173866fbacf2e1 Mon Sep 17 00:00:00 2001 From: Aras Pranckevicius Date: Wed, 6 Jul 2022 13:29:59 +0300 Subject: IO: speed up import of large Alembic/USD/OBJ scenes by optimizing material assignment The importer parts that were doing assignment of materials to the imported objects/meshes were essentially having a quadratic complexity in terms of scene object count. For each material assigned to each object, they were scanning the whole scene, checking which other Objects use the same Mesh data, in order to resize their material arrays to match the size. Performance details (Windows, Ryzen 5950X): - Import OBJ Blender 3.0 splash scene (24k objects): 43.0s -> 32.9s - Import USD Disney Moana scene (260k objects): saves two hours (~7400s). Note that later on this crashes when trying to render the imported result; crashes in the same way/place both in master and this patch. Implementation details: The importers were doing "scan the world" basically twice for each object, for each material: once when creating a new material slot (assigns an empty material), and then again when assigning the material. However, all these importers (USD, Alembic, OBJ) always create one Object for one Mesh. So that whole quadratic complexity resulting from "scan the world for possible other users of this obdata" is completely not needed; it just never finds anything. So add a new dedicated function BKE_object_material_assign_single_obdata that skips the expensive part, but should only be used when the caller knows that the obdata has exactly one user (the passed object). Reviewed By: Bastien Montagne, Michael Kowalski Differential Revision: https://developer.blender.org/D15145 --- source/blender/io/alembic/intern/abc_reader_mesh.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'source/blender/io/alembic/intern/abc_reader_mesh.cc') diff --git a/source/blender/io/alembic/intern/abc_reader_mesh.cc b/source/blender/io/alembic/intern/abc_reader_mesh.cc index d8c48357fc0..df3559c108c 100644 --- a/source/blender/io/alembic/intern/abc_reader_mesh.cc +++ b/source/blender/io/alembic/intern/abc_reader_mesh.cc @@ -77,10 +77,8 @@ static void assign_materials(Main *bmain, const std::map &mat_index_map) { std::map::const_iterator it; - for (it = mat_index_map.begin(); it != mat_index_map.end(); ++it) { - if (!BKE_object_material_slot_add(bmain, ob)) { - return; - } + if (mat_index_map.size() > MAXMAT) { + return; } std::map matname_to_material = build_material_map(bmain); @@ -100,7 +98,7 @@ static void assign_materials(Main *bmain, assigned_mat = mat_iter->second; } - BKE_object_material_assign(bmain, ob, assigned_mat, mat_index, BKE_MAT_ASSIGN_OBDATA); + BKE_object_material_assign_single_obdata(bmain, ob, assigned_mat, mat_index); } } -- cgit v1.2.3