fix refacto

pull/5/head
“christopheseux” 2023-05-02 20:46:45 +02:00
parent 41a4c7a3a4
commit 4a3042030f
6 changed files with 94 additions and 75 deletions

View File

@ -15,7 +15,7 @@ bl_info = {
import sys import sys
from pathlib import Path from pathlib import Path
sys.modules['vse_toolbox'] = sys.modules.pop(Path(__file__).parent.name) #sys.modules['vse_toolbox'] = sys.modules.pop(Path(__file__).parent.name)
from vse_toolbox import ui from vse_toolbox import ui
from vse_toolbox import operators from vse_toolbox import operators

View File

@ -50,8 +50,8 @@ class VSETB_OT_load_settings(Operator):
addon_config['trackers'] = addon_config.get('trackers') addon_config['trackers'] = addon_config.get('trackers')
trackers = addon_config.pop('trackers') trackers = addon_config.pop('trackers')
addon_config['spreadsheet'] = addon_config.get('spreadsheet') addon_config['spreadsheet_export'] = addon_config.get('spreadsheet_export')
spreadsheet = addon_config.pop('spreadsheet') spreadsheet_export_config = addon_config.pop('spreadsheet_export')
project_name = addon_config.get('project_name') project_name = addon_config.get('project_name')
if project_name: if project_name:
@ -64,30 +64,32 @@ class VSETB_OT_load_settings(Operator):
except Exception: except Exception:
print(f'Could not set property {k} with value {v} to project {settings.project_name}') print(f'Could not set property {k} with value {v} to project {settings.project_name}')
if spreadsheet.get('cells'):
#project.spreadsheet.clear() export_cells = project.spreadsheet_export.cells
for k, v in spreadsheet_export_config.items():
if k == 'cells':
#project.spreadsheet.clear()
for i, cell_data in enumerate(spreadsheet['cells']): for i, cell_data in enumerate(v):
if not 'name' in cell_data: if not 'name' in cell_data:
print(f'cell_data {cell_data} need to have a attribute name') print(f'cell_data {cell_data} need to have a attribute name')
continue continue
cell = project.spreadsheet.get(cell_data['name']) cell = export_cells.get(cell_data['name'])
if not cell: if not cell:
print(f"cell {cell_data['name']} not in spreadsheet") print(f"cell {cell_data['name']} not in spreadsheet")
continue continue
project.spreadsheet.move(list(project.spreadsheet).index(cell), i) export_cells.move(list(export_cells).index(cell), i)
for prop_name in ('export_name', 'enabled'): for prop_name in ('export_name', 'enabled'):
if prop_name in cell_data: if prop_name in cell_data:
setattr(cell, prop_name, cell_data[prop_name]) setattr(cell, prop_name, cell_data[prop_name])
if spreadsheet.get('options'): else:
for k, v in spreadsheet['options'].items():
try: try:
setattr(project.spreadsheet_options, k, v) setattr(project.spreadsheet_export, k, v)
except Exception: except Exception:
print(f'Could not set option {k} with value {v} to spreadsheet') print(f'Could not set option {k} with value {v} to spreadsheet')

View File

@ -17,21 +17,21 @@ from vse_toolbox.bl_utils import (get_addon_prefs, get_scene_settings)
from vse_toolbox.file_utils import open_file, install_module from vse_toolbox.file_utils import open_file, install_module
class VSETB_MT_spreadsheet_presets(Menu): class VSETB_MT_export_spreadsheet_presets(Menu):
bl_label = 'Presets' bl_label = 'Presets'
preset_subdir = 'vse_toolbox' preset_subdir = 'vse_toolbox'
preset_operator = 'script.execute_preset' preset_operator = 'script.execute_preset'
draw = Menu.draw_preset draw = Menu.draw_preset
class VSETB_OT_add_spreadsheet_preset(AddPresetBase, Operator): class VSETB_OT_add_export_spreadsheet_preset(AddPresetBase, Operator):
bl_idname = "vse_toolbox.add_spreadsheet_preset" bl_idname = "vse_toolbox.add_spreadsheet_preset"
bl_label = "Add Spreadsheet Preset" bl_label = "Add Spreadsheet Preset"
bl_description = "Add Spreadsheet Preset" bl_description = "Add Spreadsheet Preset"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
preset_menu = 'VSETB_MT_spreadsheet_presets' preset_menu = 'VSETB_MT_export_spreadsheet_presets'
#preset_menu = 'VSETB_OT_MT_spreadsheet_presets' #preset_menu = 'VSETB_OT_MT_spreadsheet_presets'
@ -45,16 +45,15 @@ class VSETB_OT_add_spreadsheet_preset(AddPresetBase, Operator):
# Properties to store in the preset # Properties to store in the preset
preset_values = [ preset_values = [
'project.spreadsheet', 'project.spreadsheet_export',
'project.spreadsheet_options'
] ]
# Directory to store the presets # Directory to store the presets
preset_subdir = 'vse_toolbox' preset_subdir = 'vse_toolbox/spreadsheet_export'
class VSETB_OT_spreadsheet_move(Operator): class VSETB_OT_spreadsheet_cell_move(Operator):
bl_idname = "vse_toolbox.spreadsheet_move" bl_idname = "vse_toolbox.spreadsheet_move"
bl_label = "Move Spreadsheet items" bl_label = "Move Spreadsheet items"
bl_description = "Move Spreadsheet items" bl_description = "Move Spreadsheet items"
@ -71,22 +70,24 @@ class VSETB_OT_spreadsheet_move(Operator):
scn = context.scene scn = context.scene
project = get_scene_settings().active_project project = get_scene_settings().active_project
idx = project.spreadsheet_index spreadsheet = project.spreadsheet_export
cells = spreadsheet.cells
idx = spreadsheet.cell_index
try: try:
item = project.spreadsheet[idx] item = cells[idx]
except IndexError: except IndexError:
pass pass
else: else:
if self.direction == 'DOWN' and idx < len(project.spreadsheet) - 1: if self.direction == 'DOWN' and idx < len(cells) - 1:
item_next = project.spreadsheet[idx+1].name item_next = cells[idx+1].name
project.spreadsheet.move(idx, idx+1) cells.move(idx, idx+1)
project.spreadsheet_index += 1 spreadsheet.cell_index += 1
elif self.direction == 'UP' and idx >= 1: elif self.direction == 'UP' and idx >= 1:
item_prev = project.spreadsheet[idx-1].name item_prev = cells[idx-1].name
project.spreadsheet.move(idx, idx-1) cells.move(idx, idx-1)
project.spreadsheet_index -= 1 spreadsheet.cell_index -= 1
return {"FINISHED"} return {"FINISHED"}
@ -112,12 +113,13 @@ class VSETB_OT_export_spreadsheet(Operator):
scn = context.scene scn = context.scene
settings = get_scene_settings() settings = get_scene_settings()
project = settings.active_project project = settings.active_project
options = project.spreadsheet_options spreadsheet = project.spreadsheet_export
#options = project.spreadsheet_options
layout = self.layout layout = self.layout
row = layout.row() row = layout.row()
row.template_list("VSETB_UL_spreadsheet", "spreadsheet", project, "spreadsheet", project, "spreadsheet_index", rows=8) row.template_list("VSETB_UL_spreadsheet", "spreadsheet_export", spreadsheet, "cells", spreadsheet, "cell_index", rows=8)
col_tool = row.column(align=True) col_tool = row.column(align=True)
@ -137,31 +139,31 @@ class VSETB_OT_export_spreadsheet(Operator):
row = col.row(align=True, heading='Custom Name') row = col.row(align=True, heading='Custom Name')
#row.use_property_split = True #row.use_property_split = True
row.prop(options, 'use_custom_name', text='') row.prop(spreadsheet, 'use_custom_name', text='')
sub = row.row(align=True) sub = row.row(align=True)
sub.enabled = options.use_custom_name sub.enabled = spreadsheet.use_custom_name
sub.prop(options, 'custom_name', text='') sub.prop(spreadsheet, 'custom_name', text='')
col.separator() col.separator()
row = col.row(align=False) row = col.row(align=False)
row.prop(options, "format", expand=True, text='Format') row.prop(spreadsheet, "format", expand=True, text='Format')
row.prop(options, 'show_settings', text='', icon='PREFERENCES') row.prop(spreadsheet, 'show_settings', text='', icon='PREFERENCES')
if options.show_settings: if spreadsheet.show_settings:
col.prop(options, "separator", expand=True, text='Separator') col.prop(spreadsheet, "separator", expand=True, text='Separator')
if options.format == 'CSV': if spreadsheet.format == 'CSV':
col.prop(options, "delimiter", expand=True, text='Delimiter') col.prop(spreadsheet, "delimiter", expand=True, text='Delimiter')
col.separator() col.separator()
col.prop(options, 'open_folder', text='Open Folder') col.prop(spreadsheet, 'open_folder', text='Open Folder')
col.prop(options, 'export_path', text='Export Path') col.prop(spreadsheet, 'export_path', text='Export Path')
def execute(self, context): def execute(self, context):
#self.report({'ERROR'}, f'Export not implemented yet.') #self.report({'ERROR'}, f'Export not implemented yet.')
prefs = get_addon_prefs() prefs = get_addon_prefs()
settings = get_scene_settings() settings = get_scene_settings()
project = settings.active_project project = settings.active_project
options = project.spreadsheet_options spreadsheet = project.spreadsheet_export
episode = settings.active_episode episode = settings.active_episode
cells = [cell for cell in project.spreadsheet if cell.enabled] cells = [cell for cell in project.spreadsheet if cell.enabled]
@ -170,8 +172,8 @@ class VSETB_OT_export_spreadsheet(Operator):
# Header # Header
rows.append([cell.export_name for cell in cells]) rows.append([cell.export_name for cell in cells])
separator = options.separator.replace('\\n', '\n').replace('\\t', '\t').replace('\\r', '\r') separator = spreadsheet.separator.replace('\\n', '\n').replace('\\t', '\t').replace('\\r', '\r')
delimiter = options.delimiter.replace('\\n', '\n').replace('\\t', '\t').replace('\\r', '\r') delimiter = spreadsheet.delimiter.replace('\\n', '\n').replace('\\t', '\t').replace('\\r', '\r')
for strip in get_strips('Shots'): for strip in get_strips('Shots'):
row = [] row = []
@ -185,11 +187,11 @@ class VSETB_OT_export_spreadsheet(Operator):
if not asset.asset_type == cell.name: if not asset.asset_type == cell.name:
continue continue
if options.use_custom_name: if spreadsheet.use_custom_name:
if asset.get('metadata', {}).get(options.custom_name): if asset.get('metadata', {}).get(spreadsheet.custom_name):
asset_castings.append(asset['metadata'][options.custom_name]) asset_castings.append(asset['metadata'][spreadsheet.custom_name])
else: else:
self.report({'ERROR'}, f'The asset {asset.tracker_name} has no data {options.custom_name}') self.report({'ERROR'}, f'The asset {asset.tracker_name} has no data {spreadsheet.custom_name}')
else: else:
asset_castings.append(asset.tracker_name) asset_castings.append(asset.tracker_name)
@ -209,7 +211,7 @@ class VSETB_OT_export_spreadsheet(Operator):
#print(rows) #print(rows)
export_path = Path(os.path.abspath(bpy.path.abspath(options.export_path))) export_path = Path(os.path.abspath(bpy.path.abspath(spreadsheet.export_path)))
export_name = export_path.name export_name = export_path.name
if export_path.suffix or export_name.endswith('{ext}'): if export_path.suffix or export_name.endswith('{ext}'):
@ -224,7 +226,7 @@ class VSETB_OT_export_spreadsheet(Operator):
date = datetime.now().strftime('%Y_%m_%d') date = datetime.now().strftime('%Y_%m_%d')
project_name = project.name.replace(' ', '_').lower() project_name = project.name.replace(' ', '_').lower()
episode_name = episode.name.replace(' ', '_').lower() if episode else 'episode' episode_name = episode.name.replace(' ', '_').lower() if episode else 'episode'
ext = options.format.lower() ext = spreadsheet.format.lower()
export_name = export_name.format(date=date, project=project_name, export_name = export_name.format(date=date, project=project_name,
episode=episode_name, tracker=settings.tracker_name.lower(), ext=ext) episode=episode_name, tracker=settings.tracker_name.lower(), ext=ext)
@ -234,14 +236,14 @@ class VSETB_OT_export_spreadsheet(Operator):
#2023_04_11_kitsu_boris_ep01_shots #2023_04_11_kitsu_boris_ep01_shots
export_path.parent.mkdir(parents=True, exist_ok=True) export_path.parent.mkdir(parents=True, exist_ok=True)
if options.format == 'CSV': if spreadsheet.format == 'CSV':
print('Writing .csv file to', export_path) print('Writing .csv file to', export_path)
with open(str(export_path), 'w', newline='\n', encoding='utf-8') as f: with open(str(export_path), 'w', newline='\n', encoding='utf-8') as f:
writer = csv.writer(f, delimiter=options.delimiter) writer = csv.writer(f, delimiter=spreadsheet.delimiter)
for row in rows: for row in rows:
writer.writerow(row) writer.writerow(row)
elif options.format == 'XLSX': elif spreadsheet.format == 'XLSX':
try: try:
import openpyxl import openpyxl
except ModuleNotFoundError: except ModuleNotFoundError:
@ -262,7 +264,7 @@ class VSETB_OT_export_spreadsheet(Operator):
# Save the file # Save the file
workbook.save(str(export_path)) workbook.save(str(export_path))
if options.open_folder: if spreadsheet.open_folder:
open_file(export_path, select=True) open_file(export_path, select=True)
return {"FINISHED"} return {"FINISHED"}
@ -304,9 +306,9 @@ class VSETB_OT_import_spreadsheet(Operator):
classes = ( classes = (
VSETB_OT_add_spreadsheet_preset, VSETB_OT_add_export_spreadsheet_preset,
VSETB_MT_spreadsheet_presets, VSETB_MT_export_spreadsheet_presets,
VSETB_OT_spreadsheet_move, VSETB_OT_spreadsheet_cell_move,
VSETB_OT_export_spreadsheet, VSETB_OT_export_spreadsheet,
) )

View File

@ -3,9 +3,9 @@
from vse_toolbox.ui import (panels, preferences, properties) from vse_toolbox.ui import (panels, preferences, properties)
modules = ( modules = (
panels, properties,
preferences, preferences,
properties panels
) )
if 'bpy' in locals(): if 'bpy' in locals():

View File

@ -95,7 +95,7 @@ class Trackers(PropertyGroup):
class VSETB_Prefs(AddonPreferences): class VSETB_Prefs(AddonPreferences):
bl_idname = __package__ bl_idname = 'vse_toolbox'
trackers : PointerProperty(type=Trackers) trackers : PointerProperty(type=Trackers)
expand_settings: BoolProperty(default=False) expand_settings: BoolProperty(default=False)

View File

@ -172,7 +172,7 @@ def get_custom_name_items(self, context):
project = settings.active_project project = settings.active_project
return [(m.field_name, m.name, '') for m in project.metadata_types if m.entity_type=='ASSET'] return [(m.field_name, m.name, '') for m in project.metadata_types if m.entity_type=='ASSET']
class SpreadsheetImport(PropertyGroup): class SpreadsheetExport(PropertyGroup):
format : EnumProperty(items=[(i, i, '') for i in ('CSV', 'XLSX')]) format : EnumProperty(items=[(i, i, '') for i in ('CSV', 'XLSX')])
separator : StringProperty(default='\\n') separator : StringProperty(default='\\n')
delimiter : StringProperty(default=';') delimiter : StringProperty(default=';')
@ -187,6 +187,18 @@ class SpreadsheetImport(PropertyGroup):
cell_index : IntProperty(name='Spreadsheet Index', default=0) cell_index : IntProperty(name='Spreadsheet Index', default=0)
class SpreadsheetImport(PropertyGroup):
separator : StringProperty(default='\\n')
delimiter : StringProperty(default=';')
use_custom_name : BoolProperty(default=False)
custom_name : EnumProperty(items=get_custom_name_items,
description='Use a custom name for asset using a metadata value')
show_settings : BoolProperty(default=False)
cells: CollectionProperty(type=SpreadsheetCell)
cell_index : IntProperty(name='Spreadsheet Index', default=0)
class Project(PropertyGroup): class Project(PropertyGroup):
id : StringProperty(default='') id : StringProperty(default='')
@ -225,18 +237,20 @@ class Project(PropertyGroup):
task_types : CollectionProperty(type=TaskType) task_types : CollectionProperty(type=TaskType)
task_statuses : CollectionProperty(type=TaskStatus) task_statuses : CollectionProperty(type=TaskStatus)
spreadsheet_import: PointerProperty(type=Spreadsheet) spreadsheet_import: PointerProperty(type=SpreadsheetImport)
spreadsheet_export: PointerProperty(type=Spreadsheet) spreadsheet_export: PointerProperty(type=SpreadsheetExport)
type : StringProperty() type : StringProperty()
def set_spreadsheet(self): def set_spreadsheet(self):
spreadsheet = self.spreadsheet_export
cell_names = ['Sequence', 'Shot', 'Frames', 'Description'] cell_names = ['Sequence', 'Shot', 'Frames', 'Description']
if self.type == 'TVSHOW': if self.type == 'TVSHOW':
cell_names.insert(0, 'Episode') cell_names.insert(0, 'Episode')
for cell_name in cell_names: for cell_name in cell_names:
cell = self.spreadsheet.add() cell = spreadsheet.cells.add()
cell.name = cell_name cell.name = cell_name
cell.export_name = 'Name' if cell_name == 'Shot' else cell_name cell.export_name = 'Name' if cell_name == 'Shot' else cell_name
cell.field_name = cell_name.upper() cell.field_name = cell_name.upper()
@ -245,14 +259,14 @@ class Project(PropertyGroup):
for metadata_type in self.metadata_types: for metadata_type in self.metadata_types:
if not metadata_type['entity_type'] == "SHOT": if not metadata_type['entity_type'] == "SHOT":
continue continue
cell = self.spreadsheet.add() cell = spreadsheet.cells.add()
cell.name = metadata_type.name cell.name = metadata_type.name
cell.export_name = metadata_type.name cell.export_name = metadata_type.name
cell.field_name = metadata_type.field_name cell.field_name = metadata_type.field_name
cell.type = "METADATA" cell.type = "METADATA"
for asset_type in self.asset_types: for asset_type in self.asset_types:
cell = self.spreadsheet.add() cell = spreadsheet.cells.add()
cell.name = asset_type.name cell.name = asset_type.name
cell.export_name = asset_type.name cell.export_name = asset_type.name
cell.field_name = asset_type.name.upper() cell.field_name = asset_type.name.upper()
@ -434,7 +448,8 @@ classes = (
Metadata, Metadata,
MetadataType, MetadataType,
TaskType, TaskType,
Spreadsheet, SpreadsheetImport,
SpreadsheetExport,
Project, Project,
VSETB_UL_spreadsheet, VSETB_UL_spreadsheet,
VSETB_UL_casting, VSETB_UL_casting,