Bake Material with Python script

I have a large list of object in UE4 that need to utilize the Bake Material function that is built in to UE4. Here are the screencaps of the Bake Material button and the options I need to implement:

https://i.imgur.com/suSwCSQ.jpg

https://i.imgur.com/Cs8xorT.jpg

However, I can not run it when I have multiple objects selected, so I need to utilize code.

For swapping materials I was able to find this script from the UE4 Channel that works well and thought I could use it as a base and modify for what I need:

Here it is in text form:

import python

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(“/Game/Building/Building/Materials/vray_Wall_Sign_Material”, “/Game/Wayfinding/MM_Sign_Blue_Inst”)

I found this great resource showing the Python functions for UE4 but I am not exactly sure how to write what I need.

https://docs.unrealengine.com/en-US/…alOptions.html

Using that and the script I found above I made an attempt though I know it is not complete, just not sure what I am missing:

I need to find a way to have this only affect the selected objects/assets so: for objects in selection ?

import unreal

def bake_material():
unreal.MaterialOptions.use_mesh_data(True)
unreal.MaterialOptions.texture_size(int(4096))

bake_material()

Any help is much appreciated. I will attempt to write it as well.

Kind Regards,