Reimport DataTable(csv) with python

Hi, I am trying to use python to reimport a DataTable, but I had a hard time to get it right. I don’t know how to assign my custom struct(from C++) in python properly.

task = unreal.AssetImportTask()
task.filename = fpath
task.destination_path = dpath
task.replace_existing = True
task.automated = True

csv_factory = unreal.CSVImportFactory()
outer = unreal.ScriptStruct()
csv_factory.automated_import_settings.import_row_struct = unreal.ScriptStruct(outer, ‘NameOfMyStruct’)

task.options = csv_factory

asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
asset_tools.import_asset_tasks([task])

The error is:

LogCSVImportFactory: Error: A Data table row type must be specified in the import settings json file for automated import

The ultimate goal is to use BP to execute the python command and reimport the DataTable.
Any tips will be helpful, thank you.

I haven’t found the answer for python, but there is a BP node “Fill Data Table from CSV File” can read csv and update the DataTable. The result is the same. But I am still looking forward the python answer.

image

I’m doing something similar, but with JSON instead of CSV, but it works pretty much the same.
Just make sure to first load the assets from the content browser, then you’ll be able to write it.

Here is the code I’m using to update a DataTable from a JSON file on disk using a Struct already available in the content browser

# Load DataTable and Struct assets
data_table = unreal.EditorAssetLibrary.load_asset(ue_asset_path)
data_structure = unreal.EditorAssetLibrary.load_asset(datastructure_source_path + datastructures)

# Fill in data from json file, and save DataTable asset 
unreal.DataTableFunctionLibrary.fill_data_table_from_json_file(data_table, json_file_path, data_structure)
unreal.EditorAssetLibrary.save_loaded_asset(data_table, True)

I’m not sure if having the Struct made in C++ makes any difference in term of how you can access it through python.

Hope this helps

1 Like

Finally I got it right.

fpath = r"E:\dev\XXXEngine\UnrealEngine\GameXXX\Saved\CSV\MyDataTable.csv" #Use csv to find the coresponse data table.
dpath = r'/Game/_XXX/DataTable/BattleMap'  #only provide the folder
 
def my_reimport():
    task = unreal.AssetImportTask()
    task.filename = fpath
    task.destination_path = dpath
    task.replace_existing = True
    task.automated = True
    task.save = False
 
    csv_factory = unreal.CSVImportFactory()
    csv_factory.automated_import_settings.import_row_struct = unreal.load_object(None, '/Game/_XXX/DataTable/BattleMap/MyTestStruct.MyTestStruct') #use unreal.load_object to load struct type as an object.
 
 
    task.factory = csv_factory
 
 
    asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
    asset_tools.import_asset_tasks([task])
 
my_reimport()
1 Like