Simple material swap python script not working

Hi,

Is there anything wrong with my code? It excutes but no materials replace.


import unreal

def replace_material(original, replacement):
    original_asset =
    unreal.EditorAssetLibrary.load_asset(original)
    replacement_asset =
    unreal.EditorAssetLibrary.load_asset(replacement)
        unreal.EditorAssetLibrary.consolidate_assets(replacement_asset, [original_asset])

#replace_material(original_name, replacement_name)
replace_material("Content/1619_Section_26_CENTRAL6633/Materials/TL1_Wall_Tile", "Content/Materials2/Ceramic_Enamel_Tiles_MAT")

I’ve tried so many variations of the path that I’ve given up. Is there a way to just wildcard this so TL1_Wall_Tile gets replaced with Ceramic_Enamel_Tiles_MAT?

i think u paths are incorrect…

Content/1619_Section_26_CENTRAL6633/Materials/TL1_Wall_Tile should be: /Game/Content/1619_Section_26_CENTRAL6633/Materials/TL1_Wall_Tile

Thanks, I tried that it but it also doesn’t work.

I’m lost as it’s literally cut and pasting the path, so no errors with spelling. Even using the filepath that UE4 is saying…

ue4.jpg

(Redacted my name though)

Scripting in Maya so much easier, at least it mentions where is wrong.

Sorry i think i was incorrect with the path, u have “Content/” … it should be “/Game/” . U can see it in the image above.

replace_material("/Game/1619_Section_26_CENTRAL6633/Materials/TL1_Wall_Tile", “/Game/Materials2/Ceramic_Enamel_Tiles_MAT”)

Thanks for the help.

Unfortunately that doesn’t work either.

There’s no error messages, no feedbaack. The script just executes and does nothing like the results here

Hi there guys,

ive attempted the above, but im receiving the following messages. please can someone help!

Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import unreal
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ModuleNotFoundError: No module named ‘unreal’
>>>
>>> def replace_material(original, replacement):
… original_asset = unreal.EditorAssetLibrary.load_asset(original)
… replacement_asset = unreal.EditorAssetLibrary.load_asset(replacement)
… unreal.EditorAssetLibrary.consolidate_assets(replacement_asset, [original_asset])

>>> replace_material(“/Game/Content/Materials/Metals_-Steel-_RSA_S355J”, “/Game/Content/Materials/M_Metal_Steel”)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
File “<stdin>”, line 2, in replace_material
NameError: name ‘unreal’ is not defined
>>>

Have you set Unreal’s built-in Python version to 3.X?
Your code looks like it is running in an environment other than Unreal Editor, so the header file cannot be found.

Hi. Thanks, that seemed have worked. although im getting this message. any idea how to rectify this?

I really don’t understand, over a year now and cannot get this working!

I’m even using Revit and the AdvancedGlassPack materials which is exactly what this script is using
https://gist.github.com/mdaguerre/8f…48eba3d0d78075

It’s using the exact smae same names even, yet the python script does not work.

I don’t understand why it’s so easy for everyone :frowning:

Just tried it out and it seems to work for me. Maybe copying and pasting the path name is causing you problems?

This is what i did.



# Select two material assets in the content browser based on a bad to good order
mats = unreal.GlobalEditorUtilityBase.get_default_object().get_selected_assets()

replace_material(mats[0].get_path_name(), mats[1].get_path_name())



I don’t know about python and I struggled with the same error

but I solve it. It means that can’t change Material and Material Instance. Same Class(eg. Material to Material) is possible.

Try it :slight_smile:

I had met this question like you ,
but I had solved this.
The reason why is that your object’s class is not the same,
such as first one is the material, another one is the Material Instance,
so it can not replace each other,
you must be make them class to be same,
hope this will be help for you