1
$\begingroup$

I learn plugin writing/ So, I created plugin, was add empty object on active vert

bl_info = {
    'name': 'Empty on active',
    'author': 'Igor Yuchimenko',
    'version': (0, 1, 0),
    'blender': (2, 71, 0),
    'category': 'Add Mesh',
    'description': 'Tested addon create empty on active vert'
}

import bpy
import bmesh

class vertempty(bpy.types.Operator):
    bl_idname = 'v.empty'
    bl_label = 'Crete emty on active vert'
    bl_options = {'REGISTER', 'UNDO'}

    def active_vertex(self, bm):
        for elem in reversed(bm.select_history):
            if isinstance(elem, bmesh.types.BMVert):
                return elem
            break

    def execute(self, context):
        me = bpy.context.object.data
        bm = bmesh.from_edit_mesh(me)
        avert = self.active_vertex(bm)
        bpy.ops.object.add(type='EMPTY', avert.co)
        return {'FINISHED'}

def addToMenu(self, context):
    self.layout.operator("v.empty", icon = "OUTLINER_OB_EMPTY")

def register():
    bpy.utils.register_class(vertempty)
    bpy.types.INFO_MT_mesh_add.append(addToMenu)


def unregister():
    bpy.utils.unregister_class(vertempty)
    bpy.types.INFO_MT_mesh_add.remove(addToMenu)

But on run (when menu clicked) I get error

RuntimeError: Operator bpy.ops.object.add.poll() failed, context is incorrect

So, as I understand, empty can't add to object mesh, so Ш get an error.

$\endgroup$
0

1 Answer 1

2
$\begingroup$

Use API methods

Using base API methods to add new objects to a scene, alleviates the need for context.

To create a new empty with name "somename" linked to the context scene at location (0, 0, 0)

mt = bpy.data.objects.new("somename", None)
context.scene.objects.link(mt)  
mt.location = (0, 0, 0)

Note: to put the empty at the global location of the vertex

mw = context.edit_object.matrix_world
global_vert_pos = mw * avert.co

otherwise it will be at the local coordinate, eg one local corner vertex of default cube is (1, 1, 1), any transformation on the cube and it's global location will differ.

Soapbox: since context is an argument of execute method use me = context.edit_object.data (there is no need to fully address from bpy.)

Note scene.objects.link is no more in 2.8

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .