Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

April 03, 2014

bmesh utilities testing :) nothing to see here.

Moves the cursor if there's an intersection, else prints message.
import bpy
import bmesh
from mathutils.geometry import intersect_line_line as LineIntersect

class CAD_prefs:
    VTX_PRECISION = 1.0e-5
    VTX_DOUBLES_THRSHLD = 0.0001

def coords_tuple_from_edge_idx(bm, idx):
    ''' bm is a bmesh representation '''
    return tuple(v.co for v in bm.edges[idx].verts)

def get_intersection_from_idxs(bm, idx1, ixd2):
    '''
    > takes reference to bm and 2 indices
    < returns intersection or None
    '''
    p1, p2 = coords_tuple_from_edge_idx(bm, idx1)
    p3, p4 = coords_tuple_from_edge_idx(bm, idx2)
    a, b = LineIntersect(p1, p2, p3, p4)
    if (a-b).length < CAD_prefs.VTX_PRECISION:
        return a
 
obj = bpy.context.active_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
me.update()

edges = [e for e in bm.edges if e.select]
idx1, idx2 = [edge.index for edge in edges]
pt = get_intersection_from_idxs(bm, idx1, idx2)
if pt:
    print(pt)
    bpy.context.scene.cursor_location = pt
else:
    print("no intersection")    

No comments:

Post a Comment

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.