improved auto import, spiderman
This commit is contained in:
1 parent
c21e83bb88
commit
c22e06f8b0
3 files changed
+35
-18
No files matched your search
@@ -6,9 +6,9 @@ dialog, and wires the textures into a Material Instance per mesh.
|
|||||||
"""
|
"""
|
||||||
import os, unreal
|
import os, unreal
|
||||||
|
|
||||||
SRC_DIR = r"C:\Users\koleg\Documents\Unreal Projects\ExampleProject\DemoAssets\RobotPolice"
|
DEMO_ROOT = r"C:\Users\koleg\Documents\Unreal Projects\ExampleProject\DemoAssets"
|
||||||
DEST_PATH = "/Game/ImportedAssets"
|
DEST_ROOT = "/Game/ImportedAssets"
|
||||||
MASTER_MATERIAL = f"{DEST_PATH}/M_Master"
|
MASTER_MATERIAL = f"{DEST_ROOT}/M_Master"
|
||||||
|
|
||||||
# suffix -> (texture parameter name, material property, sampled channel)
|
# suffix -> (texture parameter name, material property, sampled channel)
|
||||||
PARAMS = {
|
PARAMS = {
|
||||||
@@ -27,19 +27,19 @@ def get_or_create_master_material():
|
|||||||
if unreal.EditorAssetLibrary.does_asset_exist(MASTER_MATERIAL):
|
if unreal.EditorAssetLibrary.does_asset_exist(MASTER_MATERIAL):
|
||||||
return unreal.EditorAssetLibrary.load_asset(MASTER_MATERIAL)
|
return unreal.EditorAssetLibrary.load_asset(MASTER_MATERIAL)
|
||||||
|
|
||||||
material = asset_tools.create_asset("M_Master", DEST_PATH, unreal.Material, unreal.MaterialFactoryNew())
|
material = asset_tools.create_asset("M_Master", DEST_ROOT, unreal.Material, unreal.MaterialFactoryNew())
|
||||||
for i, (suffix, (param, prop, channel)) in enumerate(PARAMS.items()):
|
for i, (suffix, (param, prop, channel)) in enumerate(PARAMS.items()):
|
||||||
tex = mel.new_material_expression(material, unreal.MaterialExpressionTextureSampleParameter2D, -400, i * 200)
|
tex = mel.create_material_expression(material, unreal.MaterialExpressionTextureSampleParameter2D, -400, i * 200)
|
||||||
tex.set_editor_property("parameter_name", param)
|
tex.set_editor_property("parameter_name", param)
|
||||||
mel.connect_material_property(tex, channel, prop)
|
mel.connect_material_property(tex, channel, prop)
|
||||||
mel.recompile_material(material)
|
mel.recompile_material(material)
|
||||||
return material
|
return material
|
||||||
|
|
||||||
|
|
||||||
def import_task(filename, name, options=None):
|
def import_task(filename, name, dest_path, options=None):
|
||||||
task = unreal.AssetImportTask()
|
task = unreal.AssetImportTask()
|
||||||
task.filename = filename
|
task.filename = filename
|
||||||
task.destination_path = DEST_PATH
|
task.destination_path = dest_path
|
||||||
task.destination_name = name
|
task.destination_name = name
|
||||||
task.automated = True
|
task.automated = True
|
||||||
task.save = True
|
task.save = True
|
||||||
@@ -47,10 +47,10 @@ def import_task(filename, name, options=None):
|
|||||||
if options:
|
if options:
|
||||||
task.options = options
|
task.options = options
|
||||||
asset_tools.import_asset_tasks([task])
|
asset_tools.import_asset_tasks([task])
|
||||||
return unreal.load_asset(f"{DEST_PATH}/{name}")
|
return unreal.load_asset(f"{dest_path}/{name}")
|
||||||
|
|
||||||
|
|
||||||
def import_fbx(filename, name):
|
def import_fbx(filename, name, dest_path):
|
||||||
options = unreal.FbxImportUI()
|
options = unreal.FbxImportUI()
|
||||||
options.mesh_type_to_import = unreal.FBXImportType.FBXIT_SKELETAL_MESH
|
options.mesh_type_to_import = unreal.FBXImportType.FBXIT_SKELETAL_MESH
|
||||||
options.import_mesh = True
|
options.import_mesh = True
|
||||||
@@ -59,14 +59,29 @@ def import_fbx(filename, name):
|
|||||||
options.create_physics_asset = False
|
options.create_physics_asset = False
|
||||||
options.import_materials = False
|
options.import_materials = False
|
||||||
options.import_textures = False
|
options.import_textures = False
|
||||||
return import_task(filename, name, options)
|
return import_task(filename, name, dest_path, options)
|
||||||
|
|
||||||
|
|
||||||
|
def assign_material(skeletal_mesh, material):
|
||||||
|
slots = skeletal_mesh.get_editor_property("materials")
|
||||||
|
for slot in slots:
|
||||||
|
slot.set_editor_property("material_interface", material)
|
||||||
|
skeletal_mesh.set_editor_property("materials", slots)
|
||||||
|
unreal.EditorAssetLibrary.save_asset(skeletal_mesh.get_path_name())
|
||||||
|
|
||||||
|
|
||||||
master = get_or_create_master_material()
|
master = get_or_create_master_material()
|
||||||
|
|
||||||
# group source files by base name: {"RobotPolice": {"fbx": "...", "_BC": "...", ...}}
|
asset_count = 0
|
||||||
groups = {}
|
for asset_folder in os.listdir(DEMO_ROOT):
|
||||||
for f in os.listdir(SRC_DIR):
|
src_dir = os.path.join(DEMO_ROOT, asset_folder)
|
||||||
|
if not os.path.isdir(src_dir):
|
||||||
|
continue
|
||||||
|
dest_path = f"{DEST_ROOT}/{asset_folder}"
|
||||||
|
|
||||||
|
# group source files by base name: {"RobotPolice": {"fbx": "...", "_BC": "...", ...}}
|
||||||
|
groups = {}
|
||||||
|
for f in os.listdir(src_dir):
|
||||||
stem, ext = os.path.splitext(f)
|
stem, ext = os.path.splitext(f)
|
||||||
if ext.lower() == ".fbx":
|
if ext.lower() == ".fbx":
|
||||||
groups.setdefault(stem, {})["fbx"] = f
|
groups.setdefault(stem, {})["fbx"] = f
|
||||||
@@ -76,22 +91,24 @@ for f in os.listdir(SRC_DIR):
|
|||||||
groups.setdefault(stem[: -len(suffix)], {})[suffix] = f
|
groups.setdefault(stem[: -len(suffix)], {})[suffix] = f
|
||||||
break
|
break
|
||||||
|
|
||||||
for base_name, files in groups.items():
|
for base_name, files in groups.items():
|
||||||
if "fbx" not in files:
|
if "fbx" not in files:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
import_fbx(os.path.join(SRC_DIR, files["fbx"]), base_name)
|
mesh = import_fbx(os.path.join(src_dir, files["fbx"]), base_name, dest_path)
|
||||||
|
|
||||||
mi = asset_tools.create_asset(f"MI_{base_name}", DEST_PATH, unreal.MaterialInstanceConstant,
|
mi = asset_tools.create_asset(f"MI_{base_name}", dest_path, unreal.MaterialInstanceConstant,
|
||||||
unreal.MaterialInstanceConstantFactoryNew())
|
unreal.MaterialInstanceConstantFactoryNew())
|
||||||
mel.set_material_instance_parent(mi, master)
|
mel.set_material_instance_parent(mi, master)
|
||||||
|
|
||||||
for suffix, (param, _, _) in PARAMS.items():
|
for suffix, (param, _, _) in PARAMS.items():
|
||||||
if suffix not in files:
|
if suffix not in files:
|
||||||
continue
|
continue
|
||||||
texture = import_task(os.path.join(SRC_DIR, files[suffix]), f"T_{base_name}{suffix}")
|
texture = import_task(os.path.join(src_dir, files[suffix]), f"T_{base_name}{suffix}", dest_path)
|
||||||
mel.set_material_instance_texture_parameter_value(mi, param, texture)
|
mel.set_material_instance_texture_parameter_value(mi, param, texture)
|
||||||
|
|
||||||
unreal.EditorAssetLibrary.save_asset(mi.get_path_name())
|
unreal.EditorAssetLibrary.save_asset(mi.get_path_name())
|
||||||
|
assign_material(mesh, mi)
|
||||||
|
asset_count += 1
|
||||||
|
|
||||||
print(f"Imported {len(groups)} asset(s) into {DEST_PATH}")
|
print(f"Imported {asset_count} asset(s) into {DEST_ROOT}")
|
||||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 200 KiB |
Reference in new issue
Block a user