co for edges and faces with bmesh how to ?

i got this but how so you get the co for each verts per edge or face?


 
 
for e in bmesh.edges:
 print('edge =',e,' index =',e.index ,' verts=',e.verts)
 print (' other verts =',e.other_vert)
 
print ()
print (' Print faces list')
print ()
 
 
for f in bmesh.faces:
 
 print('face=',f,' edges=',f.edges,' index =',f.index,' verts=',f.verts)
 
 
 
 
 

thanks for any help

import bpy
import bmesh

object = bpy.context.object
mesh = object.data
bmesh = bmesh.from_mesh(mesh)


for f in bmesh.faces:
    for v in f.verts:
        print(v.co)

for f in bmesh.faces:
    print(f)
    for e in f.edges:
        print(e)
        for v in e.verts:
            print(v,v.co)

first line gives

face= <BMFace(0x0B5E5E00), index=0, totverts=4>

how can you get from this the index and the totverts is this lke a list may be?

also there must be a way to put it into edit mode i guess but how
or use this may be

bpy.ops.object.mode_set(mode = ‘OBJECT’)

bpy.ops.object.mode_set(mode = ‘EDIT’)
but are these bmesh compatible?

on thing here

these commands f.edges: is this in bmesh mode or old API commands?

i mean i saw a module for bmesh verts then edges and faces
but not certain if these can also be use and how

thanks

Ricky,
Look at the code below and see if this answers some of your questions, this is using the bmesh module. I’m just learning it too, so please excuse my limited knowledge of this.

This code basically checks if you’re in edit mode, if not changes to it (switches back at the end of the script). Then it prints the numbers of faces, verts and edges. It then goes through and lists how many verts/edges are in each face, and which face has the most.

Pretty basic, but it helped me understand how bmesh is working.

import bpy
import bmesh

my_mode = bpy.context.object.mode # Check if object mode is EDIT, needs to be for bMesh
if bpy.context.object.mode != 'EDIT': 
    bpy.ops.object.mode_set(mode = 'EDIT')

mesh = bpy.context.object.data
bmesh = bmesh.from_mesh(mesh) # Activate bMesh on current mesh

totalverts = len(bmesh.verts)
totaledges = len(bmesh.edges)
totalfaces = len(bmesh.faces)

