I am fairly new to python scripting, so getting into blender is a uphill battle at the moment. I want to pass a property into the REDO panel that, when checked, tells the operator to take effect only to the active object. But I keep getting an error message saying that the property is not defined (it is my understanding that it should be, because of the self argument in the function). Any help is very much appreciated!

    "name": "Snap Origin to selected",
    "author": "Juan",
    "version": (1, 0),
    "blender": (2, 90, 0),
    #"location": "View3D > Mesh > Snap > Snap Origin to selected",
    "description": "snaps the origin of the object to a selection",
    "category": "Mesh",

import bpy

class MESH_OT_snapOrigin(bpy.types.Operator):
    """It has to begin with a selected element in edit mode"""
    bl_idname = "mesh.snap_origin_to_selected"
    bl_label = "Snap Origin to selected"
    bl_options = {'REGISTER', 'UNDO'}
    only_active: bpy.props.BoolProperty(
        name="set only active",
        description="set only the origin of the active object",

    def poll(cls, context):
        return context.area.type == 'VIEW_3D'

    def execute(self, context):
        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
        if only_active.value is True:
           bpy.ops.objects.active.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
           bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
        # bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
        return {'FINISHED'}

# Registration

def set_origin_button(self, context):
        text="snap origin to selected",
def register():

def unregister():

I also don't know if bpy.ops.objects.active.origin_set(type='ORIGIN_CURSOR', center='MEDIAN') is going to work, given that I get an error before that, just so you know...it is probably also wrong.

  • 2
    $\begingroup$ You would have to use self.only_active to access the property and there is no need for a comparison with True you can simply write if self.only_active. Does that answer your question? $\endgroup$
    – Robert Gützkow
    Nov 8, 2020 at 18:38
  • $\begingroup$ yes, thanks a lot! The comparison part I had it like you say in the beggining, but trying to find the error I just add it at some point...just to make sure. $\endgroup$ Nov 9, 2020 at 20:34

1 Answer 1


ok, so...I just had to add self. before the property for the function to recognize it (and lose the .value, since it is a boolean, but that's a different error). Like so: if self.only_active is True:

I still don't know how to get the active object. The whole context part is particularly confusing to me. But I guess I'll get it eventually.


You must log in to answer this question.

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