How to change shader type?

Hi, I have several objects that use a material composed of an image node directly plugged into the surface input. (cycles)

How can I add a principled shader?
imageTexture->diffuse of principled shader -> output

I’m assuming you’re using version 2.8x. I’ve never used any of those versions so correct me if I’m wrong, but I’m pretty sure you aren’t supposed to plug textures directly into the surface input.

Try plugging the principled shader in-between your texture and the surface node.

Thanks, that’s the idea, via code.

Objects are generated from a baketool, for some reason it doesn’t put a principled shader between texture - surface node.

Found a link in order to generate material with a surface + texture, but how can I get the name of current texture in material and remove currecnt material?

import bpy

from bpy_extras.node_shader_utils import PrincipledBSDFWrapper
from bpy_extras.image_utils import load_image

mat = bpy.data.materials.new(name="Material_from_script")
mat.use_nodes = True
principled = PrincipledBSDFWrapper(mat, is_readonly=False)
principled.base_color = (0.8, 0.8, 0.5)
principled.specular_texture.image = load_image("/path/to/image.png") #how to get actual texture?

#after material is created, assign that material to object (replace old material)

Hi,
some time ago I was writing a small function to create an image texture material.
This is the function:

def _create_new_image_material(name, fname, alpha=1.):
    """Create a new material.
    Args:
        name: name of material
        fname: path to image to load
    Returns:
        The new material.
    """
    
    nodescale = 300

    # load image texture
    bpy.data.images.load(fname, check_existing=False)

    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
    nodes = mat.node_tree.nodes
    nodes.clear()
    
    texture = nodes.new(type="ShaderNodeTexCoord")
    texture.location = 0, 0

    img = nodes.new(type="ShaderNodeTexImage")
    img.location = nodescale, 0
    img.image = bpy.data.images[fname.split('/')[-1]]

    bsdf = nodes.new(type="ShaderNodeBsdfPrincipled")
    bsdf.location = 2*nodescale, 0
    #bsdf.inputs[0].default_value = color
    bsdf.inputs['Alpha'].default_value = alpha
    bsdf.inputs['Specular'].default_value = 0.

    node_output = nodes.new(type="ShaderNodeOutputMaterial")
    node_output.location = 3*nodescale, 0

    links = mat.node_tree.links
    link = links.new(texture.outputs[2], img.inputs[0])
    link = links.new(img.outputs[0], bsdf.inputs[0])
    link = links.new(bsdf.outputs[0], node_output.inputs[0])
    return mat

Hope it helps.

1 Like

Thanks for the help. Was able to find out node that was disconnecting loaded image from said script (addon) and it’s working as needed now.