bmesh face normals

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

tulson
Posts: 2
Joined: Sun Sep 30, 2012 11:54 pm

bmesh face normals

Postby tulson » Mon Oct 01, 2012 12:02 am

i tried to use bmesh to get the facenormals, but when tested it the value of all normals were null.

Code: Select all

import bpy
import bmesh

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

print('bmesh:', bm)

for f in bm.faces:
    print('face index:', f.index ,' and face normal:', f.normal)


------
console output:
face index: 0 and face normal: <Vector (0.0000, 0.0000, 0.0000)>
face index: 1 and face normal: <Vector (0.0000, 0.0000, 0.0000)>
face index: 2 and face normal: <Vector (0.0000, 0.0000, 0.0000)>
face index: 3 and face normal: <Vector (0.0000, 0.0000, 0.0000)>
face index: 4 and face normal: <Vector (0.0000, 0.0000, 0.0000)>
face index: 5 and face normal: <Vector (0.0000, 0.0000, 0.0000)>

were did i went wrong or is it not finished yet?

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Mon Oct 01, 2012 4:20 pm

dunno if this is intended this way, but if you call normal_update(), everything seems fine

Code: Select all

import bpy
import bmesh

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

print('bmesh:', bm)

for f in bm.faces:
    print('face index:', f.index ,' and face normal:', f.normal)


however, if i do
me.calc_normals()
all print() commands cause an error, which won't be shown in console oO
I'm sitting, waiting, wishing, building Blender in superstition...

tulson
Posts: 2
Joined: Sun Sep 30, 2012 11:54 pm

Postby tulson » Mon Oct 01, 2012 8:33 pm

im not a programmer and i really have some troubles with to anderstand the api. now i see the normal_update(). its very foggy from were im standing....

thanks a lot CoDEmanX.
it works
i did some more tryouts in selecting and mesh editing, well...
in the console window you can see whats going on.
theres a problem with the split() and the transform.
without bmesh: bpy.context.active_object.data.polygons this is possible.
i will search the api....

Code: Select all

import bpy
import bmesh

def rotFaces(rotval, arrsel, bme):
    for f in arrsel:
        f.select = True
    print('faces in array: ', arrsel)
    for f in bme.faces:
        print('Test selected index:', f.index, f.select)
#    bpy.ops.object.mode_set(mode = 'EDIT', toggle = False)
#    bpy.ops.mesh.split()
#    bpy.ops.transform.rotate(value=(rotval), axis=(0,0,1))
#    bpy.ops.object.mode_set(mode = 'OBJECT', toggle = False)
    for f in bme.faces:
        f.select = False 
   
ob = bpy.context.active_object
me = ob.data
bm = bmesh.new()
bm.from_mesh(me)
bm.normal_update()
print('selected bmesh:', bm)   

bpy.ops.object.mode_set(mode = 'EDIT', toggle = False)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.context.scene.tool_settings.mesh_select_mode = (False, False, True)
bpy.ops.object.mode_set(mode = 'OBJECT', toggle = False)
   
for f in bm.faces:
    print('face index:', f.index ,' and face normal:', f.normal)

xminus = []
yminus = []
yplus = []

for f in bm.faces:
    if f.normal[0] < 0:
        xminus.append(f)

for f in bm.faces:
    if f.normal[1] < 0:
        yminus.append(f)

for f in bm.faces:
    if f.normal[1] > 0:
        yplus.append(f)
                       
print('xminus: ', xminus)
print('yminus: ', yminus)
print('yplus: ', yplus)

rotv = 3.141529
rotFaces(rotv, xminus, bm)
bm.to_mesh(me)
rotv = 3.141529/2
rotFaces(rotv, yminus, bm)
bm.to_mesh(me)
rotv = -3.141529/2
rotFaces(rotv, yplus, bm)
bm.to_mesh(me)


Return to “Python”

Who is online

Users browsing this forum: Google [Bot] and 2 guests