Select Vertices by Location on Axis

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?

(Sorry, cruddy title.)

in object-mode for global coordinates (for local: delete the matrix-part):

import bpy
obj = bpy.context.object
mesh = obj.data
verts = mesh.vertices

for v in verts:
    if (obj.matrix_world  * v.co)[0] == 0.002 and (obj.matrix_world *  v.co)[2] == 0.45:
        v.select = True
    else:
        v.select = False

Hi caleb1h,


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.

i changed the code in my previous post. i unindentionally switched the matrix multiplication to the wrong side of the logical operator.

strange that it worked…

@niko You still need to change it to


(obj.matrix_world  * v.co)[0]

otherwise it’s a matrix times a scalar which gives


>>> co
Vector((1.0842021724855044e-19, 1.8583666367494303e-28, 0.0))

>>> mw
Matrix(((1.0, 0.0, 0.0, 0.6637389063835144),
        (0.0, 1.0, 0.0, 2.613856315612793),
        (0.0, 0.0, 1.0, 2.608768939971924),
        (0.0, 0.0, 0.0, 1.0)))

>>> mw * co[2]
Matrix(((0.0, 0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0, 0.0)))

yep… thx

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()

Whoops, haven’t had time to test yet. Will later today. Thank you to everybody who responded. More and more I think this community is great.