0
$\begingroup$

In Blender2.79,the script below calculates the selected vertices and stores them in a list.
As there were issues with calculating these vertices directly on a mesh instance ,the object's mesh had to be converted to a "bmesh" instance.

When the script is run it works fine in the "First run".

But in the subsequent runs it gives the following error- AttributeError: 'bytes' object has no attribute 'verts'

Also,on updating the bmesh with bmesh.update_edit_mesh(me)results in this error- ReferenceError: BMesh data of type BMesh has been removed

How to resolve these bmesh errors?

#-------------------------MAIN SCRIPT--------------------------
#Select vertices of default cube in Edit mode

from os import system
system("cls")

import bpy 
import bmesh

scene = bpy.context.scene
ob = scene.objects.active
vert = ob.data.vertices

#Set default mode == Edit 
if bpy.ops.object.mode_set.poll():
    bpy.ops.object.mode_set(mode='EDIT')

print("Active object = ",ob.name)
print("")

#Create Bmesh instance and calculate vertices 
print("Vert_selected")

me = ob.data
bm = bmesh.new()
bm = bmesh.from_edit_mesh(me) 

vert_selected = [v.co for v in bm.verts if v.select] #AttributeError: 'bytes' object has no attribute 'verts'   
for vs_s in vert_selected :
    print(vs_s)

bpy.ops.mesh.select_all(action='DESELECT')
bm.select_flush(True)


#bm.bmesh.update_edit_mesh(me) #If mode ==Object  -> ReferenceError: BMesh data of type BMesh has been removed (Uncommenting results in Blender crash)
bm.free() 
ob.update_from_editmode()

print("Script exited")
$\endgroup$
6
  • 1
    $\begingroup$ Recommend Don't access Mesh.vertices in edit mode. use edit mode bmesh for this. Can update the mesh from editmode with context.object.update_from_editmode() If using foreach get it returns a list of values eg [True, False, True, False] . The list needs to be correct size.v_selected = [False] * len(verts) and is populated (not resized) by the call. Use if v.select: no need to check against True $\endgroup$ – batFINGER May 18 '20 at 19:41
  • $\begingroup$ On using the Bmesh module on a existing object , only to check for selected vertices gives an error refer- blender.stackexchange.com/questions/178938/… .However , on creating a new vert on a selected mesh using Bmesh module works fine .Should Bmesh be only used when creating new verts ? $\endgroup$ – starzar May 19 '20 at 15:14
  • $\begingroup$ No Re the refer, I would accept the given answer. There is a big difference between a bmesh instance and the bmesh module. There are countless examples of using edit mode bmesh to select or read selection. Once again accessing Mesh.vertices in editmode is not the way to go. Blender makes a "mesh copy" to edit in edit mode, then writes back on mode toggle. An edit mode bmesh is bound to the edit mesh. $\endgroup$ – batFINGER May 19 '20 at 15:53
  • $\begingroup$ I have already implemented the answer in my other question and am getting the errors(refer attached image).Already used edit_mode_mesh(),it does give the selected vertices but it also gives the following error;- AttributeError: 'BMesh' object has no attribute 'bmesh' on update_edit_mesh().The Bmesh is set free() after this update command .Is there any way to override this error? $\endgroup$ – starzar May 20 '20 at 3:47
  • $\begingroup$ I have posted the issue's with using Bmesh in the above question. $\endgroup$ – starzar May 20 '20 at 6:48
0
$\begingroup$

Since there was no change in the actual mesh,updating the mesh with update_edit_mesh() was not required.Also setting object context directly to "Edit"mode as opposed to a context mode_set poll() works.

Code can be found here.

$\endgroup$

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.