Replace Material Python Script

Hi, Im new to python scripting. That said, i was attending an unreal learning course “Revit to Unreal Engine Fundamentals”, in which they use a python script to replace one material, imported by datasmith, with another created in the project (instance material created from revit - Master material from starter content)

import unreal

red_material = unreal.EditorAssetLibrary.load_asset("MaterialInstanceConstant'/Game/Udatasmith/GAR-MO-IA41_55b-EST-IND-01-P-R10_05_11_21-Vista3D-Unreal_1_Vivienda_terminada/Materials/Contrachapado__entablados.Contrachapado__entablados'")
if red_material is None:
	print "The red material can't be loaded"

white_material = unreal.EditorAssetLibrary.load_asset("MaterialInstanceConstant'/Game/Materials/MI_Instances/MI_RobleViejo.MI_RobleViejo'")
if white_material is None:
	print "The white material can't be loaded"

actor_list = unreal.EditorLevelLibrary.get_all_level_actors()
actor_list = unreal.EditorFilterLibrary.by_class(actor_list, unreal.StaticMeshActor.static_class())
unreal.EditorLevelLibrary.replace_mesh_components_materials_on_actors(actor_list, red_material, white_material)

Original code is in here at 3:03

I’m trying to replace the instance material created in revit that is being used in a bunch of static meshes with my instance material creted in UE
Any help would be great, thank u

you can use that code. You just need to replace the path to the red and white material with the path of those materials in your content browser.

unreal.EditorAssetLibrary.load_asset("/Game/Path to material under the Content folder")

e.g. the path of this material is “/game/import/01_Left_Turbo_part_1_SLDPRT/Materials/M_DatasmithCAD”

If you use dataprep you have 2 functions in it to replace materials based on their names: “Substitute Material” and "Substitute Materials by Table).
Those two are also available through Python

actors = unreal.EditorFilterLibrary.by_class(unreal.EditorLevelLibrary.get_all_level_actors(),unreal.StaticMeshActor)
material = unreal.EditorAssetLibrary.load_asset("/Game/White_Material")

Hi I tried to use unreal.DataprepOperationsLibrary.set_collision_complexity(object, unreal.CollisionTraceFlag.CTF_USE_COMPLEX_AS_SIMPLE) for one asset I had (irrelevant to OP’s original question per se) but it says “AttributeError: module ‘unreal’ has no attribute ‘DataprepOperationsLibrary’”. Any cluse?
I am using Unreal 5.1

Ok, I found I need to enable DataprepEditor plugin, as per documentation.