# How to know what object the snap function is targeting (in Python)?

When using the snapping function during a translation in a script, is there a way to know which object, vertex, edge, or face was used as the snapping target?

• Please correct me if I'm wrong (and roll back my edit), but it seems you are asking how to do this programmatically, correct? – A Wild RolandiXor Aug 24 '13 at 16:18
• Yes, I would like the snap function to return the object name with face/vertex index that it used for the transform operation. – matali Aug 25 '13 at 10:02

The problem here is that snapping is an interactive operation, so to what is snapped to is known inside of the transform operator somwhow. But operators don't expose their internal working data, and bpy.context.active_operator.properties doesn't contain enough to find out (in particular if closest snapping is used, it may move your object to the same location for different snapping locations).

You can find the snapping co easily for snap to active, but it won't tell you which object nor which geometry element it used.

Would a scripted snapping modal operator help? Probably much slower than the built-in snapping, but that's probably the only way to know what is snapped to (requires a lot of final bmeshes in memory I guess).

• Sounds to get in the right directions, but how could launching the translate operator in modal mode help to get the target? The goal would be to have a "stick" function which would parent or add a constraint to make the snapped object stick to it's target after user chooses the point/face/edge. – matali Sep 28 '13 at 17:18
• So the workflow for the end user would be: "Pick" (snap) a vertex/edge/face of an object, which is not selected/active. Immediately after that, parent the active object to the object which was used for snapping (or add a constraint to the active ob). Did I get that right? Is there another final step, which moves the active object? – CodeManX Sep 28 '13 at 17:39
• so the work-flow would be intuitive: 1) Grab (G) object A (to be sticked) 2) choose the point/edge/face (from object B) to stick the object too (left click, snapping on) 3) The script take the object (B) the user snapped to make parenting (then moving B will move A as if it was sticked too it) – matali Sep 28 '13 at 18:22
• I see, it's basically a translation with snapping enabled + parenting the snapped object to the snapping object - in one go. A is grabbed and moved, B stays, but will afterwards move together if A is moved. Sounds good, but we really don't know to which object we snapped A. I can only think of a brute-force approach: granted A's center was moved to snapping location (not the case with CLOSEST!), ray_cast the scene using start = ob.location and `end = ob.location +/- threshold in 6 directions until an object is hit and isn't A itself. Did a quick test in console and seems possible. – CodeManX Sep 28 '13 at 19:23
• Maybe if the involved C code changes is small enough (make translate/snap function just return the snap target), it would spare a lot of workaround ? I have no clue about C, but I guess returning a value is not that hard ? If you get it reliable in python I'm ok, but maybe asking the devs would be easier ? – matali Sep 28 '13 at 19:51

Yes. You can know.

It largely depends on the viewport you're in or the user view you're looking from. I think it doesn't give any visual indication as to where it'll be going next or sort but when it snaps to a face, edge, and or vertex, it happens visually for you to see immediately

• The question is related to Python, not to using the viewport. – A Wild RolandiXor Aug 24 '13 at 16:16
• As RolandiXor said, it's a python question. But thanks for taking time to answer Rexford. – matali Aug 25 '13 at 10:04