Add Material Slots via Script

Is there any way to create additional material slots on a StaticMesh asset via python or BP?
Basically I want my lower lods to use different materials but to do that I first need to add material slots.
I’ve searched the docs but failed to find a way myself. Seems you must click the + button in the Static Mesh editor.

I would create a master material with all the functionality for your highest detailed LOD. Than I would add parameterized switches in the material to switch off what I don’t need on lower LODs.

Afterwards I would create material instances from the master material with some switches on and some off (how it is needed).

These material instances can than be assigned to the specific LODs.

In python I would do something like:

path_to_sm = "/Game/Assets/SM_Test"
sm_obj = unreal.load_asset(path_to_sm)

sm_mats = sm_obj.get_editor_property("static_materials")

new_material = unreal.StaticMaterial()
new_material.material_slot_name = "New Material Slot"

sm_mats.append(new_material)

sm_obj.set_editor_property("static_materials", sm_mats)

Hope this helps!

Oh, excellent. This is the kind of thing I was looking for, thanks!
I wonder though…I tried get_editor_property(“static_materials”) and it failed.
This may be because I’m on 4.25 or because I didn’t do load_asset() before.
Thanks again! Will try shortly.

You’re right, I just tested it and this solution doesn’t work in 4.25. I’m not sure exactly the best way you’d want to do it, but this might help:

def add_material_slot(sm_path, new_slot_name):
    sm_obj = unreal.load_asset(sm_path)
    
    new_material = unreal.AssetToolsHelpers.get_asset_tools().create_asset(new_slot_name,
                                                                           sm_path.rpartition("/")[0],
                                                                           unreal.MaterialInstanceConstant,
                                                                           unreal.MaterialInstanceConstantFactoryNew())

    sm_obj.add_material(new_material)

There might be a better way that doesn’t involve creating a new material, hope this helps though.