Naming refactor

This commit is contained in:
doc-code 2024-08-26 23:07:28 +02:00
parent 98aa91d3e5
commit 727b535cba
4 changed files with 90 additions and 101 deletions

View File

@ -14,9 +14,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import bpy
from .classes.ots import CustomTemplatesPreferences, TemplateItem, OT_ExportTemplates, OT_ImportTemplates, CUSTOM_MT_Export_Menu, OT_SelectTemplatePopup, OT_AddTemplatePopup, OT_AddTemplateItem, OT_RemoveTemplateItem, OT_MoveDownTemplateItem, OT_MoveUpTemplateItem, OT_OpenAddonPreferences
from .classes.draw import draw_file_new_templates, draw_file_default_operators
from .classes.splash import WM_MT_splash, CUSTOM_MT_splash_mode, OT_SplashCustom, OT_SplashDefault
from .classes.splash import WM_MT_splash, CT_MT_splash_mode, CT_OT_splash_custom, CT_OT_splash_default
from .classes.ots import CustomTemplatesPreferences, TemplateItem, CT_OT_export_templates, CT_OT_import_templates, CT_MT_export, CT_OT_select_template_popup, CT_OT_add_template_popup, CT_OT_add, CT_OT_remove, CT_OT_move_down, CT_OT_move_up, CT_OT_open_preferences
bl_info = {
"id": "custom_templates",
@ -31,26 +31,26 @@ bl_info = {
classes = [WM_MT_splash,
TemplateItem,
OT_ExportTemplates,
OT_ImportTemplates,
OT_SplashCustom,
OT_SplashDefault,
OT_OpenAddonPreferences,
CUSTOM_MT_splash_mode,
CUSTOM_MT_Export_Menu,
OT_MoveUpTemplateItem,
OT_MoveDownTemplateItem,
OT_AddTemplateItem,
OT_RemoveTemplateItem,
OT_AddTemplatePopup,
OT_SelectTemplatePopup,
CT_OT_export_templates,
CT_OT_import_templates,
CT_OT_splash_custom,
CT_OT_splash_default,
CT_OT_open_preferences,
CT_MT_splash_mode,
CT_MT_export,
CT_OT_move_up,
CT_OT_move_down,
CT_OT_add,
CT_OT_remove,
CT_OT_add_template_popup,
CT_OT_select_template_popup,
CustomTemplatesPreferences]
original_splash = None;
og_splash = None;
def register():
global original_splash
original_splash = bpy.types.WM_MT_splash;
global og_splash
og_splash = bpy.types.WM_MT_splash;
for c in classes:
bpy.utils.register_class(c)
bpy.types.TOPBAR_MT_file_new.append(draw_file_new_templates)
@ -59,7 +59,7 @@ def register():
def unregister():
for c in reversed(classes):
bpy.utils.unregister_class(c)
bpy.utils.register_class(original_splash)
bpy.utils.register_class(og_splash)
bpy.types.TOPBAR_MT_file_new.remove(draw_file_new_templates)
bpy.types.TOPBAR_MT_file_defaults.remove(draw_file_default_operators)

View File

@ -7,9 +7,9 @@ def draw_file_new_templates(self, context):
prefs = context.preferences.addons[base_package].preferences
if len(prefs.projects) > 0:
layout.separator()
draw_templates_on_col(layout, context)
draw_templates(layout, context)
def draw_templates_on_col(layout, context, splash_mode = False):
def draw_templates(layout, context, splash_mode = False):
prefs = context.preferences.addons[base_package].preferences
for i in range(min(5, len(prefs.projects)) if splash_mode else len(prefs.projects)):
project = prefs.projects[i]
@ -19,17 +19,14 @@ def draw_templates_on_col(layout, context, splash_mode = False):
def draw_file_default_operators(self, context):
layout = self.layout
layout.separator()
# Manage Template
layout.operator("custom_templates.open_preferences",
layout.operator("ct.open_preferences",
text="Manage templates")
# Import/Export
layout.menu("custom_templates.export_menu", text="Import/Export")
layout.menu("CT_MT_export", text="Import/Export")
layout.separator()
# Select new custom template
layout.operator("custom_templates.select_template_popup",
layout.operator("ct.select_template_popup",
text="Select new custom template")
# Use current as new template (only with an active saved .blend file opened)
if bpy.data.filepath != "":
layout.operator("custom_templates.add_template_popup",
# Only with an active saved .blend file
layout.operator("ct.add_template_popup",
text="Use current as new template")

View File

@ -1,4 +1,5 @@
from .. import __package__ as base_package
import os
import bpy
import json
from bpy.types import Operator, PropertyGroup, AddonPreferences
@ -28,14 +29,14 @@ class CustomTemplatesPreferences(AddonPreferences):
self, "projects", self, "active_template_index")
col = row.column(align=True)
col.menu("custom_templates.export_menu", icon='DOWNARROW_HLT', text="")
col.menu("CT_MT_export", icon='DOWNARROW_HLT', text="")
col.separator()
col.operator("custom_templates.add", icon='ADD', text="")
col.operator("ct.add", icon='ADD', text="")
if self.projects:
col.operator("custom_templates.remove", icon='REMOVE', text="")
col.operator("ct.remove", icon='REMOVE', text="")
col.separator()
col.operator("custom_templates.move_up", icon='TRIA_UP', text="")
col.operator("custom_templates.move_down", icon='TRIA_DOWN', text="")
col.operator("ct.move_up", icon='TRIA_UP', text="")
col.operator("ct.move_down", icon='TRIA_DOWN', text="")
if self.projects:
project = self.projects[self.active_template_index]
@ -55,22 +56,21 @@ class CustomTemplatesPreferences(AddonPreferences):
if len(self.projects) > 5:
box.label(text="Note: Only the first 5 templates in the list will be shown in the splash screen.")
class CUSTOM_MT_Export_Menu(bpy.types.Menu):
bl_idname = "custom_templates.export_menu"
class CT_MT_export(bpy.types.Menu):
bl_label = "Import/Export custom templates"
bl_description = "Allows you to save al load your custom templates (using json format)"
def draw(self, context):
layout = self.layout
layout.operator("custom_templates.export_templates", text="Export templates")
layout.operator("custom_templates.import_templates", text="Import templates")
layout.operator("ct.export_templates", text="Export templates")
layout.operator("ct.import_templates", text="Import templates")
class OT_ExportTemplates(bpy.types.Operator):
bl_idname = "custom_templates.export_templates"
class CT_OT_export_templates(bpy.types.Operator):
bl_idname = "ct.export_templates"
bl_label = "Export custom templates"
bl_description = "Export the current list of templates to JSON file"
filepath: StringProperty(subtype="FILE_PATH", description="Select the path for the exported file", default="custom_templates.json")
filepath: StringProperty(subtype="FILE_PATH", description="Select the path for the exported file", default="templates.json")
def execute(self, context):
prefs = context.preferences.addons[base_package].preferences
@ -85,8 +85,8 @@ class OT_ExportTemplates(bpy.types.Operator):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
class OT_ImportTemplates(bpy.types.Operator):
bl_idname = "custom_templates.import_templates"
class CT_OT_import_templates(bpy.types.Operator):
bl_idname = "ct.import_templates"
bl_label = "Import custom templates"
bl_description = "Import a list of templates from JSON file (note that this will override the current templates list)"
@ -95,6 +95,7 @@ class OT_ImportTemplates(bpy.types.Operator):
def execute(self, context):
prefs = context.preferences.addons[base_package].preferences
if os.path.exists(self.filepath):
with open(self.filepath, 'r') as f:
projects = json.load(f)
@ -106,6 +107,8 @@ class OT_ImportTemplates(bpy.types.Operator):
prefs.active_template_index = 0
self.report({'INFO'}, f"Projects imported from {self.filepath}")
else:
self.report({'WARNING'}, f"Import cancelled: path not found ({self.filepath})")
return {'FINISHED'}
def invoke(self, context, event):
@ -113,8 +116,8 @@ class OT_ImportTemplates(bpy.types.Operator):
return {'RUNNING_MODAL'}
# Templates list oeprators
class OT_AddTemplateItem(Operator):
bl_idname = "custom_templates.add"
class CT_OT_add(Operator):
bl_idname = "ct.add"
bl_label = "Add Template"
bl_description = "Add new template"
@ -122,25 +125,22 @@ class OT_AddTemplateItem(Operator):
prefs = context.preferences.addons[base_package].preferences
prefs.projects.add()
prefs.active_template_index = len(prefs.projects) - 1
self.report({'INFO'}, f"Empty template added")
return {'FINISHED'}
class OT_RemoveTemplateItem(Operator):
bl_idname = "custom_templates.remove"
class CT_OT_remove(Operator):
bl_idname = "ct.remove"
bl_label = "Remove Template"
bl_description = "Remove selected template"
def execute(self, context):
prefs = context.preferences.addons[base_package].preferences
self.report(
{'INFO'}, f'Template "{prefs.projects[prefs.active_template_index].name}" removed{" (`"+prefs.projects[prefs.active_template_index].path+"`)" if prefs.projects[prefs.active_template_index].path != "" else "" }')
prefs.projects.remove(prefs.active_template_index)
prefs.active_template_index = min(
max(0, prefs.active_template_index - 1), len(prefs.projects) - 1)
return {'FINISHED'}
class OT_MoveUpTemplateItem(Operator):
bl_idname = "custom_templates.move_up"
class CT_OT_move_up(Operator):
bl_idname = "ct.move_up"
bl_label = "Move Up"
bl_description = "Move the selected template up in the list"
@ -151,14 +151,13 @@ class OT_MoveUpTemplateItem(Operator):
if index > 0:
prefs.projects.move(index, index - 1)
prefs.active_template_index -= 1
self.report({'INFO'}, f"Templates list re-ordered")
else:
self.report({'WARNING'}, "Template is already at the top")
return {'FINISHED'}
class OT_MoveDownTemplateItem(Operator):
bl_idname = "custom_templates.move_down"
class CT_OT_move_down(Operator):
bl_idname = "ct.move_down"
bl_label = "Move Down"
bl_description = "Move the selected template down in the list"
@ -169,14 +168,13 @@ class OT_MoveDownTemplateItem(Operator):
if index < len(prefs.projects) - 1:
prefs.projects.move(index, index + 1)
prefs.active_template_index += 1
self.report({'INFO'}, f"Templates list re-ordered")
else:
self.report({'WARNING'}, "Template is already at the bottom")
return {'FINISHED'}
# Popups of File > Defaults
def check_if_present(self, prefs, path):
def already_present(self, prefs, path):
for p in prefs.projects:
if p.path == path:
already_present = True
@ -185,8 +183,8 @@ def check_if_present(self, prefs, path):
return True
return False
class OT_AddTemplatePopup(Operator):
bl_idname = "custom_templates.add_template_popup"
class CT_OT_add_template_popup(Operator):
bl_idname = "ct.add_template_popup"
bl_label = "Use as template"
bl_description = "Use the current .blend file to create a new template occurency"
@ -195,14 +193,11 @@ class OT_AddTemplatePopup(Operator):
def execute(self, context):
prefs = context.preferences.addons[base_package].preferences
current_file_path = bpy.data.filepath
if check_if_present(self, prefs, current_file_path):
return {'FINISHED'}
if not already_present(self, prefs, current_file_path):
if current_file_path:
new_project = prefs.projects.add()
new_project.name = self.project_name
new_project.path = current_file_path
self.report(
{'INFO'}, f"Template '{self.project_name}' added successfully!")
else:
self.report({'ERROR'}, "Current file is not saved on disk.")
return {'FINISHED'}
@ -210,8 +205,8 @@ class OT_AddTemplatePopup(Operator):
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
class OT_SelectTemplatePopup(Operator):
bl_idname = "custom_templates.select_template_popup"
class CT_OT_select_template_popup(Operator):
bl_idname = "ct.select_template_popup"
bl_label = "Select a new custom template"
bl_description = "Create a new template occurency by selecting an existing .blend file"
@ -220,21 +215,18 @@ class OT_SelectTemplatePopup(Operator):
def execute(self, context):
prefs = context.preferences.addons[base_package].preferences
if check_if_present(self, prefs, self.project_path):
return {'FINISHED'}
if not already_present(self, prefs, self.project_path):
new_project = prefs.projects.add()
new_project.name = self.project_name
new_project.path = self.project_path
self.report(
{'INFO'}, f"Template '{self.project_name}' selected and added successfully!")
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
# Open Preferences
class OT_OpenAddonPreferences(Operator):
bl_idname = "custom_templates.open_preferences"
class CT_OT_open_preferences(Operator):
bl_idname = "ct.open_preferences"
bl_label = "Open Custom Templates Preferences"
bl_description = "Open the preferences for the Custom Templates add-on"

View File

@ -1,7 +1,7 @@
# Ref WM_MT_splash https://projects.blender.org/blender/blender/src/commit/5a9fe638dedb179050c4929ea8fcdec80d221af2/scripts/startup/bl_operators/wm.py#L3296
# Ref TOPBAR_MT_file_new https://projects.blender.org/blender/blender/src/commit/5a9fe638dedb179050c4929ea8fcdec80d221af2/scripts/startup/bl_ui/space_topbar.py#L286
from .. import __package__ as base_package
from .draw import draw_templates_on_col
from .draw import draw_templates
import bpy
# Clone of the WM_MT_splash (the section under the image of the Splash)
# It's edited (only in #Templates part) to add a menu in the New File section
@ -24,13 +24,13 @@ class WM_MT_splash(bpy.types.Menu):
if prefs.override_splash and len(prefs.projects) > 0:
colA.label(text="Custom Templates")
col1.operator_context = 'INVOKE_DEFAULT'
draw_templates_on_col(col1, context, True)
draw_templates(col1, context, True)
else:
colA.label(text="New File")
# Call original code
bpy.types.TOPBAR_MT_file_new.draw_ex(col1, context, use_splash=True)
colB = ct_split.column()
colB.menu("custom_templates.splash_mode", icon='DOWNARROW_HLT', text="")
colB.menu("CT_MT_splash_mode", icon='DOWNARROW_HLT', text="")
# Recent
col2 = split.column()
@ -72,8 +72,8 @@ class WM_MT_splash(bpy.types.Menu):
layout.separator()
class CUSTOM_MT_splash_mode(bpy.types.Menu):
bl_idname = "custom_templates.splash_mode"
class CT_MT_splash_mode(bpy.types.Menu):
bl_idname = "CT_MT_splash_mode"
bl_label = "Custom Templates Switch"
bl_description = "Swtich between Blender's default and your Custom Templates"
@ -87,13 +87,13 @@ class CUSTOM_MT_splash_mode(bpy.types.Menu):
else:
def_check = "CHECKMARK"
layout.operator("custom_templates.open_preferences", text="Manage templates")
layout.operator("ct.open_preferences", text="Manage templates")
layout.separator()
layout.operator("custom_templates.splash_custom", text="Use Custom Templates", icon=ct_check)
layout.operator("custom_templates.splash_default", text="Use Blender's Default", icon=def_check)
layout.operator("ct.splash_custom", text="Use Custom Templates", icon=ct_check)
layout.operator("ct.splash_default", text="Use Blender's Default", icon=def_check)
class OT_SplashDefault(bpy.types.Operator):
bl_idname = "custom_templates.splash_default"
class CT_OT_splash_default(bpy.types.Operator):
bl_idname = "ct.splash_default"
bl_label = "Display default Blender's templates"
bl_description = "Use Blender's default templates in the Splash Screen"
@ -102,8 +102,8 @@ class OT_SplashDefault(bpy.types.Operator):
prefs.override_splash = False
return {'FINISHED'}
class OT_SplashCustom(bpy.types.Operator):
bl_idname = "custom_templates.splash_custom"
class CT_OT_splash_custom(bpy.types.Operator):
bl_idname = "ct.splash_custom"
bl_label = "Display your custom templates"
bl_description = "Use your custom templates in the Splash Screen (limited to first 5)"