Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

Showing posts with label iterate. Show all posts
Showing posts with label iterate. Show all posts

April 19, 2016

Getting Index (indices) of selected faces / polygons

One Way

# assuming the object is currently in Edit Mode.
import bpy
import bmesh

obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

for f in bm.faces:
    if f.select:
        print(f.index)

# Show the updates in the viewport
# and recalculate n-gon tessellation.
bmesh.update_edit_mesh(me, True)


Another Way

import bpy
import bmesh
from bmesh.types import BMFace

obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

for geom in bm.select_history:
    if isinstance(geom, BMFace):
        print(geom.index)

bmesh.update_edit_mesh(me, True)

Further Reading

I encourage you to know what the documentation says. blender.org/api/blender_python_api_current/bmesh.html?

January 20, 2015

Screen Areas, Iterate Through

Sometimes I need this but it's a pain to type, so here is how to iterate through current screen areas
import bpy

for window in bpy.context.window_manager.windows:
    for area in window.screen.areas:
        if area.type == 'VIEW_3D':
            print('yes!')
and deeper
import bpy


for window in bpy.context.window_manager.windows:
    for area in window.screen.areas:
        if area.type == 'VIEW_3D':
            print('yes!')
            print(dir(area))
            for space in area.spaces:
                print(space.type)
                if space.type=='VIEW_3D':
                    print(dir(space))