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 selected. Show all posts
Showing posts with label selected. 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?

March 26, 2013

Painting Vertex Color Map using selected Faces

This script is obsolete, shift+K will fill all selected faces/ verts with the brush colour.

selected faces

During a recent conversation with Jimmy Gunawan of blendersushi I remembered that I wanted to write a script that lets me paint the vertex color map as a function of which faces are selected. Here's a version of this script that runs in vertex paint mode with face masking mode on. i'll leave refactoring this snippet to you.

I've limited some freedom by forcing the user to be in 'VERTEX_PAINT' and Face Masking Active, but it's safer this way (even after some bugfixing by campbell) -- as it is still easy to crash blender in obscure ways otherwise.

July 07, 2011

How to set active_object via python

a simple one liner!
"""using an object reference"""
>>> bpy.context.scene.objects.active = bpy.data.objects["Cube"]

# if checked to see which is now active
>>> bpy.context.scene.objects.active

# this may not update visibly as you might expect in the viewport, 
# but you will see the origin of active_object, and the name bottom left.
bpy.data.objects['Cube']

active != selected, you could have 10 objects selected, of which only 1 can ever be active at a time. Or none selected, but 1 active. To get a better understanding of this not so intuitive situation: unselect everything, if you have two objects in the scene, the last one interacted with will be 'active' (as in, shows the origin, and name displayed bottom left)

Setting the state of an object to 'select = True' , is not the same thing

Example usage of active object

as an example, below is code that adds Empties while an object is in Edit Mode, the script must set the active object back to the former object, after an empty is added. Try it with suzanne in Edit Mode, then select one vertex and run the script.

July 03, 2011

Deleting a face from a mesh

if you are modifying data of an object, sometimes you want to remove a face
# must be in edit mode.

# first select the face, then delete it.
mplane.data.faces[-1].select = True # won't show in viewport
bpy.ops.mesh.delete(type='ONLY_FACE') # will be acted on.
must be in edit mode for these options, other options for types are found by reading the tooltips in the delete menu.