0
$\begingroup$

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
$\endgroup$
  • 1
    $\begingroup$ I'd say you've essentially answered your own question. If you test your script you'll set that the Extension is set to repeat. If you want anything other than repeat you'll have to add node_texture.extension = "CLIP" or node_texture.extension = "EXTEND" but by default it will already by REPEAT. $\endgroup$ – Robert Gützkow Jul 30 at 18:41
  • $\begingroup$ @rjg But when I run it, the texture doesn't repeat, it stretches. $\endgroup$ – vndep Jul 30 at 18:49
  • 1
    $\begingroup$ That will likely be because of your texture coordinates. You need to input UV or generated coordinates through a Texture Coordinate node to your Image Texture node $\endgroup$ – Robert Gützkow Jul 30 at 18:51
  • 1
    $\begingroup$ Let's say you've created a new Texture Coordinate node in your script tex_coord = nodes.new(type = 'ShaderNodeTexCoord') , then you can links.new(tex_coord.outputs["Generated"], node_texture.inputs["Vector"]) or links.new(tex_coord.outputs["UV"], node_texture.inputs["Vector"]) $\endgroup$ – Robert Gützkow Jul 30 at 19:15
  • 1
    $\begingroup$ did you unwrap your mesh so that it has proper UV coordinates? $\endgroup$ – Robert Gützkow Jul 30 at 19:40
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ thanks! Do you have any suggestions as to a good place to learn about unwrapping a mesh and whatever other precursors are necessary? $\endgroup$ – vndep Jul 30 at 21:48
  • $\begingroup$ @vndep for the very basic idea see: youtube.com/watch?v=Y7M-B6xnaEM $\endgroup$ – Robert Gützkow Jul 30 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.