Potential task for automation is creation of material instances for a lot of meshes. For example, I use Houdini to generate a lot of assets procedurally – models with baked texture maps. Importing all these meshes, textures and creating material instances (based on common master material) and assigning respective textures for each model is repetitive and boring manual task.
Here is a sample python script that is meant to run on selected static meshes – it will automatically create Material Instance asset for each mesh based on its name and assign all textures also based on name. At the end it assigns newly created material instance to the mesh.
It expects textures to be named the same as static meshes, but with appropriate postfixes – “_basecolor”, “_normal” etc. It also expects standard folder structure:
import unreal
def set_mi_texture(mi_asset, param_name, tex_path):
if not unreal.EditorAssetLibrary.does_asset_exist(tex_path):
unreal.log_warning(“Can’t find texture: ” + tex_path)
return False
tex_asset = unreal.EditorAssetLibrary.find_asset_data( tex_path ).get_asset()
return unreal.MaterialEditingLibrary.set_material_instance_texture_parameter_value(mi_asset, param_name, tex_asset)
unreal.log(“—————————————————“)
AssetTools = unreal.AssetToolsHelpers.get_asset_tools()
MaterialEditingLibrary = unreal.MaterialEditingLibrary
EditorAssetLibrary = unreal.EditorAssetLibrary
base_mtl = unreal.EditorAssetLibrary.find_asset_data(“/Game/Environment/Cave/Materials/M_CaveBase”)
#Iterate over selected meshes
sel_assets = unreal.EditorUtilityLibrary.get_selected_assets()
for sm_asset in sel_assets:
[…]