0
$\begingroup$

I am looking for a python script to select all the vertices on negative side of axis. I want to add these to a vertex group so I can use them in my code. Any help is appreciated.

$\endgroup$
1
  • $\begingroup$ I found a code that select vertices on both side of 1 axis givn by batFINGER. But I need to select all vertex on one side of an axis like on negative x or on negative y. blender.stackexchange.com/questions/139172/… $\endgroup$
    – Sam316911
    Commented Nov 25, 2022 at 8:16

1 Answer 1

1
$\begingroup$

Using one of the answers from the link you pointed at (the first answer looked simpler to me), we can write a simple script that selects the vertices that are in one side of one of the axes.

The things you need to note are :

  • some_object.data.vertices gives you access to the vertices of some_object
  • some_vertex.co is a 3 dimensional Vector that gives you the coordinates of some_vertex. some_vertex.co[0] is the X coordinate, some_vertex.co[1] is the Y and some_vertex.co[2] is for the Z.

Once you know that, all you need to do is test for each vertex if the coordinate along the desired axis is greater than 0 (or less than 0) and select the vertex in question with some_vertex.select= True :

import bpy

obj= bpy.context.object
bpy.ops.object.mode_set(mode= 'OBJECT')
verts= obj.data.vertices

def select_one_side_axis_verts(axis= 'x', positive= True):

    #deselect all the vertices
    bpy.ops.object.mode_set(mode= 'EDIT')
    bpy.ops.mesh.select_all(action= 'DESELECT')
    bpy.ops.object.mode_set(mode= 'OBJECT')

    axis_index= 0

    #match case for the
    match axis:
        case 'x' | 'X':
            axis_index= 0
        case 'y' | 'Y':
            axis_index= 1
        case 'z' | 'Z':
            axis_index= 2
        case _:
            axis_index= 0
        
    #selecting the vertices    
    for v in verts:
        if positive and (v.co[axis_index] > 0) or not positive and (v.co[axis_index] < 0):
            v.select= True


#test run : select all the vertices that have negative z coordinates
select_one_side_axis_verts(axis= 'Z', positive= False)

This selects all the vertices that have a z coordinate strictly less than 0, if you want 0 to be included, just replace if positive and (v.co[axis_index] > 0) or not positive and (v.co[axis_index] < 0) by if positive and (v.co[axis_index] >= 0) or not positive and (v.co[axis_index] <= 0).

$\endgroup$
4
  • $\begingroup$ just a small remark: if you lowercase the axis before the match, you don't need the 3 additional or's for the cases ;) $\endgroup$
    – Chris
    Commented Nov 25, 2022 at 11:24
  • $\begingroup$ Yes, that makes sense, thank you :D $\endgroup$ Commented Nov 25, 2022 at 11:26
  • 1
    $\begingroup$ you are welcome ;) just to mention: the +1 upvote is from me because i think your answer is good anyway. $\endgroup$
    – Chris
    Commented Nov 25, 2022 at 11:29
  • $\begingroup$ Thank you for that too. $\endgroup$ Commented Nov 25, 2022 at 11:31

You must log in to answer this question.

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