Fix #141434: OBJ importer should apply bounds clamp size to the whole scene

It was applying the bounds clamp to each object individually,
which changed spatial relationships between objects. Make the logic
match what used to be done in the Python OBJ importer back in the day.

Pull Request: https://projects.blender.org/blender/blender/pulls/141446
This commit is contained in:
Aras Pranckevicius
2025-07-04 10:36:18 +02:00
committed by Aras Pranckevicius
parent ab9f03555c
commit 641824753a
5 changed files with 89 additions and 11 deletions

View File

@@ -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<const Mesh *>(object->data);
const Bounds<float3> 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)

View File

@@ -8,8 +8,10 @@
#include <string>
#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<float3>> 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) {

View File

@@ -0,0 +1,3 @@
{
"clamp_size": 1.5
}

BIN
tests/files/io_tests/obj/cubes_clamp_15.obj (Stored with Git LFS) Normal file

Binary file not shown.

View File

@@ -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