Previous Thread  Next Thread

chat icon bmesh face normals

tulson

Posted: Sun Sep 30, 2012 11:02 pm
Joined: 30 Sep 2012
Posts: 2
i tried to use bmesh to get the facenormals, but when tested it the value of all normals were null.

Code:
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?
Reply with quote


CoDEmanX

Posted: Mon Oct 01, 2012 3:20 pm
Joined: 05 Apr 2009
Posts: 680
dunno if this is intended this way, but if you call normal_update(), everything seems fine

Code:
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...
Reply with quote


tulson

Posted: Mon Oct 01, 2012 7:33 pm
Joined: 30 Sep 2012
Posts: 2
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:

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)
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group