I am trying to understand accessing object data from Python, in particular vertex data. In this simple test:
Make a new Blender document
In the scripting interface, make a function to display vertex information (as suggested here):
def showvertices(): for item in bpy.data.objects: print(item.name) if item.type == 'MESH': for vertex in item.data.vertices: print(vertex.co)
Now, typing in
showvertices()
in the Python console seems to display the correct information for the default cube:Camera Cube <Vector (-1.0000, -1.0000, -1.0000)> <Vector (-1.0000, -1.0000, 1.0000)> <Vector (-1.0000, 1.0000, -1.0000)> <Vector (-1.0000, 1.0000, 1.0000)> <Vector (1.0000, -1.0000, -1.0000)> <Vector (1.0000, -1.0000, 1.0000)> <Vector (1.0000, 1.0000, -1.0000)> <Vector (1.0000, 1.0000, 1.0000)> Light
So far, so good. Now type:
- Tab -> enter edit mode
- A -> select all
- GX2 (Enter) -> move all vertices 2 units in the X direction
This appears to work, the cube moves to the right (from the front view) and the X coordinate of the top-right vertex is shown as 3m instead of 1m as it was before:
However, typing in showvertices()
in the Python console still displays the original information, not the updated positions:
Camera
Cube
<Vector (-1.0000, -1.0000, -1.0000)>
<Vector (-1.0000, -1.0000, 1.0000)>
<Vector (-1.0000, 1.0000, -1.0000)>
<Vector (-1.0000, 1.0000, 1.0000)>
<Vector (1.0000, -1.0000, -1.0000)>
<Vector (1.0000, -1.0000, 1.0000)>
<Vector (1.0000, 1.0000, -1.0000)>
<Vector (1.0000, 1.0000, 1.0000)>
Light
Now if I press Tab to go back to Object mode and type in showvertices()
again in the console, the vertex information is now updated:
Camera
Cube
<Vector (1.0000, -1.0000, -1.0000)>
<Vector (1.0000, -1.0000, 1.0000)>
<Vector (1.0000, 1.0000, -1.0000)>
<Vector (1.0000, 1.0000, 1.0000)>
<Vector (3.0000, -1.0000, -1.0000)>
<Vector (3.0000, -1.0000, 1.0000)>
<Vector (3.0000, 1.0000, -1.0000)>
<Vector (3.0000, 1.0000, 1.0000)>
Light
So my question is, why does the information reported by the Python function not show the new positions until I leave edit mode? Is there a cached copy of the vertex information, and the "real" data is not updated until exiting edit mode?
This was with Blender 2.80.57, however the same behaviour was observed in version 2.79.