Search code examples
pythonblender

How to make a blender addon with multiple folders?


I want to make blender addons with Python, but using just one file gets very crowded. Is there a way that I can make it more than one file with folders and all that cool stuff?


Solution

  • (From https://blender.stackexchange.com/questions/105979/)

    Organize your folder structure like this:

    myaddon/
    ├── __init__.py
    ├── operators/
        ├── first_operator.py
    

    In the __init__.py do this:

    import bpy
    
    from .operators.first_operator import FirstOperator
    
    bl_info = {
        "name": "MyAddon",
        "description": "A demo addon",
        "author": "myname",
        "version": (1, 0, 0),
        "blender": (2, 7, 9),
        "wiki_url": "my github url here",
        "tracker_url": "my github url here/issues",
        "category": "Animation"
    }
    
    def register():
        bpy.utils.register_module(__name__)
    
    def unregister():
        bpy.utils.unregister_module(__name__)
    

    In the operators/first_operator.py file, do this:

    import bpy
    
    class FirstOperator(bpy.types.Operator):
        bl_label = "First Operator"
        bl_idname = "myaddon.first_operator"
        bl_description = "A demo operator"
        
        def execute(self, context):
            print("hello world")
            
            return {"FINISHED"}
    

    You'll have to install the addon to use it. You can't just run the __init__.py from the text editor in Blender to make it work.


    If you're going down this path, I'd strongly recommend using Visual Studio Code with the "Blender Development" extension, it is amazing and takes away all the trouble of having to manually load/reload the extension every time you make a change.