4

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 ?

George Profenza
  • 50,687
  • 19
  • 144
  • 218
user3052391
  • 105
  • 2
  • 10

1 Answers1

6

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.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
  • Since 2.73 you might have to add `if hasattr(mesh.verts, "ensure_lookup_table"): mesh.verts.ensure_lookup_table()` before accessing the BMesh verts to avoid getting an `IndexError`. – dwitvliet Apr 30 '15 at 15:58