Skip to main content
1 of 4
batFINGER
  • 85.2k
  • 10
  • 114
  • 244

Modularise your modules

IMO rather than importing and cherry picking classes from submodules, instead can import the module and call its register method.

For testing have placed the addon in a testing_testing folder, and given it the "Testing" category... will of-coarse need to rename to something more suitable.

Notice the submodules are imported from testing_testing rather than from current folder . which requires the addon must have the name specified by folder. This makes the imports viable when run from main thread. (eg tested in text editor).

Can alter the submodules by adding removing other classes without the need to edit init file. Only need to edit again for a new submodule. Can use something like pkgutils.list_modules to look for modules in root folder and automate the imports of each.

Reworked code.

testing_testing/__init__.py

bl_info = {
    "name": "Popup Macro",
    "author": "",
    "version": (1, 0),
    "blender": (2, 83, 0),
    "location": "DC_MT_macro_menu",
    "description": "",
    "warning": "",
    "doc_url": "",
    "category": "Testing",
}

# can use importlib.reload here instead 
import bpy
from testing_testing import operators, menus

modules = (operators, menus)


def register():
    for m in modules:
        m.register()


def unregister():
    for m in modules:
        m.uregister()


if __name__ == "__main__":
    register()

testing_testing/operators.py

import bpy


class DC_OT_bevel_subdiv(bpy.types.Operator):
    bl_idname = "3dview.bevel_subdiv"
    bl_label = "Subdivision Workflow"

    def execute(self, context):
        ob = context.object
        bm = ob.modifiers.get("Bevel")

        bm = ob.modifiers.new(name="Bevel", type='BEVEL')
        bm.segments = 2
        bm.profile = 1
        bm.limit_method = 'WEIGHT'
        bm.miter_outer = 'MITER_ARC'
        bm.use_clamp_overlap = False

        return {'FINISHED'}


class DC_OT_bevel_second(bpy.types.Operator):
    bl_idname = "3dview.bevel_second"
    bl_label = "Second Bevel"

    def execute(self, context):
        ob = context.object
        bm = ob.modifiers.get("Bevel")

        bm = ob.modifiers.new(name="secBevel", type='BEVEL')
        bm.limit_method = 'ANGLE'
        bm.miter_outer = 'MITER_ARC'
        bm.use_clamp_overlap = False

        return {'FINISHED'}


classes = (DC_OT_bevel_second, DC_OT_bevel_subdiv,)

register, unregister = bpy.utils.register_classes_factory(classes)

if __name__ == "__main__":
    register()

testing_testing/menus.py

import bpy

class DC_MT_macro_menu(bpy.types.Menu):
    bl_idname = "DC_MT_macro_menu"
    bl_label = "Macro"

    def draw(self, context):
        layout = self.layout

        layout.operator("3dview.bevel_subdiv")
        layout.operator("3dview.bevel_second")


classes = (DC_MT_macro_menu,)

register, unregister = bpy.utils.register_classes_factory(classes)

if __name__ == "__main__":
    register()
    # test call
    bpy.ops.wm.call_menu(name=DC_MT_macro_menu.bl_idname)

The addon can be enabled,

enter image description here

after which the menu can be called from for instance the python console

enter image description here

Running menus.py in text editor will also make the menu popup via a test call.

To hang your menu onto another during register, recommend writing your own register and unregister methods instead of using the factory generated methods.

batFINGER
  • 85.2k
  • 10
  • 114
  • 244