def draw(self, context):
column = self.layout.column() #items are placed under each other in a column
contexts = 
for item in self.bone_groups.keys():
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
# Do something here
print("Bone layers changed on object: ", ob.name)
# Subscribe to bone layers for a specific armature
if ob.type != 'ARMATURE':
subscribe_to = ob.data.path_resolve("layers", False)
# owner of msgbus subcribe (for clearing later)
# Args passed to callback function (tuple)
# Callback function for property update
# Ensure only armatures are passed to this function
To clear the msgbus handler, call this function on each object that you passed to subscribe_to_bone_layers
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.
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.