Download

[SOLVED] PYTHON : Cannot get StaticMesh.set_material() to work

Hello everyone,

I’m trying to write a script so I can import lotsa Static Meshes, create material instances out of one parent material and individual textures and assign the material instance to the mesh.

I’ve been stumbling a lot, had a very hard time with the doc but I got finally a working script ! Except for the last part, which is well documented and seems to work with everyone else. I’m starting to get really annoyed by all this, so I’d love a hand here.

So I got my Static Mesh, I know it’s ok since all prints say StaticMesh ; I got my MaterialInstanceConstant, they are both loaded through the assetLibrary.

But set_material never works (I tried with regular materials but it doesn’t change anything), and get_material or get_material_index never works even when I set the material manually.

Here is the problematic bit of my code :



 for meshName, textures in meshesDict.items(): # meshesDict : { 'meshName' : [texturesNames] }
    meshAsset = unreal.StaticMesh(assetLibrary.load_asset(globalDestinationPath + meshName))
    materialList = ]
    i = 0
    for textureName in textures:
        newMat = None
        textureMaterial = assetLibrary.load_asset(globalDestinationPath + textureName)
        assetName = textureName + '_inst'
        if check_asset_exists(assetName):
            newMat = assetLibrary.load_asset(globalDestinationPath + assetName)
        if not newMat:
            newMat = asset_tools.create_asset(assetName,
                                              globalDestinationPath,
                                              unreal.MaterialInstanceConstant,
                                              factory)
            paramVal = unreal.TextureParameterValue(unreal.MaterialParameterInfo('Color'), textureMaterial)
            newMat.set_editor_property('parent', parentMaterial)
            newMat.set_editor_property('textureParameterValues', [paramVal])
            assetLibrary.save_asset(globalDestinationPath + assetName)
        unreal.StaticMesh.set_material(meshAsset, i, newMat) # <== THIS IS NOT WORKING
        #meshAsset.set_material(i, unreal.MaterialInstanceConstant(newMat)) # <== NEITHER IS THIS
        assetLibrary.save_asset(globalDestinationPath + meshName)
        i += 1


Any ideas ? I’m really lost here, and would love some help.

Thanks in advance !

EIDT : code format

Ok, got it working !

I just replaced the not working line with


 
 meshAsset.set_material(i, newMat) 

Seems like casting was the problem. Also I had way too many objects loaded at the same time so I added an index to add only some of them to the dictionnary, it might have helped. I leave this here if anyone stumbles apon this issue !