0
$\begingroup$

Does any one know how to attach texture created with, tex = bpy.data.textures.new('Flag', type = 'WOOD')
to be used in a ShaderNode ?

tex does not have tex.image and the issue is reference code is using
texture_slots.add() which is deprecated from 2.8. help is greatly appreciated.

this is the function I'm writing. obviously it doesn't work when a texture is passed .


    def textured(material_name, texture=None, image=None):
        mat = bpy.data.materials.new(material_name)
        mat.use_nodes = True
        nodes = mat.node_tree.nodes # Get the nodes
        nodes.clear()
        node_principled = nodes.new(type='ShaderNodeBsdfPrincipled') # Add the Principled Shader node
        node_principled.location = 0,0
        node_output = nodes.new(type='ShaderNodeOutputMaterial')    # Add the Output node
        node_output.location = 400,0
        node_tex = nodes.new('ShaderNodeTexImage') # Add the Image Texture node
        node_tex.location = -400,0
        links = mat.node_tree.links # Link all nodes
        link = links.new(node_tex.outputs["Color"], node_principled.inputs["Base Color"])
        link = links.new(node_principled.outputs["BSDF"], node_output.inputs["Surface"])
        if texture:
            node_tex.image = texture.image
        elif image:
            img = bpy.data.images.get(image) # Get the image and assign it to the Node.image
            if img: node_tex.image = img
            else: node_tex.image = bpy.data.images.load(image) # Assign the image
        return mat
```
$\endgroup$
3
  • $\begingroup$ I don't think you can. $\endgroup$
    – scurest
    Commented Aug 31, 2022 at 12:51
  • $\begingroup$ AFAIK the Texture system can't be linked to the shader system. You're better off using procedural shader texture nodes directly in the shader editor $\endgroup$
    – Gorgious
    Commented Aug 31, 2022 at 13:02
  • $\begingroup$ scurest , Gorgious understood. thanx! $\endgroup$ Commented Aug 31, 2022 at 15:52

1 Answer 1

1
$\begingroup$

You can retrieve the pixels from your texture, and place them on a new image then use this image instead of the texture.

import bpy

def texture_pixels(texture=None, x=32, y=32):
    pixels = []
    final_pixels = []
    for rangex in range(x):
        for rangey in range(y):
            pixels.append(texture.evaluate(value=(rangex, rangey, 0)))
    for p in pixels:
        for fl in p:
            final_pixels.append(fl)
    return final_pixels

def textured(material_name, texture=None, image=None):
    mat = bpy.data.materials.new(material_name)
    mat.use_nodes = True
    nodes = mat.node_tree.nodes # Get the nodes
    nodes.clear()
    node_principled = nodes.new(type='ShaderNodeBsdfPrincipled') # Add the Principled Shader node
    node_principled.location = 0,0
    node_output = nodes.new(type='ShaderNodeOutputMaterial')    # Add the Output node
    node_output.location = 400,0
    node_tex = nodes.new('ShaderNodeTexImage') # Add the Image Texture node
    node_tex.location = -400,0
    links = mat.node_tree.links # Link all nodes
    link = links.new(node_tex.outputs["Color"], node_principled.inputs["Base Color"])
    link = links.new(node_principled.outputs["BSDF"], node_output.inputs["Surface"])
    
    if texture:
        node_tex.image = bpy.data.images.new(material_name, 32, 32, alpha=True)
        node_tex.image.pixels = texture_pixels(texture, x=32, y=32)

    elif image:
        img = bpy.data.images.get(image) # Get the image and assign it to the Node.image
        if img: node_tex.image = img
        else: node_tex.image = bpy.data.images.load(image) # Assign the image
    return mat

tex = bpy.data.textures.new("name", type="WOOD")
tex.use_color_ramp = True
textured("new_mat", tex)

enter image description here

$\endgroup$
3
  • $\begingroup$ textures doesnt seem to have 'pixels' anymore. AttributeError: 'WoodTexture'/ (even)'ImageTexture' object has no attribute 'pixels' $\endgroup$ Commented Sep 2, 2022 at 21:09
  • $\begingroup$ i updated my answer, it has a function i just wrote, it should help you. the key thing here is that now we must call "evaluate" on each pixel of a texture, to get the rgb values of that coord, we convert the vectors to a float list that the image expects. here is a link for more info docs.blender.org/api/current/… $\endgroup$ Commented Sep 7, 2022 at 14:43
  • $\begingroup$ Perfect! (evaluate.. ofcoz). Thanx a bunch!!! $\endgroup$ Commented Sep 7, 2022 at 19:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .