1
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – lemon
    Commented Mar 9, 2020 at 19:46
  • $\begingroup$ I supplemented the code. Unfortunately it doesn't help. $\endgroup$
    – Luis
    Commented Mar 13, 2020 at 12:47
  • $\begingroup$ Is the test object a cube? If yes should sl[7]=1 (not 8) $\endgroup$
    – lemon
    Commented Mar 13, 2020 at 12:48
  • $\begingroup$ I usually test on a sphere. To simplify the example for this post I divided a plane. $\endgroup$
    – Luis
    Commented Mar 13, 2020 at 14:45
  • $\begingroup$ To me the script as it is works. (but can be simplified eventually) $\endgroup$
    – lemon
    Commented Mar 13, 2020 at 14:55

1 Answer 1

1
$\begingroup$

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?

$\endgroup$
1
  • 1
    $\begingroup$ about simplification you can foreach_set("select", [False] * len(ao.data.polygons) ) $\endgroup$
    – lemon
    Commented Mar 13, 2020 at 17:25

You must log in to answer this question.

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