I want to make a pie menu to snap selected parts of my mesh to the cursor constrained to specified directions. Example of snapping to cursor along x: enter image description here

I would like to do this for x,y,z,xy,zx,yx. I don't know much scripting but I assumed that just subtracting locations would do the trick:

import bpy
import bmesh
import math
import mathutils

obj = bpy.context.edit_object
me = obj.data

bm = bmesh.from_edit_mesh(me)
cursor_location = bpy.context.scene.cursor.location

for v in bm.verts:
    v.co = cursor_location - v.co

bmesh.update_edit_mesh(me, True)

I'm probably doing something very wrong here, so I'd like some help if possible.

  • $\begingroup$ How to make it in graphic mode $\endgroup$ – kyb Jun 3 at 16:22

I seem to have figured it out by cobbling together a bunch of solutions.

import bpy
from mathutils import Vector

ob = bpy.context.object
me = ob.data
verts_sel = [v.co for v in me.vertices if v.select]
pivot = sum(verts_sel, Vector()) / len(verts_sel)
c_location = bpy.context.scene.cursor.location
m_location = ob.matrix_world @ pivot

#get get the offset
l = c_location - m_location

#split the tuple
xdir = l[0]
ydir = l[1]
zdir = l[2]

#move the mesh in x to cursor
bpy.ops.transform.translate(value=(xdir, 0, 0), constraint_axis=(False, False, True), orient_matrix_type='GLOBAL')
| improve this answer | |

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.