print("
This object contains:")
print("%s Vertices, %s edges, and %s Faces." % (totalverts, totaledges, totalfaces))

list = []
for face in bmesh.faces:
    faceindex = face .index
    verts = len(face .verts) #Don't need edges because verts == edges on same face
    list.append(verts)
    print("Face %d has: %d verts and edges" % (faceindex, verts))

# Check for the most verts
max_value = max(list)
max_index = list.index(max_value)
print("
%d is the most verts in a face. That face %d" % (max_value, max_index))

# Change the mode back to what it was
if my_mode != 'EDIT':
    bpy.ops.object.mode_set(mode = my_mode)

# Script Footer
print("
FINISHED", "---"*23)
from datetime import datetime
print(datetime.now())

Changing the object mode is compatible with bmesh. What I found when working the with the console the following command needs to be run in ‘EDIT’ mode only, it’ll error in object mode. Running this code will allow you to access the bmesh info.

import bmesh
mesh = bpy.context.object.data
bmesh = bmesh.from_mesh(mesh)

Be warned, if you’re using the console to test commands. If you switch to ‘OBJECT’ mode and back to edit mode, you’ll need to rerun the code above to be able to access bmesh again.

well i guess that everyone is re learning how to make certain things with Bmesh
and probably all the script using verts edges and faces are broken

so i need to learn this basic things cause i have so many script where i make new primitives

i would like to see somethin along this line
i needed i an upload a samll old script for making a primitive that i have using verts edges and faces

but example is very interestin

gives a better idea what canb e done with Bmesh
but i guess till in alpha stage so not certain if this will stay there or be modified again before next release !

thanks

how d you determine the number of edge per face ?

right now you got this

Face 0 has: 4 verts and edges

or is it 2 verts / edge

meaning in this case that 4 /2 = 2 edges ?

but this does work for a cube which has one face 4 verts and 4 edges!

if i do this i get bad format !

print(“Face %d has: %d verts and % edges” % (faceindex, verts, verts/2))

get this

Face 0 has: 4 verts and 2 edges

thanks

i found this example

list_ = [ [v.index for v in f.verts] for f in bmesh.faces if f.select and f.is_valid]

but have to rewrite this to better understand how it works!

be back with a more elaborate example and easier to understand what’s happening inside the loops

thanks

here is another one i’m not certain
on this page

http://www.blender.org/documentation/blender_python_api_2_62_0/bmesh.types.html#bmesh.types.BMesh

class bmesh.types.BMFace

from this

for f in bmesh.faces:

print(‘face=’,f,‘cacl_area()=’,f.calc_area())
print(‘face=’,f,‘calc_center_bounds=’,f.calc_center_bounds())
print(‘face=’,f,‘calc_center_median=’,f.calc_center_median())
print ()

got this

class bmesh.types.BMFace

face= <BMFace(0x0B4DD268), index=0, totverts=4> cacl_area()= -3.999999523162842
face= <BMFace(0x0B4DD268), index=0, totverts=4> calc_center_bounds= <Vector (-0
0000, 0.0000, -1.0000)>
face= <BMFace(0x0B4DD268), index=0, totverts=4> calc_center_median= <Vector (0.
000, 0.0000, -1.0000)>

what is the difference from center bound and center median ?

and do you get the number cause here it is like a phrase or text fields

like getting the center bound = vector = <Vector (-0
0000, 0.0000, -1.0000)>

thanks

From what I can tell, bMesh creates a list of all the faces in an object. You can then go through that list that contains the faces, and find out which edge/vert indexes are within that face.

import bpy, bmesh

bpy.ops.object.mode_set(mode = 'EDIT')
mesh = bpy.context.object.data
bmesh = bmesh.from_mesh(mesh)

def myedges(face_index):
    face = bmesh.faces[face_index].edges
    print("Face #%d has:" % face_index)
    for edge in face:
        print("edge index #%d" % edge.index)
    print("
")
    
myedges(0)
myedges(2)

bmesh.faces[0].edges[1]
is the same as
bmesh.edges[1]

This is what I’m figuring out so far

On the example, when I printed the edges and verts. That number will always be the same, a face with four verts will always have four edges as well. For simplicity I only calculated the number of verts, and just assumed that there were the same number of edges. " 4 verts and edges == 4 verts and 4 edges"

Interesting learning this stuff…

any example for these ones

class bmesh.types.BMIter
Internal BMesh type for looping over verts/faces/edges, used for iterating over BMElemSeq types.
class bmesh.types.BMLoop

seems much more powerfull then the older version
and more fun i guess
you can do lot more
but need to practice

i got to find one of my old script to make a primitive and try to convert it to bmesh!

got a problem when copying from web page to text ediitor

if ther are any un8 character like end of line
blender freeze

is there anyway to get rid of these un8 characters
or to detect them in text editor ?

thanks

did a test here with these

for f in bmesh.faces:

print(‘face=’,f)
print(‘cacl_area()=’,f.calc_area())
print(‘calc_center_bounds=’,f.calc_center_bounds())
print(‘calc_center_median=’,f.calc_center_median())
print(‘edges=’,f.edges)
print(‘index=’,f.index)
print(‘loops=’,f.loops)
print(‘normal=’,f.normal)
print(‘smooth=’,f.smooth)
print(‘verts=’,f.verts)
print ()

and got these

class bmesh.types.BMFace
face= <BMFace(0x0B63AD40), index=0, totverts=4>
cacl_area()= -3.999999523162842
calc_center_bounds= <Vector (-0.0000, 0.0000, -1.0
calc_center_median= <Vector (0.0000, 0.0000, -1.00
edges= <BMElemSeq object at 0x0ADE0530>
index= 0
loops= <BMElemSeq object at 0x0ADE0530>
normal= <Vector (0.0000, 0.0000, -1.0000)>
smooth= False
verts= <BMElemSeq object at 0x0ADE0530>

not certain what the loops is doing here and samethign with this verts
it gives like an address instead of data !