From 1ff1b37adb75dfe4ae887b16d9eaf0bc68282339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cchristopheseux=E2=80=9D?= <“seuxchristophe@hotmail.fr”> Date: Fri, 5 May 2023 09:46:50 +0200 Subject: [PATCH] import csv from clipboard --- file_utils.py | 9 +++++++++ operators/addon.py | 7 ++++--- operators/spreadsheet.py | 25 +++++++++++++++---------- ui/properties.py | 36 ++++++++++++++++++++++++++++-------- 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/file_utils.py b/file_utils.py index a1b998c..752c239 100644 --- a/file_utils.py +++ b/file_utils.py @@ -37,6 +37,15 @@ def import_module_from_path(path): print(f'Cannot import file {path}') print(e) +def fuzzy_match(s1, s2, case_sensitive=False): + '''Tell how much two passed strings are similar 1.0 being exactly similar''' + from difflib import SequenceMatcher + if case_sensitive: + similarity = SequenceMatcher(None, s1, s2) + else: + similarity = SequenceMatcher(None, s1.lower(), s2.lower()) + return similarity.ratio() + def norm_str(string, separator='_', format=str.lower, padding=0): string = str(string) string = string.replace('_', ' ') diff --git a/operators/addon.py b/operators/addon.py index 292e2b6..1965b51 100644 --- a/operators/addon.py +++ b/operators/addon.py @@ -101,7 +101,7 @@ class VSETB_OT_load_settings(Operator): if spreadsheet_import_config: import_cells.clear() - cell_names = project.get_cell_names() + cell_types = project.get_cell_types() for k, v in spreadsheet_import_config.items(): if k == 'cells': for i, cell_data in enumerate(v): @@ -113,11 +113,12 @@ class VSETB_OT_load_settings(Operator): cell.name = cell_data['name'] norm_import_name = norm_str(cell_data.get('import_name', cell_data['name'])) - import_name = next((c for c in cell_names if norm_str(c) == norm_import_name), None) + import_name = next((n for n, t in cell_types.items() if norm_str(n) == norm_import_name), None) if import_name is None: - print(f'import_name {norm_import_name} not in {cell_names}') + print(f'import_name {norm_import_name} not in {list(cell_types.values())}') else: cell.import_name = import_name + cell.type = cell_types[import_name] if 'enabled' in cell_data: try: diff --git a/operators/spreadsheet.py b/operators/spreadsheet.py index c306f35..414e75b 100644 --- a/operators/spreadsheet.py +++ b/operators/spreadsheet.py @@ -14,7 +14,7 @@ from bl_operators.presets import AddPresetBase from vse_toolbox.sequencer_utils import (get_strips, get_strip_sequence_name) 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, fuzzy_match) class VSETB_MT_export_spreadsheet_presets(Menu): @@ -143,12 +143,23 @@ class VSETB_OT_spreadsheet_from_clipboard(Operator): def execute(self, context): scn = context.scene project = get_scene_settings().active_project + import_cells = project.spreadsheet_import.cells + import_cells.clear() spreadsheet = context.window_manager.clipboard - reader = list(csv.reader(spreadsheet.splitlines(), delimiter='\t')) - header = reader[0] - print(header) + cell_types = project.get_cell_types() + rows = list(csv.reader(spreadsheet.splitlines(), delimiter='\t')) + 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 return {"FINISHED"} @@ -194,12 +205,7 @@ class VSETB_OT_import_spreadsheet(Operator): row.operator("vse_toolbox.spreadsheet_from_clipboard", text='Clipboard', icon='PASTEDOWN') row.operator("vse_toolbox.spreadsheet_from_file", text='File', icon='FILE') - - row = layout.row(align=True) - row.prop(spreadsheet, 'use_custom_cells', text='Custom Cells') - col = layout.column(align=False) - col.enabled = spreadsheet.use_custom_cells row = col.row(align=True, heading='Custom Asset Name') row.use_property_split = True @@ -211,7 +217,6 @@ class VSETB_OT_import_spreadsheet(Operator): sub.label(icon='BLANK1') row = layout.row() - row.enabled = spreadsheet.use_custom_cells row.template_list("VSETB_UL_spreadsheet_import", "spreadsheet_import", spreadsheet, "cells", spreadsheet, "cell_index", rows=8) col_tool = row.column(align=True) diff --git a/ui/properties.py b/ui/properties.py index 03cf9ec..6bae9ab 100644 --- a/ui/properties.py +++ b/ui/properties.py @@ -171,11 +171,13 @@ def get_cell_items(self, context): settings = get_scene_settings() project = settings.active_project - return [(cell, cell, '') for cell in project.get_cell_names()] + return [(cell, cell, '') for cell in project.get_cell_types().keys()] class SpreadsheetImportCell(PropertyGroup): enabled : BoolProperty(default=True) import_name : EnumProperty(items=get_cell_items) + + #type : EnumProperty(items=[(t, t, "") for t in ('METADATA', 'SHOT', 'ASSET_TYPE')]) def get_custom_name_items(self, context): @@ -200,7 +202,7 @@ class SpreadsheetExport(PropertyGroup): class SpreadsheetImport(PropertyGroup): - use_custom_cells: BoolProperty(default=False) + #use_custom_cells: BoolProperty(default=False) separator : StringProperty(default='\\n') delimiter : StringProperty(default=';') use_custom_name : BoolProperty(default=False) @@ -258,23 +260,26 @@ class Project(PropertyGroup): type : StringProperty() - def get_cell_names(self): + def get_cell_types(self): settings = get_scene_settings() project = settings.active_project + cell_types = {} cell_names = ['Sequence', 'Shot', 'Frames', 'Description'] if project.type == 'TVSHOW': cell_names.insert(0, 'Episode') + + cell_types = {cell_name: 'SHOT' for cell_name in cell_names} for metadata_type in project.metadata_types: if metadata_type['entity_type'] == "SHOT": - cell_names += [metadata_type.name] + cell_types[metadata_type.name] = 'METADATA' for asset_type in project.asset_types: - cell_names += [asset_type.name] + cell_types[asset_type.name] = 'ASSET_TYPE' - return cell_names + return cell_types def set_spreadsheet(self): @@ -406,12 +411,27 @@ class VSETB_UL_spreadsheet_import(UIList): layout.use_property_split = True layout.use_property_decorate = False + item_type = project.get_cell_types()[item.import_name] + + enabled = True + if item_type == 'METADATA' and not project.spreadsheet_import.import_custom_data: + enabled = False + elif item_type == 'ASSET_TYPE' and not project.spreadsheet_import.import_casting: + enabled = False + elif item.import_name == 'Frames' and not project.spreadsheet_import.update_edit: + enabled = False + + layout.enabled = enabled + row = layout.row(align=True) row.alignment = 'LEFT' row.prop(item, 'enabled', text='') - layout.label(text=item.name) - layout.prop(item, 'import_name', text='') + + row = layout.row(align=True) + row.enabled = item.enabled + row.label(text=item.name) + row.prop(item, 'import_name', text='') class VSETB_UL_spreadsheet_export(UIList):