One handler to rule them all (depsgraph handler improvement)
Currently when an addon developer wants to respond to something happening inside Blender that doesn't have a handler, they have to request a new handler (which is work for the devs), or attempt to use the monstrous msgbus, which is a nightmare and often can't offer a solution (for example, you can't find out which node parameter a user has just tweaked), or give up.
I propose that the depsgraph handler be modified so that it receives details of what triggered the update, attribute name, the old value, and the new value.
This way users could have one handler, in which they can respond to unlimited changes and redirect to any of their own functions accordingly.
For example (forgive the formatting, not sure how to maintain the correct indents on this website):
import bpy
def something_changed (scene, depsgraph, triggered_by, attribute_changed, old_val, new_val):
if triggered_by.type == 'MESH':
#respond to your own functions related to meshes
if attribute_changed == 'parent':
............#this should be indented. using dots instead.
............my_parent_change_custom_handler(triggered_by.name,old_val,new_val)
if triggered_by.type == 'MIX_RGB':
if attribute_changed == '.inputs['Fac']':
print(f'Factor changed to {new_val}'
#same can be done for any other blender type below.
def my_parent_change_custom_handler(name, previous_val, current_val):
print(f'the parent of object {name} was changed from {previous_val} to {current_val}')
def register_handler():
if something_changed not in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.append(something_changed )
def unregister_handler():
if something_changed in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.remove(something_changed )
For things that don't run through the deps graph (i'm not entirely sure what they are), we could have top level handler for each, such as:
bpy.app.handlers.render(scene, status) #state would be pre_render, frame_complete, render_job_post etc
bpy.app.handlers.operators(scene, op_name, status) # status could be pre, started, finished, cancelled, pass_though, running_modal, etc.
bpy.app.handlers.UI(scene, ui_element, attribute, last_val, current_val) # useful for reacting to a user changing an editor etc.
0 Comments