I have some Python experience, but I’m very new to Unreal. I’ve been searching for examples of Python code that can do any of those operations, but so far I haven’t been able to find anything. Could anyone point me to any examples showing how to do any of the above?
I’m looking for the same information, and am bumping up this thread in case someone has an idea. I essentially want to create a python script to look into all children of a material to see what overrides have been made. So for a start, getting to access a material and it’s parameters would be great.
def material_instances():
""" Get a list of all assets that are material instances. """
instances = list()
path = "/Game/"
for asset in unreal.EditorAssetLibrary.list_assets(path):
asset_data = unreal.EditorAssetLibrary.find_asset_data(asset)
if asset_data.asset_class == "MaterialInstanceConstant":
instances.append(asset)
return instances
def get_static_switch_values(material):
""" Iterate over Static Bool & Switch Material Expressions for the given
material to return a dictionary for parameter names and their default values.
:param material: the material object we want to get parameters from
:type unreal.Material:
:returns: {foo: False, bar: True}
"""
parameters = dict()
filter_on = (
unreal.MaterialExpressionStaticBoolParameter,
unreal.MaterialExpressionStaticSwitchParameter
)
# Iterate loaded objects that are child of material
# and matches filter types.
it = unreal.ObjectIterator()
for x in it:
if x.get_outer() == material:
if isinstance(x, filter_on):
name = str(x.get_editor_property("parameter_name"))
value = x.get_editor_property("default_value")
parameters.update({name: value})
return parameters
Was some time since I worked with it but want to remember you could get/set most values on the expressions. Run pythons help() on material instance constant and its material expression to see the available methods.
I can say also, Epic actually already have an amazing tool for what I assume you’re doing - Material Analyzer. I too reinvented the wheel before finding this tool. Was nice introduction to python in Unreal but thankfully I found it before I got around to work out how to properly display the data.
Yes. It was my inspiration of sorts. What I’m eventually trying to do is reduce shader compile times by reducing the number of unique static switches. I kind of had to get these commands to continue.
Hey I’m looking into actually getting a list of nodes (expressions) in a material and can’t find a way.
It seems that in C++, the material instance has an Expressions property that contains a list of nodes.
That is however not exposed to Python.
Has anyone been able to iterate through the nodes of a material (not a material instance)?
asset_registry = unreal.AssetRegistryHelpers.get_asset_registry()
all_assets = asset_registry.get_assets_by_path(asset_path, recursive=True)
if asset.asset_class == 'Material' and asset.asset_name == material_name:
loaded_material = unreal.EditorAssetLibrary.load_asset(asset.get_full_name())
parameters = unreal.MaterialEditingLibrary.get_static_switch_parameter_names(loaded_material)
for p in parameters:
static_switch_params = unreal.MaterialEditingLibrary.get_material_default_static_switch_parameter_value(loaded_material, p)
print static_switch_params
print_static_switch_params("/Game/Art/Shaders/", "MyMaterial_MAT")```](https://docs.unrealengine.com/en-US/PythonAPI/class/MaterialEditingLibrary.html)
Disclaimer: I've extracted this code from my existing script, so there might some issues with variable referencing etc., but this should give you an idea. More info on the MaterialEditingLibrary can be found here: [https://docs.unrealengine.com/en-US/PythonAPI/class/MaterialEditingLibrary.html.](https://docs.unrealengine.com/en-US/PythonAPI/class/MaterialEditingLibrary.html)
Hope this helps.
Thanks but that’s not what I’m looking for. The MaterialEditingLibrary module has methods to access Material Parameters but that’s not what i need. I’d like to loop through all the nodes in the graph. The way you access them in C++ is through the Expressions property of the material which doesn’t seem to be accessible through Python.
stat3d, the code I posted using ObjectIterator loops over all nodes in the material, just that my code filters for material expressions I was looking for.
The goal in mind is to integrate pipeline tools into the Unreal system with the rest of our tools.
I believe what stat3d was aiming for is Pythonic method find the entire tree of what is connected to a Material. Texture2D nodes, OneMinus nodes, Multiply nodes.
seems to be the closest in the Python module to being able to access what is connected to a [FONT=Courier New]MaterialProperty.
Searching the documentation, only [FONT=Courier New]unreal.MaterialProperty, and the above [FONT=Courier New]MaterialEditingLibrary method seem to make use of [FONT=Courier New]MaterialProperty.
For Python pipeline tools, we would need to be able to explore all the connections, disconnect and connect them.
Depending upon some lookdev needs, we will need to insert other material nodes such as [FONT=Courier New]OneMinus, or discover the [FONT=Courier New]Constant3Vector to set or verify its color.
The desire is to use the EPIC provided “[FONT=Courier New]unreal” module to be able to do this.
Also knowing the [FONT=Courier New]unreal module is really new, there is hopes there is some how-tos, examples, or confirmations that the current module can or cannot do some of these functions.
Any pointers or questions is greatly appreciated.
Thanks everyone
Thank you!
Golden in a roundabout way!
Would be really nice to have this inside the unreal.MaterialEditingLibrary module for easier access and discoverability.
Thanks for posting!
'''Create a new material'''
def material(name, color):
assetTools = unreal.AssetToolsHelpers.get_asset_tools()
# create new material asset
mat = assetTools.create_asset(name, "/Game/Materials", unreal.Material, unreal.MaterialFactoryNew())
# create a new node to the material graph
exp = unreal.MaterialEditingLibrary.create_material_expression(mat, unreal.MaterialExpressionConstant3Vector, -450, 250)
# set the material color value
exp.constant = color
# link the node to the material color property
print(unreal.MaterialEditingLibrary.connect_material_property(exp, "", unreal.MaterialProperty.MP_BASE_COLOR))
# save the changes
unreal.MaterialEditingLibrary.recompile_material(mat)
return mat```