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?

  • $\begingroup$ Please correct me if I'm wrong (and roll back my edit), but it seems you are asking how to do this programmatically, correct? $\endgroup$ – A Wild RolandiXor Aug 24 '13 at 16:18
  • $\begingroup$ Yes, I would like the snap function to return the object name with face/vertex index that it used for the transform operation. $\endgroup$ – 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).

| improve this answer | |
  • $\begingroup$ 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. $\endgroup$ – matali Sep 28 '13 at 17:18
  • $\begingroup$ 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? $\endgroup$ – CodeManX Sep 28 '13 at 17:39
  • 1
    $\begingroup$ 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) $\endgroup$ – matali Sep 28 '13 at 18:22
  • $\begingroup$ 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. $\endgroup$ – CodeManX Sep 28 '13 at 19:23
  • $\begingroup$ 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 ? $\endgroup$ – 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.