# \[SOLVED\]Connect A Texture To A Material

**URL:** <https://forums.unrealengine.com/t/solved-connect-a-texture-to-a-material/151392>\
**Category:** Editor Scripting\
**Tags:** unreal-engine\
**Created:** [September 29, 2020, 5:23pm UTC](https://forums.unrealengine.com/t/solved-connect-a-texture-to-a-material/151392 "2020-09-29T17:23:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AtomicPerception](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AtomicPerception](https://forums.unrealengine.com/u/AtomicPerception)\
**Post date:** [September 29, 2020, 5:23pm UTC](https://forums.unrealengine.com/t/solved-connect-a-texture-to-a-material/151392/1 "2020-09-29T17:23:52Z")

</div>

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

```auto

# 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)

```

 ![Untitled-1.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/6/6/66037f6d44e74e6e6483bd8ef1a1cd3f937577a1.jpeg)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_45320574](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_45320574](https://forums.unrealengine.com/u/anonymous_user_45320574)\
**Post date:** [February 13, 2021, 7:34pm UTC](https://forums.unrealengine.com/t/solved-connect-a-texture-to-a-material/151392/3 "2021-02-13T19:34:06Z")

</div>

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.

```auto

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

```
