2024-09-23 02:06:50 +02:00

78 lines
2.9 KiB
Python

import os
import bpy
from .. import __name__ as base_package
def already_present(self, prefs, path, report=True):
for p in prefs.projects:
if p.path == path:
if report:
self.report(
{'WARNING'}, f'Current file is already in the templates list as "{p.name}".')
return True
return False
def name_from_path(path):
if path:
return os.path.splitext(os.path.basename(path))[0]
else:
return ""
def on_path_update(self, context):
if not self.name and self.path:
self.name = name_from_path(self.path)
context.preferences.is_dirty = True
if self.path and self.path.startswith('//'):
self.path = bpy.path.abspath(self.path)
context.preferences.is_dirty = True
def pref():
return bpy.context.preferences.addons[base_package].preferences
def has_templates():
return len(pref().projects) > 0
def draw_file_new_templates(self, context):
layout = self.layout
if has_templates():
layout.separator()
draw_templates(layout, context)
def draw_templates(layout, context, splash_mode=False):
prefs = pref()
for i in range(min(5 if len(prefs.projects) <= 5 else 4, len(prefs.projects)) if splash_mode else len(prefs.projects)):
project = prefs.projects[i]
if project.path:
layout.operator(
"wm.read_homefile", text=(project.name if project.name else name_from_path(project.path)), icon="FILE_NEW" if splash_mode else "NONE").filepath = project.path
if splash_mode and len(prefs.projects) > 5:
layout.menu("CT_MT_more_templates", text="More...", icon="FILE_NEW")
if not splash_mode and prefs.start_from and context.area.type == 'TOPBAR':
layout.separator()
layout.operator("ct.start_from", icon="FILE_FOLDER")
def draw_file_default_operators(self, context):
layout = self.layout
layout.separator()
layout.operator("ct.open_preferences",
text="Manage templates")
layout.operator("ct.add_templates_from_folder",
text="Add from folder", icon="ADD")
layout.operator("ct.clear", text="Clear current templates", icon="TRASH")
layout.separator()
layout.operator("ct.import_templates",
text="Import templates", icon="IMPORT")
layout.operator("ct.export_templates",
text="Export templates", icon="EXPORT")
layout.separator()
layout.operator("ct.select_template_popup",
text="Select new template")
if bpy.data.filepath != "":
layout.operator("ct.add_template_popup",
text="Use current file as template")
def draw_ws_menu_add(self, context):
layout = self.layout
if has_templates():
layout.separator()
layout.menu("CT_MT_workspace_add", text="Add from Custom Templates", icon="WORKSPACE")