fix import spreadsheet

pull/5/head
“christopheseux” 2023-05-22 18:48:07 +02:00
parent f699ce54db
commit 56562e7052
1 changed files with 45 additions and 20 deletions

View File

@ -107,6 +107,9 @@ class VSETB_OT_spreadsheet_from_file(Operator):
scn = context.scene scn = context.scene
project = get_scene_settings().active_project project = get_scene_settings().active_project
import_cells = project.spreadsheet_import.cells
import_cells.clear()
if not self.filepath: if not self.filepath:
self.report({'ERROR'}, 'No filepath provided') self.report({'ERROR'}, 'No filepath provided')
return {'CANCELLED'} return {'CANCELLED'}
@ -118,22 +121,43 @@ class VSETB_OT_spreadsheet_from_file(Operator):
filepath = Path(self.filepath) filepath = Path(self.filepath)
if filepath.suffix.lower() == '.csv': if filepath.suffix.lower() == '.csv':
with open(filepath, newline='') as csvfile: with open(filepath, newline='\n') as csvfile:
reader = csv.reader(csvfile) rows = list(csv.reader(csvfile))
next(reader)
data = dict(reader)
elif filepath.suffix.lower() == '.xlsx': 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) 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: else:
self.report({'ERROR'}, f'File extension {filepath.suffix} should be in [.csv, .xlsx]') self.report({'ERROR'}, f'File extension {filepath.suffix} should be in [.csv, .xlsx]')
return {'CANCELLED'} 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"} 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.import_name = max(cell_types.keys(), key=lambda x: fuzzy_match(cell_name, x))
cell.enabled = True cell.enabled = True
project.spreadsheet_import.use_custom_cellss = True project.spreadsheet_import.use_custom_cells = True
SPREADSHEET.extend(rows) 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.import_name = max(cell_types.keys(), key=lambda x: fuzzy_match(cell_name, x))
cell.enabled = True cell.enabled = True
project.spreadsheet_import.use_custom_cellss = True project.spreadsheet_import.use_custom_cells = True
SPREADSHEET.extend(rows) SPREADSHEET.extend(rows)
@ -245,7 +269,7 @@ class VSETB_OT_import_spreadsheet(Operator):
row.label(text='Source') row.label(text='Source')
#row.alignment='RIGHT' #row.alignment='RIGHT'
row.operator("vse_toolbox.spreadsheet_from_clipboard", text='Clipboard', icon='PASTEDOWN') 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) col = layout.column(align=False)
@ -307,7 +331,7 @@ class VSETB_OT_import_spreadsheet(Operator):
header = SPREADSHEET[0] header = SPREADSHEET[0]
print(SPREADSHEET[:2]) #print(SPREADSHEET[:2])
cell_types = project.get_cell_types() cell_types = project.get_cell_types()
cell_names = {k: spreadsheet.cells[k].import_name for k in header if k} 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 frame_start = scn.frame_start
for row in SPREADSHEET[1:]: 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 = {cell_names[k]: v for k, v in zip(header, row) if k}
#cell_data = {k: v for k, v in zip(header, row)} #cell_data = {k: v for k, v in zip(header, row)}
shot_name = cell_data['Shot'] 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 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) strip = next((s for s in shot_strips if s.name == shot_name), None)
print(cell_data) #print(cell_data)
print("shot_name", shot_name, strip) #print("shot_name", shot_name, strip)
if spreadsheet.update_edit and (nb_frames_cell and nb_frames_cell.enabled): if spreadsheet.update_edit and (nb_frames_cell and nb_frames_cell.enabled):
nb_frames = int(cell_data['Nb Frames']) nb_frames = int(cell_data['Nb Frames'])
@ -380,7 +404,8 @@ class VSETB_OT_import_spreadsheet(Operator):
strip_settings.description = cell_value strip_settings.description = cell_value
elif cell_type == 'METADATA' and spreadsheet.import_custom_data: 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: if cell_type == 'ASSET_TYPE' and spreadsheet.import_casting:
@ -395,7 +420,6 @@ class VSETB_OT_import_spreadsheet(Operator):
if not asset_name: if not asset_name:
continue continue
print(asset_name)
if spreadsheet.use_custom_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) asset = next((a for a in project.assets if norm_str(a.get('metadata', {}).get(spreadsheet.custom_name)) == norm_str(asset_name)), None)
else: else:
@ -409,7 +433,8 @@ class VSETB_OT_import_spreadsheet(Operator):
strip_settings.casting.update() strip_settings.casting.update()
else: 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"} return {"FINISHED"}
@ -440,10 +465,10 @@ class VSETB_OT_export_spreadsheet(Operator):
layout = self.layout layout = self.layout
row = layout.row(align=True) 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 = 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 = col.row(align=True, heading='Custom Asset Name')
row.use_property_split = True row.use_property_split = True