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...

July 23, 2011

Place empty while in Edit Mode

Sometimes we need to place a marker (Empty) at a given point on our mesh. I use this often to define a pivot point. But manually hopping in and out of edit mode to add an Empty soon becomes lame. This is my solution. Run this in edit mode, select as many verts as you wish.

# be in edit mode to run this code
import bpy
from mathutils import Vector
myob = bpy.context.active_object
bpy.ops.object.mode_set(mode = 'OBJECT')
# collect selected verts
selected_idx = [i.index for i in myob.data.vertices if i.select]
original_object = myob.name
for v_index in selected_idx:
# get local coordinate, turn into word coordinate
vert_coordinate = myob.data.vertices[v_index].co
vert_coordinate = myob.matrix_world * vert_coordinate
# unselect all
for item in bpy.context.selectable_objects:
item.select = False
# this deals with adding the empty
bpy.ops.object.add(type='EMPTY', location=vert_coordinate)
mt = bpy.context.active_object
mt.location = vert_coordinate
mt.empty_draw_size = mt.empty_draw_size / 4
bpy.ops.object.select_all(action='TOGGLE')
bpy.ops.object.select_all(action='DESELECT')
# set original object to active, selects it, place back into editmode
bpy.context.scene.objects.active = myob
myob.select = True
bpy.ops.object.mode_set(mode = 'EDIT')