python 2.8 - image texture stretch versus repeat In Blender/Python 2.8, I have added an image texture to an object.  The object is very wide which causes the image texture to stretch whereas I would like it to repeat.

The documentation (under "extension") seems to suggest that the default setting is for the texture to repeat rather than to stretch.  I would assume that because I am using the default that the texture would repeat rather than stretch.

Any suggestions as to how to prevent stretch in my code would be appreciated.  Thanks in advance!


def obj_texture(objname, filename, mat_name):
    materials = bpy.data.materials
    material = materials.get(mat_name)
    if not material:
        material = materials.new(mat_name)
    clear_material(material)
    material.use_nodes = True
    nodes = material.node_tree.nodes
    links = material.node_tree.links
    output = nodes.new(type = 'ShaderNodeOutputMaterial' )
    diffuse = nodes.new(type = 'ShaderNodeBsdfDiffuse' )
    path=TextureFolder+filename
    imgTex = bpy.data.images.load(path)
    node_texture = nodes.new(type='ShaderNodeTexImage')
    node_texture.image = imgTex
    node_texture.location = 0,200

    tex_coord = nodes.new(type = 'ShaderNodeTexCoord') #NEW CODE @RJG

    links = material.node_tree.links

    link=links.new(tex_coord.outputs["Generated"], node_texture.inputs["Vector"]) #NEW CODE @RJG

    link = links.new(node_texture.outputs[0], nodes.get('Diffuse BSDF').inputs[0])
    link = links.new( diffuse.outputs['BSDF'], output.inputs['Surface'] )
    obj=bpy.data.objects[objname]
    obj.active_material=material
You need to create a Texture Coordinate node and connect it to the Image Texture node. This is necessary to provide a mapping for the 2D image onto the 3D model.

The Texture Coordinate node is created using:

tex_coord = nodes.new(type = 'ShaderNodeTexCoord')


Then link the appropriate coordinate type to your image texture. Usually this would be either UV for the coordinates created through UV unwrapping of your mesh or Generated for the automatically generated coordinates. Depending on what you choose, you'll have to use one of the following options to connect the sockets.

links.new(tex_coord.outputs["UV"], node_texture.inputs["Vector"])


or

links.new(tex_coord.outputs["Generated"], node_texture.inputs["Vector"])


The Extension of the Image Texture node doesn't have to be changed, since it is set to Repeat by default, just like the documentation says.