Apply texture to a model via blender script I am trying to apply a texture to a model programmatically via script. Via GUI, it's easy enough to do (Blender 2.8):


Import model (obj file)
In model properties go to texture properties
Change "Base color option" to "Image texture"
"Open", then load dds file for the image texture


For writing the script, this is as far as I went:

import bpy

obj_path = '/my_path_to_files/mytest.obj'
tex_path = '/my_path_to_files/mytest.dds'

# import model and texture
bpy.ops.import_scene.obj(filepath=obj_path)#, axis_forward='-Y', axis_up='Z')
bpy.ops.image.open(filepath=tex_path)
# get reference to model and texture
myobj = bpy.data.objects['mytest']
mytex = bpy.data.images['mytest.dds']

# try to set the texture (doesn't work)
myobj_material_nodes = myobj.active_material.node_tree.nodes
node_texture = myobj_material_nodes.new(type='ShaderNodeTexImage')
node_texture.image = mytex


I don't know which properties I have to set to get this working. Or if I am in the right path at all. The properties set when doing via GUI are:

bl_description: 
bl_height_default: 100.0
bl_height_max: 30.0
bl_height_min: 30.0
bl_icon: NONE
bl_idname: ShaderNodeTexImage
bl_label: Image Texture
bl_rna: &lt;bpy_struct, Struct("ShaderNodeTexImage")&gt;
bl_static_type: TEX_IMAGE
bl_width_default: 240.0
bl_width_max: 700.0
bl_width_min: 140.0
color: &lt;Color (r=0.6080, g=0.6080, b=0.6080)&gt;
color_mapping: &lt;bpy_struct, ColorMapping at 0x7f752ae3e298&gt;
dimensions: &lt;Vector (0.0000, 0.0000)&gt;
draw_buttons: &lt;bpy_func ShaderNodeTexImage.draw_buttons()&gt;
draw_buttons_ext: &lt;bpy_func ShaderNodeTexImage.draw_buttons_ext()&gt;
extension: REPEAT
height: 100.0
hide: False
image: &lt;bpy_struct, Image("mytest.dds")&gt;
image_user: &lt;bpy_struct, ImageUser at 0x7f752ae3e5c8&gt;
input_template: &lt;bpy_func ShaderNodeTexImage.input_template()&gt;
inputs: &lt;bpy_collection[1], NodeInputs&gt;
internal_links: &lt;bpy_collection[0], ShaderNodeTexImage.internal_links&gt;
interpolation: Linear
is_registered_node_type: &lt;bpy_func ShaderNodeTexImage.is_registered_node_type()&gt;
label: 
location: &lt;Vector (-280.0000, 280.0000)&gt;
mute: False
name: Image Texture
output_template: &lt;bpy_func ShaderNodeTexImage.output_template()&gt;
outputs: &lt;bpy_collection[2], NodeOutputs&gt;
parent: None
poll: &lt;bpy_func ShaderNodeTexImage.poll()&gt;
poll_instance: &lt;bpy_func ShaderNodeTexImage.poll_instance()&gt;
projection: FLAT
projection_blend: 0.0
rna_type: &lt;bpy_struct, Struct("ShaderNodeTexImage")&gt;
select: True
show_options: True
show_preview: False
show_texture: True
socket_value_update: &lt;bpy_func ShaderNodeTexImage.socket_value_update()&gt;
texture_mapping: &lt;bpy_struct, TexMapping at 0x7f752ae3e208&gt;
type: TEX_IMAGE
update: &lt;bpy_func ShaderNodeTexImage.update()&gt;
use_custom_color: False
width: 240.0
width_hidden: 42.0


My question is, what would be a good, clean way of doing this?
What it was missing is a link between the shader and the texture. We can do it as follows:

myobj_material_links = myobj.active_material.node_tree.links
my_obj_shader = myobj_material_nodes.get('Principled BSDF')
myobj_material_links.new(my_obj_shader.inputs["Base Color"], node_texture.outputs["Color"])