0
$\begingroup$

In Blender2.79, the "Main Script" below converts an existing mesh(default_cube) to a "Bmesh" (using bmesh.from_mesh()) and stores the selected vertices of the mesh in a list.

On testing,this Bmesh example(create new vert on existing selected Mesh) works fine.

However,when run the MainScript in both Edit and Object(after calculating selected vertices in edit mode) modes ,the console gives this error.

AttributeError: module 'bmesh' has no attribute 'from__mesh'   

Since,the selected object has to be accessed as a part of the "active" scene, assigning object in this manner ,gives above error

scene = bpy.context.scene
ob = scene.objects.active
me = ob.data
bm = bmesh.new()
bm = bm.from_mesh(me)

Also direct context assignment gives above error.

ob = bpy.context.object
me = ob.data
bm = bmesh.new()
bm.from_mesh(me)   

[![Bmesh.from_mesh() error][2]][2]

However, "from_mesh" is a valid attribute in Bmesh module.

Even using bmesh.from_object() gives a similar error.

AttributeError: module 'bmesh' has no attribute 'from__object'   

Also,if bmesh.from_edit_mesh() is used , and even if the selected object is in Edit mode ,the console gives the following error(even if the Bmesh is set_free after this line)

ReferenceError: BMesh data of type BMesh has been removed 

How to run this script in EDIT mode using Bmesh module (without getting any errors)?

----------------------------------MAIN SCRIPT-------------------------------

from os import system
   system("cls")

import bpy 
import bmesh

#Assign object as a part of the active scene
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)

#Store selected vertices in a list
print("vert_selected")
#ob = bpy.context.object #Also direct context assignment raised no attribute error
me = ob.data
bm = bmesh.new()
bm = bm.from_mesh(me) #AttributeError: module 'bmesh' has no attribute 'from_mesh'
#bm.from__object(me) #AttributeError: module 'bmesh' has no attribute 'from__object'

vert_selected = [v.co for v in bm.verts if v.select]   
for vs_s in vert_selected:
    print(vs_s)


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

bpy.ops.object.mode_set(mode='OBJECT') # if bmesh.from_edit_mesh() --> mode == EDIT - ValueError: to_mesh(): Mesh 'Cube' is in editmode 

bm.to_mesh(me) #If mode ==Object  -> ReferenceError: BMesh data of type BMesh has been removed
bm.free() 
ob.update_from_editmode()
$\endgroup$

3 Answers 3

3
$\begingroup$

List selected edit mode vertices.

Run the script with a mesh in edit mode. Prints an index list of the selected vertices.

An edit mode bmesh is bound to the edit mesh. The selection is live.

import bpy
import bmesh

context = bpy.context

ob = context.edit_object # RUN IN EDIT MODE
me = ob.data
bm = bmesh.from_edit_mesh(me)

print([v.index for v in bm.verts if v.select])

because we have not changed anything, only read selection there is no need to update the bound mesh.

$\endgroup$
1
  • $\begingroup$ Why use v.index(last line) ? Since actual location of verts is required ([v.co for v in bm.verts if v.select]) would be better. $\endgroup$
    – srt111
    Commented May 21, 2020 at 3:23
1
$\begingroup$

A changed the way you selected active object and the way bmesh.from_mesh was called.

from os import system
   system("cls")

import bpy 
import bmesh

#Assign object as a part of the active scene
scene = bpy.context.scene
ob = bpy.context.object
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)

#Store selected vertices in a list
print("vert_selected")
#ob = bpy.context.object #Also direct context assignment raised no attribute error
me = ob.data
bm = bmesh.new()         # Create a new bmesh container instance
bm.from_mesh(me)         # Pass your mesh into this container

vert_selected = [v.co for v in bm.verts if v.select]   
for vs_s in vert_selected:
    print(vs_s)


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

bpy.ops.object.mode_set(mode='OBJECT') # if bmesh.from_edit_mesh() --> mode == EDIT - ValueError: to_mesh(): Mesh 'Cube' is in editmode 

bm.to_mesh(me) #If mode ==Object  -> ReferenceError: BMesh data of type BMesh has been removed
bm.free() 
ob.update_from_editmode()
```
$\endgroup$
3
  • $\begingroup$ Has this script worked for you in more than the" First run"? $\endgroup$
    – srt111
    Commented May 20, 2020 at 15:09
  • $\begingroup$ i get ` Active object = Cube vert_selected <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)> ` Everytime I run I get this. I'm not using system("cls") $\endgroup$
    – Aviik
    Commented May 20, 2020 at 15:13
  • $\begingroup$ That's why you are getting the same output everytime.System("cls") clears the screen to show the new output in the next run.Follow these steps -1 Retype the System("cls") command 2.Enter Edit mode and select some vertices on the cube 3.Check console output if the script is showing the correct selected vertices 4. Run the script a few more times with newly selected vertices and check is the console is showing the correct output for each run. $\endgroup$
    – srt111
    Commented May 20, 2020 at 15:24
0
$\begingroup$

As the context object's vertices where selected in "Edit" mode, the correct command to convert mesh to Bmesh should be from_edit_mesh(), as opposed to from_mesh() which accesses the mesh datablock( as used in the above code).

$\endgroup$
3
  • $\begingroup$ There is no issue. The error is (most likely) because of unnecessary mode switching. Switching to object mode kills an edit mode bmesh because the data (edit mesh) is lost. $\endgroup$
    – batFINGER
    Commented May 20, 2020 at 10:52
  • $\begingroup$ If the context is kept in Edit mode ,the console gives the following error ValueError: to_mesh(): Mesh 'Cube' is in editmode $\endgroup$
    – srt111
    Commented May 20, 2020 at 15:05
  • $\begingroup$ Consider using bmesh.update_edit_mesh(me) to update to mesh changes from an edit mode bmesh. And that's bmesh the module not some other variable you may have named bmesh. $\endgroup$
    – batFINGER
    Commented May 20, 2020 at 16:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .