Source code for cgl.plugins.blender.custom_menu

import importlib
import logging
import os

from cgl.core.utils.general import load_json

try:
    import bpy
except ImportError:
    pass

from importlib import reload

import cgl.plugins.CustomMenu as cm
from cgl.core.config.query import AlchemyConfigManager
from cgl.core.utils.read_write import load_text_file, save_text_lines

reload(cm)






[docs] def add_operator_buttons_to_panel(panel_file="", prefix="alchemy"): """ This function is used to add buttons to the panel in blender """ filename = os.path.basename(panel_file).replace(".py", "") this_file = panel_file.split("menus")[0] menu_file_path = f"{this_file}menus.cgl" menus = load_json(menu_file_path)["blender"] ordered_buttons = [] for m in menus: if m["name"] == filename: for i, button in enumerate(m["buttons"]): ordered_buttons.append( ["{}.{}".format(prefix, button["name"].lower()), button["label"]] ) return ordered_buttons
[docs] def get_blender_menu_file(cookbook_root, recipe_type, recipe_name): """ This function gets the blender menu file """ return ( f"{cookbook_root}/cookbook/blender/{recipe_type}/{recipe_name}/{recipe_name}.py" )
[docs] def get_blender_button_template(cookbook_root, recipe_type): """ This function gets the blender menu file template Args: recipe_type: recipe_name: Returns: """ return f"{cookbook_root}/cookbook/templates/blender/buttons/for_{recipe_type}.py"
[docs] def get_blender_menu_template(cookbook_root): """ This function gets the blender menu file template Args: cookbook_root: Returns: """ return f"{cookbook_root}/cookbook/templates/blender/PanelTemplate.py"
[docs] def create_blender_menu_file(cookbook_root, recipe_type, recipe_name, recipe_label): """ This function creates the blender menu file Args: cookbook_root: recipe_type: recipe_name: name of the recipe recipe_label: GUI friendly label of the recipe """ from cgl.core.utils.general import cgl_copy # copy the template file to the new file template_file = get_blender_menu_template(cookbook_root) new_file = get_blender_menu_file(cookbook_root, recipe_type, recipe_name) if os.path.exists(new_file): print("Blender Menu File already exists: {}".format(new_file)) return # copoy template file to new file cgl_copy(template_file, new_file) file_list = load_text_file(new_file) new_file_list = [] for line in file_list: # look for #PanelTemplate and replace it with the recipe name in camel case if "PanelTemplate" in line: new_line = line.replace("PanelTemplate", recipe_name) new_file_list.append(new_line) # look for RECIPE_LABEL and replace it with the recipe label elif "RECIPE_LABEL" in line: new_line = line.replace("RECIPE_LABEL", recipe_label) new_file_list.append(new_line) # look for RECIPE_NAME and replace it with the recipe name. elif "RECIPE_NAME" in line: new_line = line.replace("RECIPE_NAME", recipe_name) new_file_list.append(new_line) else: new_line = line new_file_list.append(new_line) # go through and replace default values with the receipe name and recipe label save_text_lines(new_file_list, new_file) print(new_file)
if __name__ == "__main__": create_blender_menu_file(r"E:/Alchemy/cgl/config", "menus", "art", "Art")