0
$\begingroup$

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",
        default=False,
    )
    

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


    def execute(self, context):
        bpy.ops.view3d.snap_cursor_to_selected()
        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')
        else:
           bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
        # bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
        print("updated!")
        return {'FINISHED'}

# Registration

def set_origin_button(self, context):
    self.layout.operator(
        MESH_OT_snapOrigin.bl_idname,
        text="snap origin to selected",
        icon='PIVOT_CURSOR')
        
        
def register():
    bpy.utils.register_class(MESH_OT_snapOrigin)
    bpy.types.VIEW3D_MT_edit_mesh.append(set_origin_button)

def unregister():
    bpy.utils.unregister_class(MESH_OT_snapOrigin)
    bpy.types.VIEW3D_MT_edit_mesh.remove(set_origin_button)

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.

$\endgroup$
2
  • 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

0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.