├── .gitignore ├── LICENSE ├── README.md ├── blender_menu_structure ├── __init__.py └── icons │ ├── arrow.png │ ├── arrowBlue.png │ └── heart.png └── screenshot.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | .vscode/settings.json 131 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Frieder Erdmann 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blender Menus 2 | A simple menu generator for the Blender UI system 3 | 4 | ![Screenshot](https://github.com/friedererdmann/blender_menus/blob/master/screenshot.jpg?raw=true "Screenshot of generated menu system") 5 | 6 | ## Testing 7 | Throw into your addon folder, activate the addon (`blender_menu_structure` or "Dynamically created Blender Menu" in the UI) - see the menu! 8 | 9 | ## Extending the menu 10 | You can very simply just extend the dictionary `menu_hierarchy` if you just want to use the system as is. 11 | 12 | ### Menu Hierarchy 13 | * ---: Dashes become separators (regardless how many you use) 14 | * "menu" key is how we put in a submenu (as a dictionary value) 15 | * "operator" becomes an operator 16 | * Menus and Operators accept icons (either by Blender keyword or as png from the icon folder) 17 | * Operators accept tooltips 18 | 19 | ### Functions and Parameters 20 | Your menu entries can provide a function, but no parameters. We provide an example of using partial if you want to pass a function with parameters. 21 | 22 | ## Extending logic 23 | The `build_menus` and the corresponding `menu_hierarchy` dictionaries are meant as example code. It should be easy from this to extend the logic and build more meaningful menu systems, e.g. checking for naming clashes with operators. 24 | 25 | If you want to contribute to this project, please feel free to send back your pushes. 26 | 27 | ### Lack of proper register and unregister 28 | We do not register and unregister our menu and operator classes properly, so this would be another good step to look into, if you're worried about users loading and unloading your addon or you wanting to re-run the addon with updated menus. 29 | -------------------------------------------------------------------------------- /blender_menu_structure/__init__.py: -------------------------------------------------------------------------------- 1 | bl_info = { 2 | "name": "Dynamically created Blender Menu", 3 | "author": "Frieder Erdmann, Boris Ignjatovic", 4 | "blender": (2, 80, 3), 5 | "location": "Menu > Submenus", 6 | "description": "A test to create dynamic menus", 7 | "warning": "", 8 | "wiki_url": "", 9 | "category": "UI" 10 | } 11 | 12 | import os 13 | from functools import partial 14 | import bpy 15 | from bpy.utils import previews 16 | 17 | 18 | imageloader = None 19 | 20 | 21 | class ImageLoader(): 22 | """ImageLoader to wrap previews into a singleton. 23 | Heavily inspired by Embark tools - thanks! 24 | """ 25 | 26 | def __init__(self): 27 | self.preview_handler = previews.new() 28 | 29 | def blender_icon(self, icon="NONE"): 30 | if icon.lower().endswith(".png"): 31 | icon = self.load_icon(icon) 32 | return self.preview_handler[icon].icon_id 33 | 34 | def load_icon(self, filename="image.png"): 35 | script_dir = os.path.dirname(__file__) 36 | icon_dir = os.path.join(script_dir, "icons") 37 | filepath = os.path.join(icon_dir, filename) 38 | name = os.path.splitext(filename)[0] 39 | self.preview_handler.load(name, filepath, 'IMAGE') 40 | return name 41 | 42 | def unregister(self): 43 | previews.remove(self.preview_handler) 44 | self.preview_handler = None 45 | 46 | 47 | def add_menu(name, icon_value=0, parent_name="TOPBAR_MT_editor_menus"): 48 | """Adds a menu class and adds it to any menu. 49 | 50 | Args: 51 | name (str): Name of the menu 52 | parent_name (str, optional): What Blender menu should this be added into. Defaults to "TOPBAR_MT_editor_menus". 53 | """ 54 | 55 | def draw(self, context): 56 | pass 57 | 58 | def menu_draw(self, context): 59 | self.layout.menu(my_menu_class.bl_idname, icon_value=icon_value) 60 | 61 | bl_idname = "MENU_MT_{0}".format(name.replace(" ", "")) 62 | full_parent_name = "bpy.types.{0}".format(parent_name) 63 | 64 | parent = eval(full_parent_name) 65 | 66 | my_menu_class = type( 67 | "DynamicMenu{0}".format(name), 68 | (bpy.types.Menu,), 69 | { 70 | "bl_idname": bl_idname, 71 | "bl_label": name, 72 | "draw": draw 73 | }) 74 | 75 | bpy.utils.register_class(my_menu_class) 76 | parent.append(menu_draw) 77 | 78 | return bl_idname 79 | 80 | 81 | def add_operator(name, callback, icon_value=0, tooltip='Dynamic Operator', parent_name="TOPBAR_MT_editor_menus"): 82 | """Adds a dummy operator to be able to add your callback into any menu. 83 | 84 | Args: 85 | name (str): Name of the menu entry 86 | callback (function): What method do you want to callback to from here 87 | parent_name (str, optional): What Blender menu should this be added into. Defaults to "TOPBAR_MT_editor_menus". 88 | """ 89 | 90 | def execute(self, context): 91 | my_operator_class.func() 92 | return {"FINISHED"} 93 | 94 | def operatator_draw(self, context): 95 | self.layout.operator(my_operator_class.bl_idname, icon_value=icon_value) 96 | 97 | bl_idname = "menuentry.{0}".format(name.replace(" ", "").lower()) 98 | full_parent_name = "bpy.types.{0}".format(parent_name) 99 | parent = eval(full_parent_name) 100 | my_operator_class = type( 101 | "DynamicOperator{0}".format(name), 102 | (bpy.types.Operator,), 103 | { 104 | "bl_idname": bl_idname, 105 | "bl_label": name, 106 | "func": callback, 107 | "execute": execute, 108 | "__doc__": tooltip 109 | }) 110 | bpy.utils.register_class(my_operator_class) 111 | parent.append(operatator_draw) 112 | 113 | return bl_idname 114 | 115 | 116 | def add_separator(parent_name="TOPBAR_MT_editor_menus"): 117 | """Adds a separator to the menu identified in parent_name 118 | 119 | Args: 120 | parent_name (str, optional): What Blender menu should this be added into. Defaults to "TOPBAR_MT_editor_menus". 121 | """ 122 | 123 | def draw_separator(self, context): 124 | self.layout.row().separator() 125 | 126 | full_parent_name = "bpy.types.{0}".format(parent_name) 127 | parent = eval(full_parent_name) 128 | parent.append(draw_separator) 129 | 130 | 131 | def build_menus(menu_dict, parent_name="TOPBAR_MT_editor_menus"): 132 | """A simple recursive menu builder without too much logic or extra data. 133 | 134 | Args: 135 | menu_dict (dict): A dictionary with entries for submenus and operators. 136 | parent_name (str, optional): What Blender menu should this be nested into. Defaults to "TOPBAR_MT_editor_menus". 137 | """ 138 | 139 | for key in menu_dict: 140 | if key.count("-") == len(key): 141 | add_separator(parent_name) 142 | entry = menu_dict[key] 143 | icon = 0 144 | tooltip = "" 145 | if "icon" in entry: 146 | icon = imageloader.blender_icon(entry["icon"]) 147 | if "tooltip" in entry: 148 | tooltip = entry["tooltip"] 149 | if "menu" in entry: 150 | bl_idname = add_menu( 151 | name=key, 152 | icon_value=icon, 153 | parent_name=parent_name) 154 | build_menus(entry["menu"], bl_idname) 155 | elif "operator" in entry: 156 | add_operator( 157 | name=key, 158 | callback=entry["operator"], 159 | icon_value=icon, 160 | tooltip=tooltip, 161 | parent_name=parent_name) 162 | 163 | 164 | def an_example(): 165 | """A simple example method to call in our example menu. 166 | """ 167 | 168 | print("An Example!") 169 | 170 | 171 | def an_example_with(parameter): 172 | """A simple example method with a parameter to showcase 173 | how to add it in our menu. 174 | 175 | Args: 176 | parameter (str, int, float): We will call print() on this. 177 | """ 178 | 179 | print(parameter) 180 | 181 | 182 | menu_hierarchy = { 183 | "My Menu": { 184 | "icon": "heart.png", 185 | "menu": { 186 | "A Menu Entry": { 187 | "icon": "arrow.png", 188 | "tooltip": "An example of a menu entry", 189 | "operator": an_example}, 190 | "Another": { 191 | "icon": "arrowBlue.png", 192 | "tooltip": "An example of a menu entry with parameters", 193 | "operator": partial(an_example_with, "This works!")}, 194 | "----": {}, 195 | "A Sub Menu": { 196 | "menu": { 197 | "With One More Entry": { 198 | "operator": partial(an_example_with, "This works too!"), 199 | "tooltip": "I even have a tooltip"} 200 | } 201 | } 202 | } 203 | } 204 | } 205 | 206 | 207 | def register(): 208 | global imageloader 209 | if imageloader is None: 210 | imageloader = ImageLoader() 211 | build_menus(menu_hierarchy) 212 | 213 | 214 | def unregister(): 215 | global imageloader 216 | if imageloader is not None: 217 | imageloader.unregister() 218 | imageloader = None 219 | 220 | 221 | if __name__ == "__main__": 222 | register() 223 | -------------------------------------------------------------------------------- /blender_menu_structure/icons/arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friedererdmann/blender_menus/1896dcf5c07676a1dccc3c252980e9a26f4a3c0b/blender_menu_structure/icons/arrow.png -------------------------------------------------------------------------------- /blender_menu_structure/icons/arrowBlue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friedererdmann/blender_menus/1896dcf5c07676a1dccc3c252980e9a26f4a3c0b/blender_menu_structure/icons/arrowBlue.png -------------------------------------------------------------------------------- /blender_menu_structure/icons/heart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friedererdmann/blender_menus/1896dcf5c07676a1dccc3c252980e9a26f4a3c0b/blender_menu_structure/icons/heart.png -------------------------------------------------------------------------------- /screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/friedererdmann/blender_menus/1896dcf5c07676a1dccc3c252980e9a26f4a3c0b/screenshot.jpg --------------------------------------------------------------------------------