6
$\begingroup$

Is there a way to "lock" an object so that it can't be deleted without unlocking it? Kind of like the option often present on digital cameras.

If not, any other way of preventing the user from deleting an object?

Also, this is for an addon, so no problem with script-based answers as a last resort.

$\endgroup$
10
$\begingroup$

You can override the delete function to achieve this :

  • add protected boolean property for the object type
  • set this property to true to prevent the object from deletion
  • the new delete function will only delete unprotected objects

this is the new delete operator no other changes are needed just include it in your addon and it will override the default delete function :

import bpy
from bpy.props import BoolProperty

bpy.types.Object.protected = BoolProperty(name = 'protected', default = False)
def main(context):
    for obj in context.selected_objects:
        if not obj.protected :
            bpy.context.scene.objects.unlink(obj)
            bpy.data.objects.remove(obj)
        else :
            print(obj.name +' is protected')


class delete_override(bpy.types.Operator):
    """delete unprotected objects"""
    bl_idname = "object.delete"
    bl_label = "Object Delete Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(delete_override)

def unregister():
    bpy.utils.unregister_class(delete_override)

if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ However, after reading the code a few times, would you mind explaining how it works? I can't seem to find where and how you override the default deletion operator... $\endgroup$ – Ludwik Apr 25 '15 at 11:07
  • 3
    $\begingroup$ they have the same Id name bl_idname = "object.delete" so when you register it the old definition gets overwritten by this one $\endgroup$ – Chebhou Apr 25 '15 at 11:09
  • 1
    $\begingroup$ I know this is old question but - how to get the "OK?" prompt from the original delete operator? $\endgroup$ – spacer May 19 '16 at 10:54

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.