# foreach_set - Doesn't “False” toggle selection off?

I have some verticies selected (for example vtx[0] from total 9), but try to select some others using "foreach_set". So, I create an array (...here with 8 times "False", except vtx[8] is "True") and "throw" it on the mesh:

oa = bpy.context.active_object
bpy.ops.object.mode_set(mode='OBJECT')

sl = np.zeros(9, dtype=np.int32)
sl[8] = 1

### added after @lemon comment ###
setE = np.zeros(len(oa.data.edges), dtype=np.bool)
setF = np.zeros(len(oa.data.polygons), dtype=np.bool)
oa.data.polygons.foreach_set("select", setF.ravel())
oa.data.edges.foreach_set("select", setE.ravel())
##################################

oa.data.vertices.foreach_set("select", sl.ravel())
oa.data.update()

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


I end up with vtx[0] and [8] selected. Shouldn't "False" deselect the first vertex?

• Meshes have 3 levels of selection: vertex, edge and face. Ensure that all are selected as you want. oa.data.edges.foreach, oa.data.polygons.foreach... as by default all are selected. – lemon Mar 9 '20 at 19:46
• I supplemented the code. Unfortunately it doesn't help. – Luis Mar 13 '20 at 12:47
• Is the test object a cube? If yes should sl[7]=1 (not 8) – lemon Mar 13 '20 at 12:48
• I usually test on a sphere. To simplify the example for this post I divided a plane. – Luis Mar 13 '20 at 14:45
• To me the script as it is works. (but can be simplified eventually) – lemon Mar 13 '20 at 14:55

## 1 Answer

This seems to work:

oa = bpy.context.active_object
bpy.ops.object.mode_set(mode='OBJECT')

sl = np.zeros(9, dtype=np.int32)
sl[8] = 1

setV = np.zeros(len(oa.data.vertices), dtype=np.bool)
setE = np.zeros(len(oa.data.edges), dtype=np.bool)
setF = np.zeros(len(oa.data.polygons), dtype=np.bool)

oa.data.polygons.foreach_set("select", setF.ravel())
oa.data.edges.foreach_set("select", setE.ravel())
oa.data.vertices.foreach_set("select", setV.ravel())
oa.data.update()

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

oa.data.vertices.foreach_set("select", sl.ravel())
oa.data.update()

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


Maybe this code could be simplified?

• about simplification you can foreach_set("select", [False] * len(ao.data.polygons) ) – lemon Mar 13 '20 at 17:25