2
$\begingroup$

I have an existing selection which I'd like to further isolate using Python scripting. In this instance the math is simply that if the X coordinate is greater than zero, it should be deselected.

I already have this code, which successfully selects half of the object:

import bpy

obj = bpy.context.active_object
bpy.ops.object.mode_set(mode = 'EDIT') 
bpy.ops.mesh.select_mode(type="VERT")
bpy.ops.object.mode_set(mode = 'OBJECT')

for i in range(len(obj.data.vertices)):
    if obj.data.vertices[i].co.x > 0:
        obj.data.vertices[i].select = True

bpy.ops.object.mode_set(mode = 'EDIT')

If I change the > to a < and run the script, then it selects the rest of the object, so it does seem that selections are able to build upon each other.

If I change True to False, I would expect selected areas matching the logic (co.x > 0) to be deselected, since it is setting the select property to false. However, this doesn't actually work. Running the script with False instead of True results in the selection being unchanged regardless of any conditions I set. It seems that nothing happens when I use .select = False. There are no errors printed to the console or anything.

After finding this question, it seems that I'm not the only one with the problem, but the answers simply seem to indicate that this is impossible due to the way Blender works with selecting faces and edges. So, is it possible? Can we deselect all connected faces and edges of a vertex in a Python script so that we can deselect the vertex?

$\endgroup$

2 Answers 2

5
$\begingroup$

if you want to run it in object mode:

import bpy
vert = bpy.context.object.data.vertices
face = bpy.context.object.data.polygons #not faces!
edge = bpy.context.object.data.edges
#vertices can be selected 
#to deselect vertices you need to deselect faces(polygons) and edges at first
for i in face:                   
    i.select=False               
for i in edge:
    i.select=False
for i in vert:
    i.select=False
    if i.co.x > 0:
        i.select = True




$\endgroup$
2
  • $\begingroup$ Elaborate on the script pls. $\endgroup$ Nov 6, 2019 at 16:50
  • $\begingroup$ it works the same but in object mode $\endgroup$ Nov 9, 2019 at 23:06
3
$\begingroup$

Think you need to flush your selection.

This code works, slightly different from yours as it uses bmesh.from_edit_mesh, but if it gets the job done then maybe okay:

import bpy
import bmesh
context = bpy.context

obj = context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
bm.select_mode = {'VERT'}
for v in bm.verts:
    v.select = ( v.co.x > 0 )
bm.select_flush_mode()   
me.update()
$\endgroup$

You must log in to answer this question.

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