Outrsource Asset Tool - UE Importer
JO00
plain_text
2 months ago
9.0 kB
5
Indexable
import unreal
import os
import csv
#########
######## importing important locations from csv
csv_location=str(os.path.dirname(__file__))+"/mat_data.csv"
#####reading csv for ue project definitions
with open(csv_location, newline="", encoding="utf-8") as f:
reader = csv.reader(f)
rows = list(reader)
mat_names=rows[0]
mat_name_locations=rows[1]
ue_folder_text=rows[2]
ue_folder_mesh=rows[3]
ue_folder_mats=rows[4]
thumb_level_loc=rows[5]
###getting values of where mesh folder is and the meshes to import from that config
mesh_csv_location=str(os.path.dirname(__file__))+"/mesh_data.csv"
with open(mesh_csv_location, newline="", encoding="utf-8") as f:
reader = csv.reader(f)
rows = list(reader)
mesh_import_folder=rows[0]
meshes_to_import=rows[1]
###getting data on texture file format for end of file name
format_options=[".tga",".png",".jpeg"]
config_file=str(os.path.dirname(__file__))+"/config.txt"
with open(config_file, "r", newline="", encoding="utf-8") as f:
all_lines=f.readlines()
format_chosen=int(all_lines[4].strip())
final_image_format=format_options[format_chosen]
##
#printing debug it can reach all data
print(mat_names)
print(mat_name_locations)
print(ue_folder_text)
print(mesh_import_folder)
print(meshes_to_import)
print(final_image_format)
###the actual import bit
try:
#######
#########
##now per mesh get the location where it is for import
for mesh in meshes_to_import:
print("now importing mesh "+str(mesh))
mesh_loc_0=mesh_import_folder[0]+"/"+str(mesh)
if (os.path.isfile(mesh_loc_0+".fbx")):
print("mesh is fbx")
mesh_location=mesh_loc_0+".fbx"
elif (os.path.isfile(mesh_loc_0+".obj")):
print("mesh is obj")
mesh_location=mesh_loc_0+".obj"
print(mesh_location)
##now import into engine
print("now importing meshes")
options = unreal.FbxImportUI()
options.import_animations = False
options.import_materials = True
options.import_textures = False
options.mesh_type_to_import = unreal.FBXImportType.FBXIT_STATIC_MESH
task = unreal.AssetImportTask()
task.filename = mesh_location
task.destination_path = str(ue_folder_mesh[0])
task.automated = True
task.save = True
task.options = options
task.replace_existing =True
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task])
static_mesh= unreal.EditorAssetLibrary.load_asset(task.imported_object_paths[0])
#####
###import textures
print("now importing textures")
text_folder=mesh_import_folder[0]+"/"+str(mesh)+"/"
text_to_import=["AOCEP","N"]
for text_type in text_to_import:
mesh_name_shorter=mesh[3:]
text_path=text_folder+"T_"+str(mesh_name_shorter)+"_"+text_type+final_image_format
text_task = unreal.AssetImportTask()
text_task.filename = text_path.strip()
text_task.destination_path = str(ue_folder_text[0])#ue_folder_text
text_task.automated = True
text_task.save = True
text_task.replace_existing =True
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([text_task])
tex = unreal.EditorAssetLibrary.load_asset(text_task.imported_object_paths[0])
#switch off srgb on channel pack
if text_type=="AOCEP":
tex.set_editor_property("sRGB", False)
unreal.EditorAssetLibrary.save_loaded_asset(tex)
###now it imports in correctly both parts it just needs setup, mat instancing based on mat id, and close up...
#finding out what mats this one needs
static_path=f"{ue_folder_mesh[0]}/{mesh}.{mesh}"
print(static_path)
static_load=unreal.EditorAssetLibrary.load_asset(static_path)
num_slots = static_load.get_num_sections(0)
materials = static_load.get_editor_property("static_materials")
#now make instance for it with correct name in this folder
mat_slot_counter=0
for mat in materials:
mat_name=mat.material_interface.get_name()
if mat_name=="None":
print("not found mat skippih")
elif mat_name!="None":
mi_factory = unreal.MaterialInstanceConstantFactoryNew()
instance_check_name=0
instance_name=mat_name+"_"+mesh_name_shorter
instance_name_2=mat_name+"_"+mesh_name_shorter+"_"+mesh_name_shorter
#same mat exists delete to overwrite
full_path_check=f"{ue_folder_mats[0]}/{mat_name}"
mat_checking=unreal.EditorAssetLibrary.load_asset(full_path_check)
if unreal.EditorAssetLibrary.does_asset_exist(str(full_path_check)) and isinstance(mat_checking, unreal.MaterialInstance):
material_instance=unreal.EditorAssetLibrary.load_asset(full_path_check)
mi = material_instance
print("found re-using existin mat instance")
else:
print(full_path_check)
print("not found so going on")
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
material_instance = asset_tools.create_asset(asset_name=instance_name,package_path=str(ue_folder_mats[0]),asset_class=unreal.MaterialInstanceConstant,factory=mi_factory)
unreal.EditorAssetLibrary.save_asset(material_instance.get_path_name())
mi = material_instance
count=0
#found_mat_location="nothing"
print(mat_names)
print(mat_name_locations)
total_no_mats_loaded_csv=len(mat_names)
manual_count=0
#found_mat_location=mat_name_locations[0]
for i, name in enumerate(mat_names):
print(i)
#if name!=mat_names[i]:
#print("name not match")
#count+=1
if mat_name==mat_names[i]:
print("name finally match!")
found_mat_location=mat_name_locations[i]
break
#print(str(found_mat_location))
#print(found_mat_location)
print(found_mat_location)
parent_mat=unreal.EditorAssetLibrary.load_asset(found_mat_location)
mi.set_editor_property("parent", parent_mat)
mi.set_editor_property("parent", parent_mat)
#mi.set_editor_property("parent", parent_mat)
unreal.EditorAssetLibrary.save_asset(mi.get_path_name())
###now parent assigned just need to assign text
baked_map_loc=str(ue_folder_text[0])+"/T_"+str(mesh_name_shorter)+"_"+"AOCEP."+"T_"+str(mesh_name_shorter)+"_"+"AOCEP"
normal_map_loc=str(ue_folder_text[0])+"/T_"+str(mesh_name_shorter)+"_"+"N."+"T_"+str(mesh_name_shorter)+"_"+"N"
baked_map=unreal.EditorAssetLibrary.load_asset(baked_map_loc)
normal_map=unreal.EditorAssetLibrary.load_asset(normal_map_loc)
unreal.MaterialEditingLibrary.set_material_instance_texture_parameter_value(mi,"Baked Map",baked_map)
unreal.MaterialEditingLibrary.set_material_instance_texture_parameter_value(mi,"Baked Noise Map",baked_map)
unreal.MaterialEditingLibrary.set_material_instance_texture_parameter_value(mi,"Normal Map",normal_map)
###now set assign final back to mesh
#mat_slot_counter
static_load.set_material(mat_slot_counter, mi)
###this guy
mat_slot_counter+=1
#mat_slot_counter=mat_slot_counter+1
unreal.EditorAssetLibrary.save_asset(static_load.get_path_name())
###completed now delete the mats from import that spawn in meshes folder
for i, name in enumerate(mat_names):
print(i)
import_mat_duplicate_path=f"{ue_folder_mesh[0]}/{name}.{name}"
if unreal.EditorAssetLibrary.does_asset_exist(import_mat_duplicate_path):
unreal.EditorAssetLibrary.delete_asset(import_mat_duplicate_path)
unreal.EditorAssetLibrary.save_asset(static_load.get_path_name())
#unreal.EditorLevelLibrary.pilot_level_actor(None)
except Exception as e:
print("FAILED:", e)
raise
###now to take screenshotsEditor is loading...
Leave a Comment