BVH overlap() failing to detect intersection due to bmesh rotation not updating until script ends #50721

Closed
opened 2017-02-19 03:20:32 +01:00 by Valera Barashkov · 10 comments

System Information
Windows 10
NVIDIA GeForce GTX 460M

Blender Version
Blender 2.78,a,b

Short description of error
overlap() failing to detect intersection of objects that have been rotated, however it succeeds when running independently (scripts included) on the same objects.

Exact steps for others to reproduce the error
I have included a .blend file with two scripts, first one makes copies of objects on one of the selected planes. The overlap() function fails when a copy of an object is rotated. However, when running the second script with exact same overlap detection it works fine. Please see the blend file it includes more instructions for reproducing the error.

In summary: bmesh location updates, but bmesh rotation does not until after the script ends.

intersect_bug.blend

intersect_bug.py

testing_bug_intersect.py

**System Information** Windows 10 NVIDIA GeForce GTX 460M **Blender Version** Blender 2.78,a,b **Short description of error** overlap() failing to detect intersection of objects that have been rotated, however it succeeds when running independently (scripts included) on the same objects. **Exact steps for others to reproduce the error** I have included a .blend file with two scripts, first one makes copies of objects on one of the selected planes. The overlap() function fails when a copy of an object is rotated. However, when running the second script with exact same overlap detection it works fine. Please see the blend file it includes more instructions for reproducing the error. In summary: bmesh location updates, but bmesh rotation does not until after the script ends. [intersect_bug.blend](https://archive.blender.org/developer/F489388/intersect_bug.blend) [intersect_bug.py](https://archive.blender.org/developer/F489387/intersect_bug.py) [testing_bug_intersect.py](https://archive.blender.org/developer/F489386/testing_bug_intersect.py)

Changed status to: 'Open'

Changed status to: 'Open'

Added subscriber: @valera-8

Added subscriber: @valera-8

Added subscriber: @mano-wii

Added subscriber: @mano-wii

Mathutils.BVHTree does not take into account the matrix when creating a bvhtree. It is as if bvhtree were using the local coordinates of the object.
That's why in raycasts or nearest, the ray or point need to be transformed with the inverse matrix.
For overlap you need to create the BVHTree with the object coordinates already transformed.
It is not practical to analyze possible errors in scripts that do not come with blender, you need to be more specific with the problem.
You can ask questions in the blenderartists.org forum to make sure it is a bug.

Mathutils.BVHTree does not take into account the matrix when creating a bvhtree. It is as if bvhtree were using the local coordinates of the object. That's why in raycasts or nearest, the ray or point need to be transformed with the inverse matrix. For overlap you need to create the BVHTree with the object coordinates already transformed. It is not practical to analyze possible errors in scripts that do not come with blender, you need to be more specific with the problem. You can ask questions in the blenderartists.org forum to make sure it is a bug.

In #50721#418449, @mano-wii wrote:
It is not practical to analyze possible errors in scripts that do not come with blender, you need to be more specific with the problem.

Germano, please see the .blend files, scripts are included and test scene is setup with extra instructions.

For overlap you need to create the BVHTree with the object coordinates already transformed.

That is exactly how the script works:

  1. Make copy of an object
  2. Change rotation and location
  3. Make bmesh
  4. Make BVHtree
  5. Check for intersection with the existing objects.

The overlap() test works as long as the objects are not rotated.

> In #50721#418449, @mano-wii wrote: > It is not practical to analyze possible errors in scripts that do not come with blender, you need to be more specific with the problem. Germano, please see the .blend files, scripts are included and test scene is setup with extra instructions. > For overlap you need to create the BVHTree with the object coordinates already transformed. That is exactly how the script works: 1) Make copy of an object 2) Change rotation and location 3) Make bmesh 4) Make BVHtree 5) Check for intersection with the existing objects. The overlap() test works as long as the objects are not rotated.

I've tested your script and it seems to be working...
I also tested this script with rotated objects:

from mathutils.bvhtree import BVHTree
import bpy, bmesh

ob1 = bpy.data.objects['Plane']
ob2 = bpy.data.objects['Plane.001']

bm = bmesh.new()
bm.from_mesh(ob1.data)
bm.transform(ob1.matrix_world)
bvh1 = BVHTree.FromBMesh(bm)
bm.free()

bm = bmesh.new()
bm.from_mesh(ob2.data)
bm.transform(ob2.matrix_world)
bvh2 = BVHTree.FromBMesh(bm)
bm.free()

inter = bvh1.overlap(bvh2)

edit_object = bpy.context.edit_object
if edit_object in {ob1, ob2}:
    i = edit_object == ob2
    bm = bmesh.from_edit_mesh(edit_object.data)
    for f in inter:
        bm.faces[f[i]].select_set(True)

    bmesh.update_edit_mesh(edit_object.data)

del bvh1, bvh2

Sem título.png
As you can see, the overlap is working.
Did I miss something?

I've tested your script and it seems to be working... I also tested this script with rotated objects: ``` from mathutils.bvhtree import BVHTree import bpy, bmesh ob1 = bpy.data.objects['Plane'] ob2 = bpy.data.objects['Plane.001'] bm = bmesh.new() bm.from_mesh(ob1.data) bm.transform(ob1.matrix_world) bvh1 = BVHTree.FromBMesh(bm) bm.free() bm = bmesh.new() bm.from_mesh(ob2.data) bm.transform(ob2.matrix_world) bvh2 = BVHTree.FromBMesh(bm) bm.free() inter = bvh1.overlap(bvh2) edit_object = bpy.context.edit_object if edit_object in {ob1, ob2}: i = edit_object == ob2 bm = bmesh.from_edit_mesh(edit_object.data) for f in inter: bm.faces[f[i]].select_set(True) bmesh.update_edit_mesh(edit_object.data) del bvh1, bvh2 ``` ![Sem título.png](https://archive.blender.org/developer/F493381/Sem_título.png) As you can see, the overlap is working. Did I miss something?

Your case worked because the objects already exist in the scene, I assume. If the objects already exist overalp() works for me as well as demonstrated in testing_intersect.py

Take a look at the image below after I run the script to place the objects:

Screenshot from 2017-02-21 19-19-25.png

2 - as you can see spheres failed the overlap test
1 - In this case 4 spheres should have been placed since there is room for 4, unless objects have not been rotated (1 and 3 have the same length in x, y)

I should have been more clear that:
testing_intersect.py only used to prove that the objects intersect while using the same intersection testing code as intersect_bug.py

intersect_bug.py is the one one failing to detect intersection of rotated objects

Your case worked because the objects already exist in the scene, I assume. If the objects already exist overalp() works for me as well as demonstrated in testing_intersect.py Take a look at the image below after I run the script to place the objects: ![Screenshot from 2017-02-21 19-19-25.png](https://archive.blender.org/developer/F493419/Screenshot_from_2017-02-21_19-19-25.png) 2 - as you can see spheres failed the overlap test 1 - In this case 4 spheres should have been placed since there is room for 4, unless objects have not been rotated (1 and 3 have the same length in x, y) I should have been more clear that: testing_intersect.py only used to prove that the objects intersect while using the same intersection testing code as intersect_bug.py intersect_bug.py is the one one failing to detect intersection of rotated objects

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Germano Cavalcante self-assigned this 2017-02-22 02:24:21 +01:00

I took a quick look at your script and... this is not a bug.
You are changing the quaternion rotation of an object expecting its matrix to be automatically updated within the script.

Instead of doing this, try changing the matrix directly

A good place to ask questions like this is here ;)
https://blenderartists.org/forum/forumdisplay.php?11-Python-Support

I took a quick look at your script and... this is not a bug. You are changing the quaternion rotation of an object expecting its matrix to be automatically updated within the script. Instead of doing this, try changing the matrix directly A good place to ask questions like this is here ;) https://blenderartists.org/forum/forumdisplay.php?11-Python-Support

Thanks! I was so convinced this was a bug, should have check the forums or stack overflow first..

Thanks! I was so convinced this was a bug, should have check the forums or stack overflow first..
Sign in to join this conversation.
No Milestone
No project
2 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-addons#50721
No description provided.