self intersection detection?

I need a way to detect self intersection in my script. I know that the 3d printing toolbox can do it, but I can’t seem to find the source code. does anyone know where I can find it? does anyone know of any code somewhere that can be used to detect self intersection?

***[SOLVED]:

a HUGE thank you to patmo141

the object must be in edit mode

import bpy
import bmesh
import mathutils

def bmesh_check_self_intersect_object(obj):
    """
    Check if any faces self intersect


    returns an array of edge index values.
    """
    import bpy


    if not obj.data.polygons:
        return array.array('i', ())


    bm = bmesh.from_edit_mesh(obj.data)



    import mathutils
    tree = mathutils.bvhtree.BVHTree.FromBMesh(bm, epsilon=0.00001)


    overlap = tree.overlap(tree)
    intersecting_edge_indexes = {i for i_pair in overlap for i in i_pair}
    
    return intersecting_edge_indexes


object = bpy.context.scene.objects.active
iei = bmesh_check_self_intersect_object(object)
print(iei)

I could do it with raycasting, but I think you can only do that in the game engine. is there a way to raycast outside of the game engine?

  1. There are several options for ray casting outside bge. Search the wiki for Scene.ray_cast, Object.ray_cast and BVHTree.ray_cast

  2. The self intersection code from 3d print toolbox is with Blender

  3. Related
    http://blender.stackexchange.com/questions/9073/how-to-check-if-two-meshes-intersect-in-python/9080#9080

Blender -> 2.7x -> scripts -> addons -> object_print3d_utils

mesh_helpers.py


def bmesh_check_self_intersect_object(obj):
    """
    Check if any faces self intersect


    returns an array of edge index values.
    """
    import bpy


    if not obj.data.polygons:
        return array.array('i', ())


    bm = bmesh_copy_from_object(obj, transform=False, triangulate=False)


    import mathutils
    tree = mathutils.bvhtree.BVHTree.FromBMesh(bm, epsilon=0.00001)


    overlap = tree.overlap(tree)
    faces_error = {i for i_pair in overlap for i in i_pair}
    return array.array('i', faces_error)

OMG THANK YOU! you just saved me a tone of wheel reinventing. I will add a comment in my code thanking you, unless you don’t want me to.

Just a note, the code here was written by Campbell Barton and is in the Blender source with the 3d print toolbox addon. So no need to credit me, unless you just want to credit me pointing you in the right direction :slight_smile:

-P