Get materials from staticmesh with python

Hey guys, I’m looking for your help !
I read a lot of questions, I had already to know how to get the materials with c++, but I need python。 I have already get the StaticMesh and StaticMeshComponent , but the python api ,only StaticMesh have ‘’get_material” 。the StaticMeshComponent have no 。。 how can i do ?
thank you !

the component has a “static_mesh” property. So try:
unreal.StaticMeshComponent.static_mesh.get_material(material_index) → MaterialInterface

thank you…but i don’t know the static meshs has how many mats…for example ,one mesh has one mat,so,get_material(0) …but other one has two,i must get_materials(0) and get_materials(1) …how can i do ? Cheers

Would you like to replace a material with an other? That would be the script: Replace Material Python Script?? - Editor Scripting - Unreal Engine Forums

but for your question try the get_num_sections function. it’s a little bit missleading but should give you the materials:

        static_mesh = unreal.StaticMeshComponent.static_mesh
        if static_mesh is None:
            continue
        #iterate on all material slots
        for material_index in range(0,static_mesh.get_num_sections(0)):
            #get material name for example
            material_name = static_mesh.get_material(material_index).get_name()
            # do your stuff

amazing ! this function can slove my question ! thanks a lot !cheers !

your welcome - happy to help :slight_smile: please mark the answer as correct so the ticket is resolved