Well, I happen to be looking for a way to select all vertices on a given object, that fall under a specific location on an axis (e.g., all vertices that have a location of 0.002 on the X axis, all vertices that have a location of 0.45 on the z axis.) I have been unable to find a way to do so by UI or by script. How would I begin doing so?
import bpy
context = bpy.context
object = context.object
mesh = object.data
tol = 0.00001
val = 1.0
i = 2 plane z = 1.0
use_global = False
if not use_global:
verts_onplane = [v.index for v in mesh.vertices if abs(v.co[i]-val) < tol]
else:
mw = object.matrix_world
verts_onplane = [v.index for v in mesh.vertices if abs((mw * v.co)[i]-val) < tol]
print(verts_onplane)
This will give you a list of the index of the vertices that are on the plane, in this case z = 1 which for the default cube gives (4, 5, 6, 7) Now that this works ok I would convert it to a method that returns the list
import bpy
def verts_on_plane(object, offset, axis_index, use_global=False, tol=0.00001):
mesh = object.data
if not use_global:
verts = [v.index
for v in mesh.vertices
if abs(v.co[axis_index]- offset) < tol]
else:
mw = object.matrix_world
verts = [v.index
for v in mesh.vertices
if abs((mw * v.co)[axis_index] - offset) < tol]
return verts
context = bpy.context
object = context.object
verts = verts_on_plane(object, 1.0, 2)
print(verts)
# selecting them
for v in object.data.vertices:
v.select = v.index in verts
@niko looking at your code i noticed you are comparing a scalar and a matrix, which I’m surprised “works”
>>> C.object.matrix_world == 3
False
but probably doesn’t give the result you are after.
import bpy
ob = bpy.context.object
if ob and ob.type == 'MESH':
me = ob.data
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle()
for v in me.vertices:
if round((ob.matrix_world * v.co).z, 1) == -1.5:
v.select = True
bpy.ops.object.editmode_toggle()