Download

[SOLVED]Connect A Texture To A Material

I came up with some code to make a material with a few extra connected TextureSample node.



# Works with Unreal 4.25.3
# (c) 2020 Atom.

asset_path = "/Game"
mat_name = "honeycomb"

#Import known images as a list of Texture2D objects.
lst_image_files = 
r'F:\Keep\Maps\3D Textures\Honeycomb_002_SD\Honeycomb_002_basecolor.jpg',
r'F:\Keep\Maps\3D Textures\Honeycomb_002_SD\Honeycomb_002_normal.jpg',
r'F:\Keep\Maps\3D Textures\Honeycomb_002_SD\Honeycomb_002_ambientOcclusion.jpg'
]

data = unreal.AutomatedAssetImportData()
data.set_editor_property('destination_path', "%s/Mats" % asset_path)
data.set_editor_property('filenames', lst_image_files)
lst_texture2D = unreal.AssetToolsHelpers.get_asset_tools().import_assets_automated(data)

# Node input conenction types.
# MP_BASE_COLOR, MP_NORMAL, MP_ROUGHNESS, MP_SPECULAR, MP_EMISSIVE_COLOR, MP_OPACITY, MP_AMBIENT_OCCLUSION

# Create a material.
assetTools = unreal.AssetToolsHelpers.get_asset_tools()
mf = unreal.MaterialFactoryNew()
mat_closure = assetTools.create_asset("M_%s" % mat_name, "%s/Mats" % asset_path, unreal.Material, mf)

# Make a texture diffuse node.
ts_node_diffuse = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionTextureSample,-384,-200)
unreal.MaterialEditingLibrary.connect_material_property(ts_node_diffuse, "RGBA", unreal.MaterialProperty.MP_BASE_COLOR)
if lst_texture2D[0]!=None:
ts_node_diffuse.texture = lst_texture2D[0]

# Make a texture normal node.
ts_node_normal = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionTextureSample,-384,200)
unreal.MaterialEditingLibrary.connect_material_property(ts_node_normal, "RGB", unreal.MaterialProperty.MP_NORMAL)
if lst_texture2D[1]!=None:
# Change this sampler color sample type to work with normal types.
ts_node_normal.sampler_type = unreal.MaterialSamplerType.SAMPLERTYPE_NORMAL
ts_node_normal.texture = lst_texture2D[1]

# Make a texture AO node.
ts_node_AO = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionTextureSample,-384,500)
unreal.MaterialEditingLibrary.connect_material_property(ts_node_AO, "RGB", unreal.MaterialProperty.MP_AMBIENT_OCCLUSION)
if lst_texture2D[2]!=None:
ts_node_AO.texture = lst_texture2D[2]

# Make a constant float node.
ts_node_roughness = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionConstant,-125,150)
ts_node_roughness.set_editor_property('R', 0.5)
unreal.MaterialEditingLibrary.connect_material_property(ts_node_roughness, "", unreal.MaterialProperty.MP_ROUGHNESS)

# Make a constant float node.
ts_node_specular = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionConstant,-125,50)
ts_node_specular.set_editor_property('R', 0.13)
unreal.MaterialEditingLibrary.connect_material_property(ts_node_specular, "", unreal.MaterialProperty.MP_SPECULAR)

#unreal.MaterialEditingLibrary.recompile_material(mat_closure)


Thanks!

Finding practical examples or tutorials are (almost) non-existent.
So to feed the internet with more examples, here is a script based on yours. It does same stuff plus adding and connecting other expression, and setting properties. The interesting part here I guess is different terminoloy (connect_material_property X connect_material_expressions) for what’s visually the same thing while doing it on the blueprint, despite OK API logic.



import unreal

asset_path = "/Game"
mat_name = "honeycomb_changed"

#Import known images as a list of Texture2D objects.
lst_image_files =  r'D:\MA	ecnico\SensoriamentoRemoto\UnrealEngine	t	1.jpg' ]

data = unreal.AutomatedAssetImportData()
data.set_editor_property('destination_path', "%s/Mats" % asset_path)
data.set_editor_property('filenames', lst_image_files)
lst_texture2D = unreal.AssetToolsHelpers.get_asset_tools().import_assets_automated(data)

# Node input conenction types.--> https://docs.unrealengine.com/en-US/PythonAPI/class/MaterialProperty.html
# MP_BASE_COLOR, MP_NORMAL, MP_ROUGHNESS, MP_SPECULAR, MP_EMISSIVE_COLOR, MP_OPACITY, MP_AMBIENT_OCCLUSION

# Create a material.
assetTools = unreal.AssetToolsHelpers.get_asset_tools()
mf = unreal.MaterialFactoryNew()
mat_closure = assetTools.create_asset("M_%s" % mat_name, "%s/Mats" % asset_path, unreal.Material, mf)
# And set a property
mat_closure.set_editor_property('shading_model', unreal.MaterialShadingModel.MSM_UNLIT)

# Create a texturesample expression and connect.
texturesample = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionTextureSample,-384,-200)
unreal.MaterialEditingLibrary.connect_material_property(texturesample, "rgba", unreal.MaterialProperty.MP_EMISSIVE_COLOR)
if lst_texture2D[0]!=None:
      texturesample.texture = lst_texture2D[0]

# Create a expression, set properties and Connect
coordexpression = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionTextureCoordinate,-550,-200)
coordexpression.set_editor_property('utiling', 0.5)
coordexpression.set_editor_property('vtiling', 0.5)
unreal.MaterialEditingLibrary.connect_material_expressions(coordexpression, "", texturesample, "uvs")


2 Likes