From 56562e70529c8ed15cbdddc17f6f39b6c7f7155e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cchristopheseux=E2=80=9D?= <“seuxchristophe@hotmail.fr”> Date: Mon, 22 May 2023 18:48:07 +0200 Subject: [PATCH] fix import spreadsheet --- operators/spreadsheet.py | 65 +++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/operators/spreadsheet.py b/operators/spreadsheet.py index 8cac2ca..243db79 100644 --- a/operators/spreadsheet.py +++ b/operators/spreadsheet.py @@ -107,6 +107,9 @@ class VSETB_OT_spreadsheet_from_file(Operator): scn = context.scene project = get_scene_settings().active_project + import_cells = project.spreadsheet_import.cells + import_cells.clear() + if not self.filepath: self.report({'ERROR'}, 'No filepath provided') return {'CANCELLED'} @@ -118,22 +121,43 @@ class VSETB_OT_spreadsheet_from_file(Operator): filepath = Path(self.filepath) if filepath.suffix.lower() == '.csv': - with open(filepath, newline='') as csvfile: - reader = csv.reader(csvfile) - next(reader) - data = dict(reader) - + with open(filepath, newline='\n') as csvfile: + rows = list(csv.reader(csvfile)) elif filepath.suffix.lower() == '.xlsx': - raise Exception('Not Supported') + try: + import openpyxl + except ModuleNotFoundError: + self.report({'INFO'}, 'Installing openpyxl') + openpyxl = install_module('openpyxl') + + from openpyxl import Workbook + workbook = openpyxl.load_workbook(filepath, read_only=True) - sheet = next((ws for ws in workbook.worksheets if ws.title.lower() == 'export'), None) - + sheet = workbook.active + rows = [(c.value or '') for r in sheet.rows for c in r] + else: self.report({'ERROR'}, f'File extension {filepath.suffix} should be in [.csv, .xlsx]') return {'CANCELLED'} + rows = [r for r in rows if any(r)] + + cell_types = project.get_cell_types() + for cell_name in rows[0]: + if not cell_name: + continue + + cell = import_cells.add() + cell.name = cell_name + cell.import_name = max(cell_types.keys(), key=lambda x: fuzzy_match(cell_name, x)) + cell.enabled = True + + project.spreadsheet_import.use_custom_cells = True + + SPREADSHEET.extend(rows) + return {"FINISHED"} @@ -164,7 +188,7 @@ class VSETB_OT_spreadsheet_from_clipboard(Operator): cell.import_name = max(cell_types.keys(), key=lambda x: fuzzy_match(cell_name, x)) cell.enabled = True - project.spreadsheet_import.use_custom_cellss = True + project.spreadsheet_import.use_custom_cells = True SPREADSHEET.extend(rows) @@ -198,7 +222,7 @@ class VSETB_OT_spreadsheet_to_clipboard(Operator): cell.import_name = max(cell_types.keys(), key=lambda x: fuzzy_match(cell_name, x)) cell.enabled = True - project.spreadsheet_import.use_custom_cellss = True + project.spreadsheet_import.use_custom_cells = True SPREADSHEET.extend(rows) @@ -245,7 +269,7 @@ class VSETB_OT_import_spreadsheet(Operator): row.label(text='Source') #row.alignment='RIGHT' row.operator("vse_toolbox.spreadsheet_from_clipboard", text='Clipboard', icon='PASTEDOWN') - row.operator("vse_toolbox.spreadsheet_from_file", text='File', icon='FILE') + row.operator("vse_toolbox.spreadsheet_from_file", text='File', icon='FILE').filepath = self.filepath col = layout.column(align=False) @@ -307,7 +331,7 @@ class VSETB_OT_import_spreadsheet(Operator): header = SPREADSHEET[0] - print(SPREADSHEET[:2]) + #print(SPREADSHEET[:2]) cell_types = project.get_cell_types() cell_names = {k: spreadsheet.cells[k].import_name for k in header if k} @@ -318,7 +342,7 @@ class VSETB_OT_import_spreadsheet(Operator): frame_start = scn.frame_start for row in SPREADSHEET[1:]: - print(row) + #print(row) cell_data = {cell_names[k]: v for k, v in zip(header, row) if k} #cell_data = {k: v for k, v in zip(header, row)} shot_name = cell_data['Shot'] @@ -326,8 +350,8 @@ class VSETB_OT_import_spreadsheet(Operator): #strip = next((s for s in sequencer if s.vsetb_strip_settings.source_name == shot_name), None) strip = next((s for s in shot_strips if s.name == shot_name), None) - print(cell_data) - print("shot_name", shot_name, strip) + #print(cell_data) + #print("shot_name", shot_name, strip) if spreadsheet.update_edit and (nb_frames_cell and nb_frames_cell.enabled): nb_frames = int(cell_data['Nb Frames']) @@ -380,7 +404,8 @@ class VSETB_OT_import_spreadsheet(Operator): strip_settings.description = cell_value elif cell_type == 'METADATA' and spreadsheet.import_custom_data: - setattr(strip_settings.metadata, cell.import_name, cell_value) + metadata = project.metadata_types[cell.import_name] + setattr(strip_settings.metadata, metadata.field_name, cell_value) if cell_type == 'ASSET_TYPE' and spreadsheet.import_casting: @@ -395,7 +420,6 @@ class VSETB_OT_import_spreadsheet(Operator): if not asset_name: continue - print(asset_name) if spreadsheet.use_custom_name: asset = next((a for a in project.assets if norm_str(a.get('metadata', {}).get(spreadsheet.custom_name)) == norm_str(asset_name)), None) else: @@ -409,7 +433,8 @@ class VSETB_OT_import_spreadsheet(Operator): strip_settings.casting.update() else: - self.report({'WARNING'}, f'Asset {asset_name} not found in Project') + print(f'Asset {asset_name} not found in Project for strip {strip.name}') + self.report({'WARNING'}, f'Asset {asset_name} not found in Project for strip {strip.name}') return {"FINISHED"} @@ -440,10 +465,10 @@ class VSETB_OT_export_spreadsheet(Operator): layout = self.layout row = layout.row(align=True) - row.prop(spreadsheet, 'use_custom_cellss', text='Custom Cells') + row.prop(spreadsheet, 'use_custom_cells', text='Custom Cells') col = layout.column(align=False) - col.enabled = spreadsheet.use_custom_cellss + col.enabled = spreadsheet.use_custom_cells row = col.row(align=True, heading='Custom Asset Name') row.use_property_split = True