Get material slot names for static mesh in python.

I am attempting to get the material slot names of a static mesh asset in the editor using python. However I cant seem to find a way to retrieve this information.

I am able to do this easily for a skeletal mesh using material = skeletal_mesh_asset.get_editor_property('materials') this return’s a SkeletalMaterial which contains it’s slot name as an editor property eg. material.get_editor_property('material_slot_name')

However, I am unable to read the Materials property of a static mesh as it is marked as protected:
Running material = static_mesh_asset.get_editor_property('materials') throws the following exception:

LogPython: Error: Exception: StaticMesh: Property 'Materials' for attribute 'materials' on 'StaticMesh' is protected and cannot be read

There does not appear to be any other way to get a StaticMaterial object in the python api. I am able to get the number of slots a static mesh has using the EditorStaticMeshLibrary, but this returns a MaterialInstance object which does not contain any information about the slot it is assigned to.

Is there a reason that the Materials property is protected on static meshes while being accessible for skeletal meshes? I am thinking that this may be an oversight and should be made accessible in the future.

Hey,

i did this with:

sMMaterials = static_mesh.get_material_slot_names();
for sMMaterial in sMMaterials:

Ok, so the function get_material_slot_names() is a method of the StaticMeshComponent class. So you are able to retrieve the materials from base StaticMesh class by first creating a StaticMeshComponent, setting your asset as the static mesh property, then calling get_material_slot_names()

I wrote a simple function to do this:

def get_material_slot_names(static_mesh):
    sm_component = unreal.StaticMeshComponent()
    sm_component.set_static_mesh(static_mesh)
    return unreal.StaticMeshComponent.get_material_slot_names(sm_component)

It seems a bit hacky to have to create a static mesh component just to access the material slots of the mesh itself, but this seems to work well enough for my purposes.

1 Like