How can I assign material to skeleton mesh with python?

Hello, I’m trying to put my material instance to 1st slot of skeletal mesh with python. After few hours of research, I did this code

import unreal
sm_asset = unreal.load_asset('/Game/Meshes/Bob')
mi_asset = '/Game/Materials/mi_head'
unreal.SkeletalMesh.materials = [unreal.SkeletalMaterial(material_interface=mi_asset, material_slot_name='mat_head', uv_channel_data=unreal.MeshUVChannelInfo())]

but I get this error

LogPython: Error: Traceback (most recent call last):
LogPython: Error:   File "<string>", line 4, in <module>
LogPython: Error: TypeError: SkeletalMaterial: Failed to convert type 'str' to property 'MaterialInterface' (ObjectProperty) for attribute 'material_interface' on 'SkeletalMaterial'
LogPython: Error:   TypeError: NativizeProperty: Cannot nativize 'str' as 'MaterialInterface' (ObjectProperty)
LogPython: Error:     TypeError: NativizeObject: Cannot nativize 'str' as 'Object' (allowed Class type: 'MaterialInterface')

All info about how can I add materials to skeletal mesh is outdated :frowning:

you need to unreal.load_asset(mi_asset)
material_interface accept MaterialInterface type not str type

This is how I do it, not sure if it’s the best way. You have to set a whole new array but this function should keep any unchanged material slots the same while updating your new ones.

def set_sk_materials(path_to_sk, materials_to_change):
    """
    path_to_sk = "/Game/Meshes/Bob"
    materials_to_change = {"mat_head": unreal.load_asset("/Game/Materials/mi_head")}
    """
    skeletal_mesh = unreal.load_asset(path_to_sk)
    skeletal_mesh_materials = skeletal_mesh.materials

    material_array = unreal.Array(unreal.SkeletalMaterial)
    for material in skeletal_mesh_materials:
        new_sk_material = unreal.SkeletalMaterial()

        slot_name = material.get_editor_property("material_slot_name")
        material_interface = material.get_editor_property("material_interface")

        if materials_to_change.get(str(slot_name)):
            material_interface = materials_to_change[str(slot_name)]

        new_sk_material.set_editor_property("material_slot_name", slot_name)
        new_sk_material.set_editor_property("material_interface", material_interface)

        material_array.append(new_sk_material)

    skeletal_mesh.set_editor_property("materials", material_array)
4 Likes

Yeah, it works with my 4.26! Thank you very much :slight_smile:

1 Like