Regression: OBJ import: Clamp Bounding Box does not work #122256

Open
opened 2024-05-25 13:24:34 +02:00 by Scurest · 6 comments
Contributor

System Information
Operating system: Linux-6.8.9-arch1-2-x86_64-with-glibc2.39 64 Bits, X11 UI
Graphics card: Mesa Intel(R) HD Graphics 520 (SKL GT2) Intel 4.6 (Core Profile) Mesa 24.0.6-arch1.2

Blender Version
Broken: 3.6 - 4.2
Worked: 3.6 with OBJ Legacy importer

Short description of error
The "Clamp Bounding Box" OBJ import option does not seem to work.

Exact steps for others to reproduce the error
Import this OBJ file

o Plane
v -1000 0 1000
v 1000 0 1000
v -1000 0 -1000
v 1000 0 -1000
s 0
f 1 2 4 3

In 3.6, using the legacy Python importer and setting the Clamp Size option to 2, the object has the expected scale of (0.001, 0.001, 0.001).

In 3.6 - 4.2, using the new OBJ importer and setting Clamp Bounding Box to 2, the OBJ has the default scale (1, 1, 1). The option had no effect.

**System Information** Operating system: Linux-6.8.9-arch1-2-x86_64-with-glibc2.39 64 Bits, X11 UI Graphics card: Mesa Intel(R) HD Graphics 520 (SKL GT2) Intel 4.6 (Core Profile) Mesa 24.0.6-arch1.2 **Blender Version** Broken: 3.6 - 4.2 Worked: 3.6 with OBJ Legacy importer **Short description of error** The "Clamp Bounding Box" OBJ import option does not seem to work. **Exact steps for others to reproduce the error** Import this OBJ file ``` o Plane v -1000 0 1000 v 1000 0 1000 v -1000 0 -1000 v 1000 0 -1000 s 0 f 1 2 4 3 ``` In 3.6, using the legacy Python importer and setting the _Clamp Size_ option to 2, the object has the expected scale of (0.001, 0.001, 0.001). In 3.6 - 4.2, using the new OBJ importer and setting _Clamp Bounding Box_ to 2, the OBJ has the default scale (1, 1, 1). The option had no effect.
Scurest added the
Severity
Normal
Type
Report
Status
Needs Triage
labels 2024-05-25 13:24:34 +02:00
Contributor

Can confirm, exporting a cube of size 4 and reimporting with different clamp_size still results in cube of same dimensions.

import bpy

file = bpy.app.tempdir + "cube4m.obj"
import_scales = [0,1, 2]

def clear_scene():
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete(use_global=False)
    bpy.ops.outliner.orphans_purge()

clear_scene()

bpy.ops.mesh.primitive_cube_add(size=4)
bpy.ops.wm.obj_export(filepath=file, global_scale=1.0, export_materials=False, path_mode='AUTO')

clear_scene()

for clamp_size in import_scales:
    bpy.ops.wm.obj_import(filepath=file, clamp_size=clamp_size)

Just tested with latest 4fc8a72780 on win10
This is what i get for the cube bounds in

https://projects.blender.org/blender/blender/src/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc#L107

Mesh bounds min: (1.07229e-13, 8.98232e-43, 1.30249e-08)
Mesh bounds max: (3.47522e-43, 0, 0)
Max diff: -8.98232e-43

Not excatly what it should be.

Can confirm, exporting a cube of size 4 and reimporting with different clamp_size still results in cube of same dimensions. ```python import bpy file = bpy.app.tempdir + "cube4m.obj" import_scales = [0,1, 2] def clear_scene(): bpy.ops.object.select_all(action='SELECT') bpy.ops.object.delete(use_global=False) bpy.ops.outliner.orphans_purge() clear_scene() bpy.ops.mesh.primitive_cube_add(size=4) bpy.ops.wm.obj_export(filepath=file, global_scale=1.0, export_materials=False, path_mode='AUTO') clear_scene() for clamp_size in import_scales: bpy.ops.wm.obj_import(filepath=file, clamp_size=clamp_size) ``` Just tested with latest 4fc8a7278048ebdcc6d4fbdc0e7999112bbbab83 on win10 This is what i get for the cube bounds in https://projects.blender.org/blender/blender/src/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc#L107 Mesh bounds min: (1.07229e-13, 8.98232e-43, 1.30249e-08) Mesh bounds max: (3.47522e-43, 0, 0) Max diff: -8.98232e-43 Not excatly what it should be.
Iliya Katushenock changed title from OBJ import: Clamp Bounding Box does not work to Regression: OBJ import: Clamp Bounding Box does not work 2024-05-26 18:50:38 +02:00

