BMesh Creation on specific side

Hi guys, here is a new 3d and Python problem for creating BMeshes. I clicked a face in 3d viewport, hold the mouse button still pressed, the face I got and saved after a raycast. When I hit a face, the operation starts

Now I move the mouse and want to know, at which side I am leaving the face, to create a new one.

Here my ideas:

  1. While moving the mouse raycast til I dont hit a face
  2. Then measure the distance to the edges of the last face that I hit.

I want to create a new face with about the same size as the one that I hit - as a first implementation I could extrude the edge by length of this edge.

Any other or better ideas? Or Python samples for the BMesh operations?

Thx and best regards!

While your idea seems to work, you probably won’t know the closest edge until you pass it.

Using the face from your last raycast, and assuming the cursor is within the face, you can calculate the distance from any of its edges using the function below. Then it’s a matter of sorting the closest edge.

def distance_point_line(pt, v1, v2):
    d = (v1 - v2).normalized()
    line_point = (pt - v1) @ d * d + v1
    length = (line_point - pt).length
    return line_point, length

# Assuming you have a list of the edges in your last raycast face
# with the elements (edge_index, edge_co1, edge_co2),
# where coordinates are Vector objects and not just tuples.
results = []
for index, v1, v2 in edges:
    point, length = distance_point_line(mouse_pos_3d, v1, v2)
    results.append((index, point, length))

# Closest edge to cursor
index, point, distance = min(results, key=lambda item: item[2])
1 Like

Thx a lot, this looks really good.