# Replace Material Python Script

**URL:** <https://forums.unrealengine.com/t/replace-material-python-script/272903>\
**Category:** Editor Scripting\
**Tags:** unreal-engine, question, UE4-27\
**Created:** [January 16, 2022, 5:35pm UTC](https://forums.unrealengine.com/t/replace-material-python-script/272903 "2022-01-16T17:35:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Madogg13](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Madogg13](https://forums.unrealengine.com/u/Madogg13)\
**Post date:** [January 16, 2022, 5:35pm UTC](https://forums.unrealengine.com/t/replace-material-python-script/272903/1 "2022-01-16T17:35:28Z")

</div>

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)

```auto

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"
	quit() 

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"
	quit()

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](https://learn.unrealengine.com/course/2820439/module/5966035?moduletoken=UHxxnDLPW8TlNAzTjuHWhmeY31LueVKhAcfSj27uR7qpeYi-Huu9QCx-y7uSyNcj&LPId=0) 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

---

<div class="post-metadata">

**Author:** ![UE\_FlavienP](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ue_flavienp/32/1194546_2.png) [@UE\_FlavienP](https://forums.unrealengine.com/u/UE_FlavienP)\
**Post date:** [January 17, 2022, 11:02am UTC](https://forums.unrealengine.com/t/replace-material-python-script/272903/2 "2022-01-17T11:02:46Z")

</div>

Hello,  
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.

```auto
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”

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/9/b/9b3d3f053ad09e55c6d13a40d7b4f3e78df594bc.png)

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

```auto
actors = unreal.EditorFilterLibrary.by_class(unreal.EditorLevelLibrary.get_all_level_actors(),unreal.StaticMeshActor)
material = unreal.EditorAssetLibrary.load_asset("/Game/White_Material")
unreal.DataprepOperationsLibrary.substitute_material(actors,"Name_Of_Red_Material_In_Actor",unreal.EditorScriptingStringMatchType.CONTAINS,material)

```

---

<div class="post-metadata">

**Author:** ![kernelkiller](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kernelkiller](https://forums.unrealengine.com/u/kernelkiller)\
**Post date:** [February 22, 2023, 3:59am UTC](https://forums.unrealengine.com/t/replace-material-python-script/272903/3 "2023-02-22T03:59:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kernelkiller](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kernelkiller](https://forums.unrealengine.com/u/kernelkiller)\
**Post date:** [February 22, 2023, 4:03am UTC](https://forums.unrealengine.com/t/replace-material-python-script/272903/4 "2023-02-22T04:03:40Z")

</div>

Ok, I found I need to enable `DataprepEditor` plugin, as per [documentation](https://docs.unrealengine.com/5.1/en-US/PythonAPI/class/DataprepOperation.html).

---

<div class="post-metadata">

**Author:** ![PMcKenzie2004](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PMcKenzie2004](https://forums.unrealengine.com/u/PMcKenzie2004)\
**Post date:** [October 25, 2024, 9:31pm UTC](https://forums.unrealengine.com/t/replace-material-python-script/272903/5 "2024-10-25T21:31:11Z")

</div>

@UE_FlavienP Thanks, this script works. Question, please: The script won’t replace Materials for me in UE 5.4 in Nested Static Meshes (such as a railing imported from Revit), that has one or more Static Mesh Components under it in the Details Pane.

I changed the script to use “get\_all\_level\_actors\_components()” and "unreal.StaticMeshComponent - which does replace Materials on sub-Static Mesh Components. BUT, the change is not preserved even by Save All in the Editor.

Any thoughts, please, on how to replace Materials on Static Mesh Components that are part of an Actor, and make it stick, please?
