# Why is this python script not working?

import bmesh
bmesh.from_edit_mesh(bpy.context.edit_object.data).faces[1].select=True


It says python script fail
What should I do ?

• What would you like to obtain? – Faceb Faceb Nov 30 '16 at 12:24
• The mesh object needs to be (a mesh, and) in edit mode (meshobject == context.edit_object) to use bmesh.from_edit_mesh(...) Use this if you are going to make an edit mode tool. A call to bmesh.update_edit_mesh(...) will show your changes "live". Otherwise use bm = bmesh.new() and load the mesh with bm.from_mesh(...). Oh and import bpy too, what exactly is the error message? – batFINGER Nov 30 '16 at 14:36

# Background

This is how to create a new object and add the vertices in the verts list:

import bpy
import bmesh

verts = [(1, 1, 1), (0, 0, 0)]  # 2 verts made with XYZ coords
mesh = bpy.data.meshes.new("mesh")  # add a new mesh
obj = bpy.data.objects.new("MyObject", mesh)  # add a new object using the mesh

scene = bpy.context.scene
scene.objects.active = obj  # set as the active object in the scene
obj.select = True  # select object

mesh = bpy.context.object.data
bm = bmesh.new()

for v in verts:
bm.verts.new(v)  # add a new vert

# make the bmesh the object's mesh
bm.to_mesh(mesh)
bm.free()  # always do this when finished


This is how to alter an existing mesh:

import bpy
import bmesh

verts = [(1, 1, 1), (0, 0, 0)]  # 2 verts made with XYZ coords
mesh = bpy.context.object.data
bm = bmesh.new()

# convert the current mesh to a bmesh
bm.from_mesh(mesh)

for v in verts:
bm.verts.new(v)  # add a new vert

# make the bmesh the object's mesh
bm.to_mesh(mesh)
bm.free()  # always do this when finished


As for selection:

import bpy
import bmesh

mesh = bpy.context.object.data
bm = bmesh.new()

# convert the current mesh to a bmesh
bm.from_mesh(mesh)

# must run this so we can use indexing on the faces
bm.faces.ensure_lookup_table()

# "select" the 0th face by setting its select property to True
bm.faces[0].select = True

# make the bmesh the object's mesh
bm.to_mesh(mesh)
bm.free()  # always do this when finished

# go back to edit mode to see the selection
bpy.ops.object.mode_set(mode='EDIT')


To test this:

1. Add a cube (go into edit mode and deselect all faces)
2. Paste the script into Blender's text editor
3. Hit run script in the text editor with the cube object still selected
• still it is not working? – user7098526 Nov 30 '16 at 12:42
• Do ineed to download something else for bmesh? – user7098526 Nov 30 '16 at 12:43
• Why are you toggling edit / object mode? The mesh doesn't need to be in edit mode at all to use bm.from_mesh(). To make it "live" in edit mode use bmesh.from_edit_mesh(mesh) and bmesh.update_edit_mesh(...)` to update changes to mesh without the need to toggle modes. – batFINGER Nov 30 '16 at 14:45
• See my updated answer, and see if you are doing things the same as I am. @batFINGER Thanks, I updated my answer. – JakeD Nov 30 '16 at 15:35
• Don't you also need to specify which vertices are connected with edges? Verts alone don't make a mesh! – Chris Dunaway Nov 30 '16 at 21:33