2
$\begingroup$

I would like to select only the vertices on the left side of my plane using Python (like on the screeenshot), does anybody have an idea how to do it ?

I try the following to get all vertices but i'm unable to isolate those on the left.

enter image description here

My actual code to get all the vertices:

bpy.ops.import_image.to_plane(
  files=[{'name': os.path.basename(file)}],
  directory=os.path.dirname(file)
)
obj = bpy.context.object
group = obj.vertex_groups.new(name = 'Group' + str(number))
index = 0
verts = []
for v in obj.data.vertices:
    verts += [index]
    index = index + 1
    print(v.co)
group.add(verts, 1.0, 'REPLACE')
$\endgroup$
1
$\begingroup$

You need to somehow define what is 'on the left side'. Is it global space, is it local, which way you are looking from?.. Anyway, you can compare the coordinates to something. Let's say you are looking from the top as in the screenshot and left is the most negative X point of the object in global space plus 0.5 units:

import bpy

obj = bpy.context.object
data = obj.data

the_matrix = obj.matrix_world

on_the_left_side = (the_matrix @ obj.data.vertices[0].co).x # have to start somewhere

for v in data.vertices:
    co = the_matrix @ v.co #Let's say "on the left" is in global space
    if co.x < on_the_left_side:
        on_the_left_side = co.x

offset = 0.5

#Element selection is a bit weird in bpy - you can select faces, 
#but not vertices for example like user cannot, so let's deselect
for e in data.edges:
    e.select = False
for f in data.polygons:
    f.select = False    

for v in data.vertices:
    co = the_matrix @ v.co
    if co.x < on_the_left_side + offset:
        v.select = True
    else:
        v.select = False
$\endgroup$
4
  • $\begingroup$ I wasn't very clear about the space indeed. It was local space i think. I tried your script and it works well to get my selection. Thank you very much. I would like to put those selected vertices in a new vertex group (group.add(verts, 1.0, 'REPLACE') in my previous code snippets. Where can i put these part in your code ? $\endgroup$ Apr 15 '20 at 8:25
  • $\begingroup$ Read: blender.stackexchange.com/a/145975/31447 @NicolasSouthside If this solved your issue please accept the answer. How this site works: blender.stackexchange.com/tour $\endgroup$
    – brockmann
    Apr 15 '20 at 8:28
  • 1
    $\begingroup$ @NicolasSouthside add this line bpy.ops.object.mode_set(mode = 'OBJECT') and this line group.add([v.index for v in data.vertices if v.select], 1.0, 'REPLACE') at the end of the script. Don't forget the line from your answer where you create the vertex group. $\endgroup$
    – Gorgious
    Apr 15 '20 at 9:41
  • $\begingroup$ You can always suggest an edit @Gorgious $\endgroup$
    – brockmann
    Apr 15 '20 at 9:47

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.