Draw a custom mesh on a model

Hi!

Since two weeks, I’m trying to draw a custom mesh on a mesh to send it to my python script. Do to so, I draw sphere where I click and translate the global coordinate to local coordinate from the pickedMesh. But my custom mesh is built weirdly and if I go on an another face, the custom mesh goes thought the mesh. i’m kinda blocked since one week on it.

What I want : Imgur: The magic of the Internet

If you always connect the clicked vertices by a line, this will inevitably happen. To avoid the created mesh going through the inside of the mesh, you will have to detect whenever two vertices are in different faces and put an additional vertex at the position where the line between two vertices crosses an edge, like this diagram:
image

I’m almost done, but how do I detect if my triangle cross an edge ? How do I get the intersect line ?

These are geometry problems, I recommend checking out ray triangle intersections.