1
$\begingroup$

For the sake of simplicity, lets start with the default screen.

How would you go about selecting faces in Python?
Top, Bottom, Sides?

bpy.ops.object.editmode_toggle()  
bpy.ops.mesh.select_all(action='DESELECT')  
bpy.ops.mesh.select_face_by_sides(0)  

This seems to select everything.
I can't find a subscriptable face[0] anywhere...

I also know you can do:

bpy.ops.mesh.select_all(action='SELECT')  
bpy.ops.mesh.select_nth(nth=6, skip=1)  

but I'm not too savvy on the whole 'nth' thing.
I had it at one point, but I don't recall what I did.

set nth= to the number of faces,
then use skip= to pick different faces?

Is that the only method, or can I find a simple list of faces somewhere?

$\endgroup$
2
$\begingroup$

bpy.ops.mesh.select_face_by_sides allows to select faces which are matching an amount of sides (border edges). It also needs named arguments:

Examples:

#Selects faces bordered by 3 edges
bpy.ops.mesh.select_face_by_sides(number=3)

#Selects faces bordered by more than 3 edges
bpy.ops.mesh.select_face_by_sides(number=3, type='GREATER')

The object's faces are stored in polygons (obj.data.polygons).

So for instance, for top, bottom and side faces you can use:

import bpy
from mathutils import Vector

def NormalInDirection( normal, direction, limit = 0.5 ):
    return direction.dot( normal ) > limit

def GoingUp( normal, limit = 0.5 ):
    return NormalInDirection( normal, Vector( (0, 0, 1 ) ), limit )

def GoingDown( normal, limit = 0.5 ):
    return NormalInDirection( normal, Vector( (0, 0, -1 ) ), limit )

def GoingSide( normal, limit = 0.5 ):
    return GoingUp( normal, limit ) == False and GoingDown( normal, limit ) == False

obj = bpy.context.object

prevMode = obj.mode

bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

#Selects faces going side
for face in obj.data.polygons:
    face.select = GoingSide( face.normal )

bpy.ops.object.mode_set(mode=prevMode, toggle=False)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.