# Why deselecting an already selected vertex does NOT work?

I have found out that while it is possible to select vertices by setting select to True, it is not possible to deselect vertices by setting select to False if the vertices are already selected.
Sounds silly but it is not.

To demonstrate this, open a new file with the default cube, switch to Edit mode (all vertices should be selected by default).
Then run this script:

import bpy

bpy.ops.object.mode_set(mode="OBJECT")

bpy.data.meshes['Cube'].vertices[0].select = False

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


The vertex 0 should be deselected, right? Well, it is not.

Now, deselect the top 4 vertices on the Cube and run this:

import bpy

bpy.ops.object.mode_set(mode="OBJECT")

bpy.data.meshes['Cube'].vertices[0].select = False
bpy.data.meshes['Cube'].vertices[5].select = True

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


The vertex 5 will be a new selected one, as it should, but vertex 0 will still be selected but it should not be.

More info: if the select property's value is checked while still in Object mode, it will be False. However, switching to Edit mode makes it back to True.

Why is deselecting vertices not working? Am I missing something?

Actually, yes, you are missing something ;)

The fact is, when you select all vertices of an edge or face, you also select said edge or face (same when you select all edges of a face). In other words, selection of verts, edges and faces are linked together, so if you want to deselect vertices you must first deselect all faces and edges they belong to.

Note that selection operator(s) in editmode take care of that, using current select mode as reference (such that in vertices select mode, deselecting a vertex automatically propagates to relevant edges and faces).

• Thanks, but this happens also when only a single vertex is selected - just deselect all except vertex 0 and run my example code and you will still see the same effect. Selecting single vertex does not select any faces. Is there more to it after all? – spacer May 16 '16 at 10:42
• Cannot confirm that here… Selected a single vert, switched back to Object mode, unselected that vert in py, switched back to Edit mode, vert was no more selected… – mont29 May 16 '16 at 15:31
• I just started the test from scratch and the single vertex deselect now works. Now even the test file works. I'm buffled. How was I able to reproduce it without problems for the last 5 days!? Maybe it is some "memory effect" when several complex files are loaded and reloaded. I'll see if it appears again. Now I'm accepting the answer as it is essentially right. Thanks! – spacer May 16 '16 at 16:08

I found this question because I'm facing the same problem, looks like when vertices are already selected, before running script, for some weird reason, you cannot deselect them in context, you have to go to Edit mode, and Deselect from there, then toggle back to Object mode if working in context:

import bpy

bpy.ops.object.mode_set(mode="EDIT") #Activating Edit mode
bpy.ops.mesh.select_all(action = 'DESELECT') #Deselecting all
bpy.ops.object.mode_set(mode="OBJECT") #Going back to Object mode

• Very useful. Exactly what I'm looking fo. Hat off! – Sibbs Gambling Mar 20 '17 at 4:40