import bpy import re from bpy.types import Operator from bpy.props import StringProperty, BoolProperty from .utils import get_addon_prefs # --- OPS --- ## multi-prefix solution (Caps letters) class PROJ_OT_layer_name_build(Operator): bl_idname = "gp.layer_name_build" bl_label = "Layer Name Build" bl_description = "Change prefix of layer name" bl_options = {"REGISTER", "UNDO"} @classmethod def poll(cls, context): return True prefix : StringProperty(default='', options={'SKIP_SAVE'}) prefix2 : StringProperty(default='', options={'SKIP_SAVE'}) desc : StringProperty(default='', options={'SKIP_SAVE'}) def execute(self, context): prefs = get_addon_prefs() sep = prefs.separator ob = context.object gpl = ob.data.layers act = gpl.active if not act: self.report({'ERROR'}, 'no layer active') return {"CANCELLED"} name = act.info # pattern = r'([A-Z]{2})?_?([A-Z]{2})?_?(.*)' # bad ! match whithout separator # pattern = r'(?:(^[A-Z]{2})_)?(?:([A-Z]{2})_)?(.*)' # matching only two letter # pattern = r'^([A-Z]{2}_)?([A-Z]{2}_)?(.*)' # matching letters with separator pattern = r'^([A-Z]{1,6}_)?([A-Z]{1,6}_)?(.*)' # matching capital letters from one to six pattern = pattern.replace('_', sep) # set separator res = re.search(pattern, name) p1, p2, p3 = res.group(1), res.group(2), res.group(3) ## empty instead of None p1 = '' if p1 is None else p1 p2 = '' if p2 is None else p2 p3 = '' if p3 is None else p3 if self.prefix: if self.prefix == 'prefixkillcode': p1 = '' else: p1 = self.prefix.upper() + sep if self.prefix2: p2 = self.prefix2.upper() + sep if self.desc: p3 = self.desc new = f'{p1}{p2}{p3}' act.info = new return {"FINISHED"} ## --- UI --- def layer_name_builder(self, context): prefs = get_addon_prefs() if not prefs.prefixes: return layout = self.layout # {'EDIT_GPENCIL', 'PAINT_GPENCIL','SCULPT_GPENCIL','WEIGHT_GPENCIL', 'VERTEX_GPENCIL'} # layout.separator() col = layout.column() all_prefixes = prefs.prefixes.split(',') line_limit = 8 ## first prefix for i, prefix in enumerate(all_prefixes): if i % line_limit == 0: row = col.row(align=True) row.operator("gp.layer_name_build", text=prefix.upper() ).prefix = prefix row.operator("gp.layer_name_build", text='', icon='X').prefix = 'prefixkillcode' ## secondary prefix # row = layout.row(align=True) # for task in prefs.prefixes: # 'PO', 'AN' # row.operator("me.set_layer_name", text=task).prefix2 = task row = col.row(align=True) row.prop(context.scene.gptoolprops, 'layer_name', text='') row.operator("gp.layer_name_build", text='', icon='EVENT_RETURN').desc = context.scene.gptoolprops.layer_name classes=( PROJ_OT_layer_name_build, ) def register(): for cls in classes: bpy.utils.register_class(cls) bpy.types.DATA_PT_gpencil_layers.prepend(layer_name_builder) def unregister(): bpy.types.DATA_PT_gpencil_layers.remove(layer_name_builder) for cls in reversed(classes): bpy.utils.unregister_class(cls)