Unusual python selection problem, need help

Good day all,

I’m writing a simple script for an import function that creates an object and parents it to an armature, but I’m having a problem.
The issue is in vertex / face weighting.

The script successfully creates vertex groups for the statically named bones of the armature, and if I go to the vertex groups at the end of the import, i can press SELECT in the mesh data panel, and the vertices get selected as expected.

The problem lies here though: the script selection of the vertices does not select the ‘attached’ faces, as would instead happen when selecting the vertices manually.

I have reproduced this problem by ‘feeding’ the script with the standard cube (triangulated), and the following script:

import bpy
import bmesh

bpy.context.tool_settings.mesh_select_mode = (True , False , False)
if hasattr(mesh.verts, 'ensure_lookup_table'): mesh.verts.ensure_lookup_table()


As the image shows, selecting 5, 6 and 7 manually will highlight the relevant face. Doing the same with the above code, the face is not selected. It’s a couple of days I’m stuck with this already, and I fear I’m not looking in the right place so help if you can.

What do i need to do to select the face once I select the vertices? I use vertex selection because I can’t know the face index in advance.




Not very elegant, but simply appending

bpy.context.tool_settings.mesh_select_mode = (True , False , True)
bpy.context.tool_settings.mesh_select_mode = (True , False , False)

to the end of the script will do it.

slack-jawed lol! What’s the rationale behind this? It works actually. I reckon it’s related to the data update from changing mode?
Thanks a ton anyhow!

have a look at bm.select_mode:

and also: