Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import bpy
import bmesh
from mathutils import Vector
bl_info = {
'name': 'Add Empties',
'author': 'Dealga McArdle (zeffii)',
'version': (1, 1, 2),
'blender': (2, 7, 3),
'location': 'Add > Empties',
'description': 'adds new Empty while in edit mode',
'wiki_url': '',
'tracker_url': '',
'category': 'Mesh'}
def add_to_selected(context, kind):
D = bpy.data
scn = context.scene
obj = context.edit_object
obj_name = obj.name
mesh = obj.data
bm = bmesh.from_edit_mesh(mesh)
def add_empty(coordinate, kind='PLAIN_AXIS'):
empty = D.objects.new('MT_' + obj_name, None)
empty.location = coordinate
empty.empty_draw_size = 0.45
empty.empty_draw_type = kind
scn.objects.link(empty)
scn.update()
coordinates = [v.co[:] for v in bm.verts if v.select]
# must be in object mode to add objects to the scene.
myob = D.objects[obj_name]
bpy.ops.object.mode_set(mode='OBJECT')
if not coordinates:
coordinate = context.scene.cursor_location
add_empty(coordinate, kind)
else:
for co in coordinates:
vert_coordinate = myob.matrix_world * Vector(co)
add_empty(vert_coordinate, kind)
# set original object to active, selects it, place back into editmode
scn.objects.active = myob
myob.select = True
bpy.ops.object.mode_set(mode='EDIT')
return
class AddEmpties(bpy.types.Operator):
"""Add a vertex to 3d cursor location"""
bl_idname = "object.empties_add"
bl_label = "Add Empties"
bl_options = {'REGISTER', 'UNDO'}
kind = bpy.props.StringProperty(default='PLAIN_AXES')
def execute(self, context):
add_to_selected(context, self.kind)
return {'FINISHED'}
class EDITMODE_MT_PlaceEmpties(bpy.types.Menu):
bl_label = "Adding Empties.."
def draw(self, context):
options = [
'PLAIN_AXES', 'ARROWS', 'SINGLE_ARROW',
'CIRCLE', 'CUBE', 'SPHERE', 'CONE', 'IMAGE']
layout = self.layout
for opt in options:
layout.operator("object.empties_add", text=opt).kind = opt
def menu_func(self, context):
self.layout.menu("EDITMODE_MT_PlaceEmpties", icon='EMPTY_DATA')
def register():
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_mesh_add.prepend(menu_func)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_mesh_add.remove(menu_func)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.