diff --git a/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc b/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc index 996fa071619..65f2875c4de 100644 --- a/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc +++ b/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc @@ -100,17 +100,6 @@ void transform_object(Object *object, const OBJImportParams &import_params) import_params.global_scale, import_params.global_scale, import_params.global_scale}; rescale_m4(obmat, scale_vec); BKE_object_apply_mat4(object, obmat, true, false); - - if (import_params.clamp_size > 0.0f) { - BLI_assert(object->type == OB_MESH); - const Mesh *mesh = static_cast(object->data); - const Bounds bounds = *mesh->bounds_min_max(); - const float max_diff = math::reduce_max(bounds.max - bounds.min); - if (import_params.clamp_size < max_diff * import_params.global_scale) { - const float scale = import_params.clamp_size / max_diff; - copy_v3_fl(object->scale, scale); - } - } } std::string get_geometry_name(const std::string &full_name, char separator) diff --git a/source/blender/io/wavefront_obj/importer/obj_importer.cc b/source/blender/io/wavefront_obj/importer/obj_importer.cc index 9197cd5dc15..5c2940f29ed 100644 --- a/source/blender/io/wavefront_obj/importer/obj_importer.cc +++ b/source/blender/io/wavefront_obj/importer/obj_importer.cc @@ -8,8 +8,10 @@ #include +#include "BLI_bounds.hh" #include "BLI_listbase.h" #include "BLI_map.hh" +#include "BLI_math_vector.h" #include "BLI_set.hh" #include "BLI_sort.hh" #include "BLI_string.h" @@ -20,6 +22,7 @@ #include "BKE_geometry_set.hh" #include "BKE_instances.hh" #include "BKE_layer.hh" +#include "BKE_object.hh" #include "DEG_depsgraph_build.hh" @@ -149,6 +152,23 @@ static void geometry_to_blender_objects(Main *bmain, } } + /* Clamp object size if needed. */ + if (import_params.clamp_size > 0.0f) { + std::optional> bounds = std::nullopt; + for (Object *obj : objects) { + bounds = blender::bounds::merge(bounds, BKE_object_boundbox_get(obj)); + } + if (bounds.has_value()) { + const float max_diff = math::reduce_max(bounds->max - bounds->min); + if (import_params.clamp_size < max_diff * import_params.global_scale) { + const float scale = import_params.clamp_size / max_diff; + for (Object *obj : objects) { + copy_v3_fl(obj->scale, scale); + } + } + } + } + /* Do object selections in a separate loop (allows just one view layer sync). */ BKE_view_layer_synced_ensure(scene, view_layer); for (Object *obj : objects) { diff --git a/tests/files/io_tests/obj/cubes_clamp_15.json b/tests/files/io_tests/obj/cubes_clamp_15.json new file mode 100644 index 00000000000..44ab497c52e --- /dev/null +++ b/tests/files/io_tests/obj/cubes_clamp_15.json @@ -0,0 +1,3 @@ +{ + "clamp_size": 1.5 +} diff --git a/tests/files/io_tests/obj/cubes_clamp_15.obj b/tests/files/io_tests/obj/cubes_clamp_15.obj new file mode 100644 index 00000000000..37aae7735f3 --- /dev/null +++ b/tests/files/io_tests/obj/cubes_clamp_15.obj @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:72bbf515e18c9b05c919590dfa16beee6a2098b57c5152dcef61f7770fbcf7a5 +size 592 diff --git a/tests/files/io_tests/obj/reference/cubes_clamp_15.txt b/tests/files/io_tests/obj/reference/cubes_clamp_15.txt new file mode 100644 index 00000000000..c8a98a098c8 --- /dev/null +++ b/tests/files/io_tests/obj/reference/cubes_clamp_15.txt @@ -0,0 +1,63 @@ +==== Meshes: 3 +- Mesh 'Big_Cube' vtx:8 face:6 loop:24 edge:12 + - 0 1 3 2 2 ... 0 7 3 1 5 + - 0/2 0/1 1/3 2/3 2/6 ... 4/6 5/7 4/5 0/4 1/5 + - attr 'position' FLOAT_VECTOR POINT + - (-6.200, -6.200, -4.200) + - (-6.200, 6.200, -4.200) + - (-6.200, -6.200, -16.600) + ... + - (6.200, 6.200, -4.200) + - (6.200, -6.200, -16.600) + - (6.200, 6.200, -16.600) + - attr 'material_index' INT FACE + - 0 0 0 0 0 0 + - attr 'sharp_face' BOOLEAN FACE + - 1 1 1 1 1 1 + +- Mesh 'Little_Cube' vtx:8 face:6 loop:24 edge:12 + - 0 1 3 2 2 ... 0 7 3 1 5 + - 0/2 0/1 1/3 2/3 2/6 ... 4/6 5/7 4/5 0/4 1/5 + - attr 'position' FLOAT_VECTOR POINT + - (-1.000, -1.000, 1.000) + - (-1.000, 1.000, 1.000) + - (-1.000, -1.000, -1.000) + ... + - (1.000, 1.000, 1.000) + - (1.000, -1.000, -1.000) + - (1.000, 1.000, -1.000) + - attr 'material_index' INT FACE + - 0 0 0 0 0 0 + - attr 'sharp_face' BOOLEAN FACE + - 1 1 1 1 1 1 + +- Mesh 'Tiny_Cube' vtx:8 face:6 loop:24 edge:12 + - 0 1 3 2 2 ... 0 7 3 1 5 + - 0/2 0/1 1/3 2/3 2/6 ... 4/6 5/7 4/5 0/4 1/5 + - attr 'position' FLOAT_VECTOR POINT + - (-0.200, -0.200, 2.200) + - (-0.200, 0.200, 2.200) + - (-0.200, -0.200, 1.800) + ... + - (0.200, 0.200, 2.200) + - (0.200, -0.200, 1.800) + - (0.200, 0.200, 1.800) + - attr 'material_index' INT FACE + - 0 0 0 0 0 0 + - attr 'sharp_face' BOOLEAN FACE + - 1 1 1 1 1 1 + +==== Objects: 3 +- Obj 'Big_Cube' MESH data:'Big_Cube' + - pos 0.000, 0.000, 0.000 + - rot 1.571, 0.000, 0.000 (XYZ) + - scl 0.080, 0.080, 0.080 +- Obj 'Little_Cube' MESH data:'Little_Cube' + - pos 0.000, 0.000, 0.000 + - rot 1.571, 0.000, 0.000 (XYZ) + - scl 0.080, 0.080, 0.080 +- Obj 'Tiny_Cube' MESH data:'Tiny_Cube' + - pos 0.000, 0.000, 0.000 + - rot 1.571, 0.000, 0.000 (XYZ) + - scl 0.080, 0.080, 0.080 +