I have a Master Addon which adds its own vertical tabs and panels. Then i have a Child Addon which provides some additional tools which fit well into various panels created by master. But the Child Addon can also run standalone.
When both Addons are enabled the additional tools from the Child Addon shall be placed into the Panels provided by the Master Addon. But when the Child Addon is running "standalone", then it shall create its own panels instead. The master may know about the child. but the child must not depend in any way on the master addon.
So the goal is to avoid to duplicate Operator Buttons and Panels when both Addons are enabled. To achieve this i added a global Property to the child addon and defined a child Toolbox like this:
# In the Child Addon:
# ===================
global CHILD_ENABLED
CHILD_ENABLED = True
class PanelChildTool(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_label = _("Child Tools")
bl_idname = "child.tools"
bl_category = "Tools"
@classmethod
def poll(self, context):
global CHILD_ENABLED
return CHILD_ENABLED
....
def set_ui(is_enabled=True):
global CHILD_ENABLED
CHILD_ENABLED = is_enabled
So i can now enable/disable the drawing of Child panels. And in the master Addon's init section i added:
# In the Master Addon
# ===================
loaded_default, loaded_state = addon_utils.check("child")
if loaded_default == False and loaded_state is None:
print("Child seems to be not installed")
elif loaded_state != True :
addon_utils.enable("child", default_set=True, persistent=False, handle_error=None)
print("Enabled module 'child' On The Fly")
loaded_state = True
if loaded_state == True:
import child
child.set_ui(is_enabled=False)
print("Disabled Child's native UI")
Now the Child panels are disabled. Finally, wherever i want to load an operator from the Child into one of the master Panels, i add something similar to this to the master's panel draw() function:
if sys.modules.get("child"):
import child
child.draw_generic(context, layout) # Generic draw function provided by the child
So this appraoch is working. But is there a better solution ? thanks.