Mesh Data: Not Updating in Edit Mode

Hi, I’ve been working on a script where the following function may be run in or out of edit mode, but I’m finding that when I change the mesh geometry in edit mode the list returned by this function does not update unless I toggle into object mode first.

def findVerts(ob):
    "Return a list of xyz coordinates for each vertex"
    list = []
    for v in ob.data.vertices:
        list.append([v.co[0],v.co[1],v.co[2]])
    return list

Does anyone know a way to fix this, besides having the script toggle in and out of object mode automatically? Thanks.

P.S. I’m using Blender2.58 on Windows 64 bit.

To my knowledge you are forced to use going out and in edit-mode … ;-(
I came across this too … (2.58 and W32)

Right now to fix the problem I now have the following 2 lines:

bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()

The problems here is if you’re in object mode it’s unnecessary and if for some reason your in a mode other than edit or object mode you’ll end up in object mode… but at least it always works (I think).

Is there a way to get the current mode? (Sorry dumb question but can’t find it)

1 Like

Yes, e.g.
bpy.context.active_object.mode

or any (mesh) object, which can be edited

I prefer to use mode_set():

bpy.ops.object.mode_set(mode='OBJECT') 
bpy.ops.object.mode_set(mode='EDIT') 

Thank you both for your help! – Dragonrider

I use them like this:


    m=bpy.context.mode
    if bpy.context.mode!='OBJECT':
        bpy.ops.object.mode_set(mode='OBJECT')

then…


    #   RE-SET MODE
    if bpy.context.mode!=m:
        bpy.ops.object.mode_set(mode=m)

1 Like

Right now I just did the following.

currentMode = bpy.context.object.mode
bpy.ops.object.mode_set() #default is mode='OBJECT'
bpy.ops.object.mode_set(mode=currentMode)

I might be wrong, but it seemed more efficient just to always switch to object mode then to use two if statements (let me know if I’m wrong). Actually I don’t know why I’m worrying about efficiency at all.

Also, in this case the mesh data doesn’t need to be retrieved in object mode. It does needs to be updated by going to object mode apparently. (I would like to just have an update command, which is why I started the thread).

With and if statement (maybe):

m=bpy.context.mode
if bpy.context.mode!='OBJECT':
     bpy.ops.object.mode_set(mode='OBJECT')
     bpy.ops.object.mode_set(mode=m)

I was looking at this earlier, but no joy, because what I was trying I needed to be in Object mode anyway…

http://www.blender.org/documentation/blender_python_api_2_58a_release/bpy.types.Scene.html?highlight=scene.update#bpy.types.Scene.update

Maybe your test might get better use out of it… bpy.context.scene.update()

Unfortunately, bpy.context.scene.update() doesn’t seem to be working as a substitute, but thanks for the suggestion.

I was wondering the same thing, and asked on Blender Stack Exchange.

The solution appears to be to call item.update_from_editmode (), for example:

if bpy.context.mode=='EDIT_MESH':
    item.update_from_editmode ()

… where “item” is the object in question.