2
$\begingroup$

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:

Cube after moving

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.

$\endgroup$
2
$\begingroup$

I think you just need to call the update_from_editmode () method of the object. https://docs.blender.org/api/2.79/bpy.types.Object.html?highlight=update_from#bpy.types.Object.update_from_editmode

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Of note: update_from_editmode will update the objects mesh data to match the edit mesh, this only needs to be done once when exiting edit mode. You can also use bmesh.from_edit_edit_mesh() to access the edit mesh while in edit mode and not have to repeatedly update the mesh data. $\endgroup$ – sambler Apr 28 '19 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.