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

It says python script fail
What should I do ?

$\endgroup$
2
  • 1
    $\begingroup$ What would you like to obtain? $\endgroup$ Commented Nov 30, 2016 at 12:24
  • $\begingroup$ 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? $\endgroup$
    – batFINGER
    Commented Nov 30, 2016 at 14:36

1 Answer 1

1
$\begingroup$

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.link(obj)  # put the object into the scene (link)
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

Answer

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
$\endgroup$
6
  • $\begingroup$ still it is not working? $\endgroup$ Commented Nov 30, 2016 at 12:42
  • $\begingroup$ Do ineed to download something else for bmesh? $\endgroup$ Commented Nov 30, 2016 at 12:43
  • $\begingroup$ 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. $\endgroup$
    – batFINGER
    Commented Nov 30, 2016 at 14:45
  • $\begingroup$ See my updated answer, and see if you are doing things the same as I am. @batFINGER Thanks, I updated my answer. $\endgroup$
    – JakeD
    Commented Nov 30, 2016 at 15:35
  • $\begingroup$ Don't you also need to specify which vertices are connected with edges? Verts alone don't make a mesh! $\endgroup$ Commented Nov 30, 2016 at 21:33

You must log in to answer this question.

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