2
$\begingroup$

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
    print(v.co)

bmesh.update_edit_mesh(me, True)

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

$\endgroup$
1
$\begingroup$

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

import bpy
from mathutils import Vector

ob = bpy.context.object
ob.update_from_editmode()
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')
$\endgroup$

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.