1
$\begingroup$

Is it possible to run my own script after the "Delete object" and "Rename object" action? Not all at once deletion and rename - separately. (bpy.ops.object.delete(), bpy.context.object.name = "something").

Or it is possible to somehow override the original operator "delete" and keep all the steps from the original "delete" and add extra script (e.g. a reference to def). And how will it work with "renaming" (changin variable).

I'm only looking for a way to run my script after detect default delete, rename.

Thank you for your time.

$\endgroup$
3
  • $\begingroup$ I don't know if you can run it after the default ones but maybe you could just add the delete and rename functions to the code you want to run and then hotkey those scripts to the default keys for delete and rename? $\endgroup$
    – Cornivius
    Commented Jul 1 at 18:53
  • $\begingroup$ Unfortunately, this is not the case. My script is created as a protection when someone accidentally deletes or rename something. $\endgroup$ Commented Jul 1 at 19:13
  • 2
    $\begingroup$ you can override a builtin operator (as a matter of fact this may answer your question : blender.stackexchange.com/a/240558/86891 ) but it's risky if other addons depend on it. You can also overwrite hotkeys with your addon on register and use your own override operator that will call the builtin operator if conditions are met. I prefer this method since it doesn't monkey patch vanilla operations and hotkey shortcuts are easily reversable $\endgroup$
    – Gorgious
    Commented Jul 2 at 9:49

2 Answers 2

1
$\begingroup$
import bpy
from bpy import data as D
from bpy import context as C

# this will check after every time the dependcy graph is updated
# and run this function
# depsgraph_update is pretty broad tho, basically anytime 
# you manipulate anything about an object in the scene it updates

def post_delete(context):
    print("Just deleted an object.")    
    
bpy.app.handlers.depsgraph_update_post.append(post_delete)


# you could also just write a custom delete function like this 

sel_objs = C.selected_objects

for obj in sel_objs:
    bpy.data.objects.remove(obj, do_unlink=True)
    
# Do your post-delete thing here
    
# put that into an operator and then just remap the 'X' hotkey to 
# your operator
    
$\endgroup$
7
  • $\begingroup$ Have you tested this code? ;) Is this AI-generated? $\endgroup$ Commented Jul 1 at 21:03
  • $\begingroup$ I always test my code before posting as an answer. I would rather die than use anything AI-related. The question itself is a little vague so best answer I can offer is a basic demo of the concept, but I have no idea what the guy is trying to do aside from "do something after I delete an object" $\endgroup$
    – Jakemoyo
    Commented Jul 1 at 21:09
  • 1
    $\begingroup$ Doesn't look like AI-generated, but the name mismatch suggests it won't work lol. Also you don't make an actual check for something being deleted, the post_del(ete) will print on every depsgraph change not just removal. Also what's with the triple backtick quote? :D And why do you delete all selected objects? It all seems so random that's why I asked. $\endgroup$ Commented Jul 1 at 22:05
  • $\begingroup$ @MarkusvonBroady good points. Doesn't look like it's well-tested. Function post_delete will always get triggered. after a depsgraph change. Honestly I'm confused how the OP says it solved their problem. Hmmm... $\endgroup$
    – Harry McKenzie
    Commented Jul 2 at 0:34
  • $\begingroup$ @MarkusvonBroady I tested the code, to see if my answer would work, then I must have made some changes to add formatting and make things more readable changed e.g. post_del -> post_delete and missed that. Usually if I notice minor formatting issues in someone elses answer I'll just edit it and move on with my day instead of accusing the answerer of using ChatGPT. $\endgroup$
    – Jakemoyo
    Commented Jul 2 at 0:56
1
$\begingroup$

For my purposes, I'll use this for now:

import bpy
from bpy.app.handlers import persistent

@persistent
def my_handler(context):
    if bpy.context.active_operator.name == 'Delete':
        print ("I deleted something")

    elif bpy.context.active_operator.name == 'Rename':
        print ("I renamed something")

bpy.app.handlers.depsgraph_update_post.append(my_handler)
$\endgroup$
1
  • 1
    $\begingroup$ Hello. I suggest you add a little safety check at the start of your function. eg if not hasattr(bpy.context, "active_operator") or bpy.context.active_operator is None: return otherwise you might see error messages in some situations. $\endgroup$
    – Gorgious
    Commented Jul 2 at 9:45

You must log in to answer this question.

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