Auto Material Assignment

https://forums.unrealengine.com/core/create-content/text/

https://forums.unrealengine.com/core/create-content/text/

I’m trying to make a Blutility that allows me to select actors in the content browser and auto fill their material slots with material instances depending on the name of the material slot. I’m having trouble with this because the selected assets are Static Meshes but the Get Material Slot Names node works only on Mesh Components. Is there a workaround to find out the material slot names of each selected asset?

Thank you.

https://forums.unrealengine.com/core/create-content/text/

https://forums.unrealengine.com/core/create-content/text/

Found a work around, You can download the tool here free: Assign Material Tool UE4

Here I have a class that gets selected static meshes from content browsers and looks for the material and instanced ones based on the material slot name and if it finds it anywhere in content browser it will assigns it:



import unreal

def set_mat():
    assets = unreal.EditorUtilityLibrary.get_selected_asset_data()
    try:
        asset_registry = unreal.AssetRegistryHelpers.get_asset_registry()
        project_mats =  asset_registry.get_assets(unreal.ARFilter(recursive_classes= True, class_names="Material","MaterialInstance"]))
        for asset in assets:
            object_path = unreal.StructBase.get_editor_property(asset, 'object_path')
            package_name = str(unreal.StructBase.get_editor_property(asset, 'package_name'))
            package_path = unreal.StructBase.get_editor_property(asset, 'package_path')
            asset_name = str(unreal.StructBase.get_editor_property(asset, 'asset_name'))
            asset_class = unreal.StructBase.get_editor_property(asset, 'asset_class')
            if (asset_class == 'StaticMesh'):
                tmp_asset = unreal.load_asset(object_path)
                sm_component = unreal.StaticMeshComponent()
                sm_component.set_static_mesh(tmp_asset)
                mat_slot_names = unreal.StaticMeshComponent.get_material_slot_names(sm_component)
                for mat in mat_slot_names:
                    for mats in project_mats:
                        mats_name = unreal.StructBase.get_editor_property(mats, 'asset_name')
                        if mats_name is mat:
                            tmp_asset.set_material(tmp_asset.get_material_index(mat), mats.get_asset())
                            unreal.EditorAssetLibrary.save_asset(object_path, True)
    except Exception as error:
      print(error)


set_mat()