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"
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)
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
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.
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")
unreal.DataprepOperationsLibrary.substitute_material(actors,"Name_Of_Red_Material_In_Actor",unreal.EditorScriptingStringMatchType.CONTAINS,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
@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?