Confirmed. The data returned from mesh->bounds_min_max is invalid at this point. If you call it after BKE_mesh_nomain_to_mesh it will work.

Confirmed. The data returned from `mesh->bounds_min_max` is invalid at this point. If you call it after `BKE_mesh_nomain_to_mesh` it will work.
Contributor

Confirmed. The data returned from mesh->bounds_min_max is invalid at this point. If you call it after BKE_mesh_nomain_to_mesh it will work.

I was going mad, then realized the BKE_mesh_nomain_to_mesh was just called before the transform_object.

Now, i think the math is wrong:

    float scale = 1.0f;
    while (import_params.clamp_size < max_diff * scale) {
      scale = scale / 10;
    }

This is the same loop as in the 3.6 python importer, but i believe this would be the correct way of setting the scale

    float scale = import_params.clamp_size / max_diff;

Using this and importing the 4m cube i get

clamp size result scale result dimensions
0 1 4m
1 0.25 1m
2 0.5 2m

which seems correct.

Should i make 2 different pull request?
One for the bounds and one for the calculation?

> Confirmed. The data returned from `mesh->bounds_min_max` is invalid at this point. If you call it after `BKE_mesh_nomain_to_mesh` it will work. I was going mad, then realized the `BKE_mesh_nomain_to_mesh` was just called before the `transform_object`. Now, i think the math is wrong: ```c float scale = 1.0f; while (import_params.clamp_size < max_diff * scale) { scale = scale / 10; } ``` This is the same loop as in the 3.6 python importer, but i believe this would be the correct way of setting the scale ```c float scale = import_params.clamp_size / max_diff; ``` Using this and importing the 4m cube i get | clamp size | result scale | result dimensions| | :---: | :---: | :---: | | 0 | 1 | 4m | | 1 | 0.25 | 1m | | 2 | 0.5 | 2m | which seems correct. Should i make 2 different pull request? One for the bounds and one for the calculation?
Author
Contributor

The intention of the calculation is obviously to scale down by the first power of ten that makes it fit in the box. That is, (a) it only scales down or not at all, never up (scale <= 1), and (b) it only scales by powers of ten (1, 0.1, 0.01, etc).

import_params.clamp_size / max_diff would change both behaviors. It would scale either up or down to make the object fit the box exactly.

(a) is expected from the tooltip ("Resize the objects to keep bounding box under this value"). I did not expect (b). Whether (a) and (b) are desirable is a separate question from this bug though. They are obviously intentional and long-standing behaviors.

(I actually think there is a more serious problem with the calculation, which is that the scale appears to be set from each object's bounding box individually, which will distort the relative position of different objects if they are assigned different scales. The Python importer correctly determines a single scale from all the objects together and gives them all that scale. This is just from looking at code though, I can't test it until this bug is fixed.)

The intention of the calculation is obviously to scale down by the first power of ten that makes it fit in the box. That is, (a) it only scales down or not at all, never up (scale <= 1), and (b) it only scales by powers of ten (1, 0.1, 0.01, etc). `import_params.clamp_size / max_diff` would change both behaviors. It would scale either up or down to make the object fit the box exactly. (a) is expected from the tooltip ("Resize the objects to keep bounding box under this value"). I did not expect (b). Whether (a) and (b) are desirable is a separate question from this bug though. They are obviously intentional and long-standing behaviors. (I actually think there is a more serious problem with the calculation, which is that the scale appears to be set from each object's bounding box _individually_, which will distort the relative position of different objects if they are assigned different scales. The Python importer correctly determines a single scale from _all_ the objects together and gives them all that scale. This is just from looking at code though, I can't test it until this bug is fixed.)
Contributor

