1

Some preamble:

enter image description here

In a recent answer on blender stackexchange selected UV verts based on their coordinates.

The image shows a simple mesh with 9 vertices and 4 faces. A meshes "loops" is the verts in each face. So for this mesh it has 16 loops. A UV is associated with a loop.

import bpy
import numpy as np

ob = bpy.context.object
me = ob.data
uv_layer = me.uv_layers.active
# get uv values
uvs = np.empty((2 * len(me.loops), 1))
uv_layer.data.foreach_get("uv", uvs)
# select 
u, v = uvs.reshape((-1, 2)).T
uv_layer.data.foreach_set(
        "select",
         np.logical_and(
            (u >= 0) & (u <= 1),
            (v >= 0) & (v <= 1)
            )
        )

End preamble

For example sake, the selected uv coords. (in loop order)

>>> uvlayer.data.foreach_get("select", uvselect)
>>> uvselect
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)

The vertex index associated with the loop. The middle vertex above is in 4 times (once for each face), there will be 4 with 2 entries and the four outer corners with 1. (again in loop order)

>>> me.loops.foreach_get("vertex_index", loop_vert_index)
>>> loop_vert_index
array([8, 6, 3, 7, 4, 8, 7, 2, 0, 5, 8, 4, 5, 1, 6, 8], dtype=int32)

The current selection of the 9 vertices.

>>> me.vertices.foreach_get("select", vert_select)
>>> vert_select
array([0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int32)

The question: how can I take the uv selection uvselect, mapped with the loop_vert_index to create the vert_select if any of the loops associated with a vert is selected?. eg middle vert (index 8) selected if any of loops 0, 5, 10 or 15 are selected), in a "numpy" way.

EDIT.

Have found a way using the method outlined in Sum rows where value equal in column to get, via approach 2 of this answer if for example all loops were selected

vert_select = [1 1 1 1 2 2 2 2 4]

which can be used to select the mesh vertices. (Equiv of select vert if any loop is selected).

batFINGER
  • 484
  • 2
  • 10
  • Please do not edit the question to include the answer; post an answer instead (and then accept it, so that it can be useful for others in the future). – desertnaut Mar 08 '21 at 15:08
  • 2
    Cheers for the prod. The question edit was in more a step towards answer. Often finding myself prodding same on BSE, least I didn't put SOLVED in the title lol. In as much as I've answered, such a novice wrt numpy that always feel there is a "better approach". – batFINGER Mar 08 '21 at 15:36

1 Answers1

0

Numpy.logical_or.at

Putting forward as an answer method outlined in https://stackoverflow.com/a/30042202/5317130 however unique was not required in this case.

logical_or.at will set True for any vert that has at least one of its loops selected. Similarly with add.at Use of logical_and.at will require all loops selected for vert selection.

import numpy as np
import bpy

ob = bpy.context.object
me = ob.data

uvlayer = me.uv_layers.active
A = np.empty((2, len(me.loops)), dtype=np.int32)
me.loops.foreach_get("vertex_index", A[0])
uvlayer.data.foreach_get("select", A[1])

vert_sel = np.zeros(len(me.vertices), dtype=np.int32)

np.logical_or.at(vert_sel, *A)
print(vert_sel.T)
me.vertices.foreach_set("select", vert_sel)
batFINGER
  • 484
  • 2
  • 10