B2.54 Get selected vertices or update mesh vertice list

Hi there,
inside my script i am using the knife_cut function given by blender (bpy.ops.mesh.knife_cut). After cutting the mesh, all newly generated vertices are selected, but still not in the meshes vertice list.
e.g. i cut the default cube through the middle, using following code inside the console:

>>> len(bpy.data.meshes["Cube"].vertices)
8
>>> bpy.ops.mesh.select_all(action='SELECT')
{'FINISHED'}
>>> bpy.ops.mesh.knife_cut(type='EXACT', path=[{..."loc":(0,250)...}, {..."loc":(500,250)...}]...)
{'FINISHED'}
>>> len(bpy.data.meshes["Cube"].vertices)
8
>>> bpy.ops.object.editmode_toggle()
{'FINISHED'}
>>> bpy.ops.object.editmode_toggle()
{'FINISHED'}
>>> len(bpy.data.meshes["Cube"].vertices)
 16

It would be fine to update the vertice list of the mesh just by toggling out of editmode and back inside again, but this works only inside the console, inside a running script the script would finish first even with toggling between object and editmode, after finishing the script the vertice list would be updated.

As i need the list of newly generated vertices, i looked around for a function that really updates the list of vertices a mesh is holding or a function which gives me a list of all selected vertices (the knife cut tool unselects all old vertices and selects the generated ones), so that i could get the generated vertices just by looping through all selected verts.

Anybody has a function for updating the verts hold by a mesh or a function for getting the selected verts? Would be fine :slight_smile:

Here piece of the code.
mesh.data.vertices[0].select

Gorn: I’m not certain what your problem is, as the following code works fine for me in a script (so not inside the console).

bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.knife_cut(type='EXACT', path=[........], num_cuts=1, cursor=9)
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
updated_list = bpy.data.meshes["Cube"].vertices

The script doesn’t finish before the mesh is updated. The mesh data is updated as you go from edit-mode to object-mode (second time you toggle edit-mode).

No, won’t work anyway :frowning:
My script is very long and has to do several cuts, at least also the mesh will be deleted after i get all vertices and use them for some calculations, i am working with copies of objects so that the original mesh won’t change (it is faster copying the whole mesh and generating a new object for just one cut each time that cutting several times through the same mesh, the amount of generated vertices keep small)

I think trying to update the list with toggling between object mode and edit mode won’t help me at least.

@Darknet
thanks, but this doesn’t work either. For checking if the verts are selected, i have to access to the vertices, but if they aren’t inside the list, i will only get a traceback that the vertex doesn’t exist (even if it is there^^)

Try this one:
yourobject.update(bpy.context.scene)

Naturally replacing yourobject by the object you just have used the knife on.

Get it^^
I needed to do both, update the object and toggle one time between object and editmode, then the list of vertices is updated:


bpy.ops.mesh.knife_cut(...)

obj.update(bpy.context.scene)

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

This code is running well now, using a seperate copy of my object just for each cut and deleting the copy after i get the coords of the verts :slight_smile:

Thanks for help!