The intention of the calculation is obviously to scale down by the first power of ten that makes it fit in the box. That is, (a) it only scales down or not at all, never up (scale <= 1), and (b) it only scales by powers of ten (1, 0.1, 0.01, etc).

import_params.clamp_size / max_diff would change both behaviors. It would scale either up or down to make the object fit the box exactly.

(a) is expected from the tooltip ("Resize the objects to keep bounding box under this value"). I did not expect (b). Whether (a) and (b) are desirable is a separate question from this bug though. They are obviously intentional and long-standing behaviors.

(I actually think there is a more serious problem with the calculation, which is that the scale appears to be set from each object's bounding box individually, which will distort the relative position of different objects if they are assigned different scales. The Python importer correctly determines a single scale from all the objects together and gives them all that scale. This is just from looking at code though, I can't test it until this bug is fixed.)

That is absolutely right.
I just sent a pull request for fixing the bound values, you may now try to use that patch.

On the behaviour of the clamp_size itself i would argue that it would make more sense to use it to make the object fit exactly the dimension desired.
I don't think there's a similar behaviour anywhere else in blender where you could get a 1 order of magnitude of error as output.

But maybe opening a new issue for just that would problably be better.

> The intention of the calculation is obviously to scale down by the first power of ten that makes it fit in the box. That is, (a) it only scales down or not at all, never up (scale <= 1), and (b) it only scales by powers of ten (1, 0.1, 0.01, etc). > > `import_params.clamp_size / max_diff` would change both behaviors. It would scale either up or down to make the object fit the box exactly. > > (a) is expected from the tooltip ("Resize the objects to keep bounding box under this value"). I did not expect (b). Whether (a) and (b) are desirable is a separate question from this bug though. They are obviously intentional and long-standing behaviors. > > (I actually think there is a more serious problem with the calculation, which is that the scale appears to be set from each object's bounding box _individually_, which will distort the relative position of different objects if they are assigned different scales. The Python importer correctly determines a single scale from _all_ the objects together and gives them all that scale. This is just from looking at code though, I can't test it until this bug is fixed.) That is absolutely right. I just sent a pull request for fixing the bound values, you may now try to use that patch. On the behaviour of the `clamp_size` itself i would argue that it would make more sense to use it to make the object fit exactly the dimension desired. I don't think there's a similar behaviour anywhere else in blender where you could get a 1 order of magnitude of error as output. But maybe opening a new issue for just that would problably be better.
Bart van der Braak added
Type
Bug
and removed
Type
Report
labels 2024-08-14 13:00:19 +02:00
Author
Contributor

Since a fix is not forthcoming, here is a script I am using after import as a workaround. It scales both up or down to fit the target cube size.

import bpy
from mathutils import Vector

# Scale so AABB fits in a cube this size
target_size = 10

obs = bpy.context.selected_objects
if obs:
    bounds = [ob.matrix_world @ Vector(bound) for ob in obs for bound in ob.bound_box]
    maxs = [max(bound[i] for bound in bounds) for i in range(3)]
    mins = [min(bound[i] for bound in bounds) for i in range(3)]
    max_axis = max(maxs[i] - mins[i] for i in range(3))
    for ob in obs:
        ob.scale *= target_size / max_axis
Since a fix is not forthcoming, here is a script I am using after import as a workaround. It scales both up or down to fit the target cube size. ```py import bpy from mathutils import Vector # Scale so AABB fits in a cube this size target_size = 10 obs = bpy.context.selected_objects if obs: bounds = [ob.matrix_world @ Vector(bound) for ob in obs for bound in ob.bound_box] maxs = [max(bound[i] for bound in bounds) for i in range(3)] mins = [min(bound[i] for bound in bounds) for i in range(3)] max_axis = max(maxs[i] - mins[i] for i in range(3)) for ob in obs: ob.scale *= target_size / max_axis ```
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#122256
No description provided.