Can't deselect vertex in this way... why not?

I can’t tell if this is a bug.

The blend file contains two scripts side by side for easy comparison. The scripts should do opposite things.

The left script selects a random vert and moves it up. This works correctly.

The right script should DESELECT a random vert and move it down, but it doesn’t deselect it, it just moves it down.

Could someone tell me why? Thanks!


select_deselect.blend (485 KB)

it’s an API limitation, you can’t deselect vertices. Using bmesh module it’s possible however:

import bpy
from random import choice
import bmesh
'''This script is supposed to DEselect a random vert and move it down, but doesn't work.'''

#This mesh starts with all of its verts selected...
obj =['start selected']

#make sure the right mesh is the active one...
for o in = False = obj = True
if obj.mode != 'EDIT':
bm = bmesh.from_edit_mesh(
    random_vert = choice([vert for vert in bm.verts if])
except IndexError:
else:[2] -= .5

made choice() safer and added if to list comprehension to move selected verts only