Selected vertex did not highlight in Blender 3D

3.3k views Asked by At

I've made a Cube in Blender. Using Python I did enter EDIT mode and selected one vertex:

import bpy

bpy.ops.mesh.primitive_cube_add()
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="DESELECT")
bpy.context.tool_settings.mesh_select_mode = (True , False , False)
bpy.context.object.data.vertices[0].select = True

bpy.context.object.data.vertices[0].co = (-3,-2,-3)

However, the vertex was not highlighted in orange, and although I told the vertex to go to -3,-2-,-3 it's position did not update.

Why did it not highlight nor move ?

1

There are 1 answers

1
Yann Vernier On

While in edit mode, the editor handles a mirror of the mesh, which is then saved as the object's data once you leave edit mode. Your script meanwhile alters the underlying original mesh, which isn't being displayed. Leaving editmode stores the edit mesh, so the scripted alterations don't show up at all.

One way to work around this is to do the scripted changes outside of edit mode:

import bpy

bpy.ops.mesh.primitive_cube_add()
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="DESELECT")
bpy.context.tool_settings.mesh_select_mode = (True , False , False)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.context.object.data.vertices[0].select = True
bpy.context.object.data.vertices[0].co = (-3,-2,-3)
bpy.ops.object.mode_set(mode="EDIT")

Another is to request the editing BMesh:

import bpy, bmesh

bpy.ops.mesh.primitive_cube_add()
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="DESELECT")
bpy.context.tool_settings.mesh_select_mode = (True , False , False)
mesh=bmesh.from_edit_mesh(bpy.context.object.data)
mesh.verts[0].select = True
mesh.verts[0].co = (-3,-2,-3)

This is a documented gotcha of Blender's scripting interface.