1
$\begingroup$

by script I can do a snap-to-grid of the selected vertices:

bpy.context.space_data.grid_scale = 0.125
bpy.ops.view3d.snap_selected_to_grid()

But I need to restrain it to one axis ... but how?

Many thanks for any hints.

$\endgroup$

1 Answer 1

1
$\begingroup$

Ditch the operator

IMO far simpler to ditch the operator and round the desired axis component to the nearest gradation.

Test script, Run in edit mode, moves all selected verts local z coordinate to nearest multiple of 0.125

import bpy
import bmesh

context = bpy.context

ob = context.edit_object
me = ob.data
bm = bmesh.from_edit_mesh(me)

grid_spacing = 0.125

sel_verts = [v for v in bm.verts if v.select]

for v in sel_verts:
    v.co.z = round(v.co.z / grid_spacing) * grid_spacing

bmesh.update_edit_mesh(me)

Considering the grid to be a global system, will convert the vert coordinate to global mw @ v.co find the nearest gradation point, then convert back to local mwi @ v.co

Please Note script written for and tested on 2.8. For prior replace @ with *

import bpy
import bmesh


context = bpy.context
ob = context.edit_object
mw = ob.matrix_world
mwi = mw.inverted()
me = ob.data
bm = bmesh.from_edit_mesh(me)

grid_spacing = 0.125

sel_verts = [v for v in bm.verts if v.select]

for v in sel_verts:
    g = mw @ v.co # use * prior 2.8
    g.z = round(g.z / grid_spacing) * grid_spacing
    v.co = mwi @ g

bmesh.update_edit_mesh(me)
$\endgroup$
1
  • $\begingroup$ thanks for the hint, batFINGER. I had so chance to test it yet but I found my own workarount by simply looping over all selected verts and storing their x,y position and after applying the snap to grid, restore them. $\endgroup$
    – 32Smooth
    Commented Jan 4, 2019 at 16:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .