I’m trying to make a certain graphing addon for blender, useful for rigging, but I’m stuck in the very first steps. How do I attach callback functions to buttons? I created button using this code:
def draw(self, context):
column = self.layout.column() #items are placed under each other in a column
column.prop(context.scene, "switch_mode")
contexts = []
if context.scene.switch_mode==False:
for item in self.bone_groups.keys():
column.prop(context.active_object.data, 'layers',
index=self.bone_groups[item],
toggle=True, text=item,
emboss=True)
now there are basically 5 buttons created inside the for loop. But, I’d want to associate each button with a specific callback in addition to it’s behaviour of toggling the bone layers. Any help would really be appreciated.
If a property is created by you in your script, you can attach an update function. For existing RNA properties (like bone layers), the RNA message bus API needs to be used. The msgbus is an under-documented part of the Blender Python API that allows subscribing to changes in RNA properties. In your specific case with bone layers I’m not sure on how to determine which button was pressed, as they are all tied to one internal array.
In your specific case, it might be useful to subscribe to each armature individually. The code to do this would be:
# Callback function for bone layer changes
def bone_layer_update_callback(ob):
# Do something here
print("Bone layers changed on object: ", ob.name)
# Subscribe to bone layers for a specific armature
def subscribe_to_bone_layers(ob):
if ob.type != 'ARMATURE':
return
subscribe_to = ob.data.path_resolve("layers", False)
bpy.msgbus.subscribe_rna(
key=subscribe_to,
# owner of msgbus subcribe (for clearing later)
owner=ob,
# Args passed to callback function (tuple)
args=(ob,),
# Callback function for property update
notify=bone_layer_update_callback,
)
# Ensure only armatures are passed to this function
subscribe_to_bone_layers(bpy.context.object)
To clear the msgbus handler, call this function on each object that you passed to subscribe_to_bone_layers
bpy.msgbus.clear_by_owner(ob)
Maybe someone else knows how to subscribe to individual layers, but I don’t think that is possible. You may need to add custom properties to each armature in code to track changes to specific layers. Hope this is helpful!
I’m sorry but, it seems I have yet to know much about the architecture, I didn’t really understand terms like msgbus, RNA message bus API, there seems to be some sort of architecture going under the hood. Can you give me a link or some reference to what these are and how these are laid out in blender?
After looking back over my previous answer I can see how it could be confusing. Within Blender’s C source code is defined the RNA and DNA. I do not understand the system completely, but simply put the DNA data is stored in the .blend file. This is any data that needs to be saved permanently (objects, window layout, materials, settings, etc). The RNA allows this data to be accessed from Python.
There are some older documents on the architecture of Blender. I have not found any newer sources but things seem relatively unchanged on a higher-level. Some design documents are found on the Blender Dev Wiki Archive and there is an old BlenderNation post on DNA and RNA.
I’ve written many addons without knowing the inner workings of Blender, so unless you are curious I wouldn’t worry too much about understanding this perfectly.
Essentially the message bus (shortened to msgbus) allows subscribing to changes in certain properties already defined within Blender. These are properties like object locations, names, materials, scene gravity, render settings… any property that is already exists in a .blend file. The link in my other reply is the best documentation I can find on how to use the msgbus. Because you want a function to trigger when bone layers are changed, you need to use the msgbus.
An alternative might be to create an operator for each button, which would be a more straightforward method of triggering code on a button press.
@natecraddock Thank you for sharing the information about DNA and RNA. I would like to add the following link which talks about the publish/subscribe design pattern which the bpy.msgbus module provides us.