0
$\begingroup$

I am doing a process to some switch links in materials. I need to know if something is ever linked or not before to process. I can't find out how to :/ The script works well if something is linked only.

import bpy
for m in bpy.context.object.material_slots:
    mat = m.material
    nodes = mat.node_tree.nodes

    # get node input (socket)
    alphasocket = mat.node_tree.nodes.get("Principled BSDF").inputs[18]
    # get node link that connects into the socket
    lnk = (link.from_socket for link in mat.node_tree.links if link.to_socket == alphasocket)

    #add emission node
    emi= nodes.new('ShaderNodeEmission')
    emi.name= "emibak"
    outepute= mat.node_tree.nodes.get("Material Output").inputs[0]

    #make new link destination
    #here 's where I cant find out how to check
    if lnk != None:
        print(mat.name + " alphanod found")
       # mat.node_tree.links.new(lnk, emi.inputs[0])
    else :
        print(mat.name + " alphanod missing")

    mat.node_tree.links.new(emi.outputs[0], outepute)

here is another way I am searching...

import bpy

for m in bpy.context.object.material_slots:

    mat = m.material
    nodes = mat.node_tree.nodes     
    print("**** Material " + mat.name+ " ****")

    for mat_node in mat.node_tree.nodes:
        if mat_node.type == 'BSDF_PRINCIPLED':
            print("From " + mat_node.name )            
            alphasocket = mat_node.inputs[18]
            #print(alphasocket)
            node_in = mat_node.inputs
            #print(node_in)      

            for n in alphasocket.links:               
                print("Alpha connected to " + n.from_node.name)

This show me what is connected to the alpha channel but if nothing is connected I want to connect a new node.

$\endgroup$
  • $\begingroup$ Hi. Firstly, I suggest removing anything from your script that isn't relevant to the question you are asking (I'm not sure if the code related to adding an emission node is relevant here). Secondly, when you say "I need to know if something is ever linked", what do you mean by "something"? $\endgroup$ – Ray Mairlot May 23 at 16:02
  • $\begingroup$ I post the entire (small) code so you can have a better view. What I want is to test if any node is connected to the alpha channel of the Principled Shader. If not, I would create a White RGB node and connect it. $\endgroup$ – Loranozor Graphitz May 23 at 21:27
0
$\begingroup$

To answer comment

What I want is to test if any node is connected to the alpha channel of the Principled Shader. If not, I would create a White RGB node and connect it.

import bpy
context = bpy.context
ob = context.object
# all node based materials on object
mats = set(s.material for s in ob.material_slots
       if s.material and s.material.use_nodes)
for m in mats:
    nodes = m.node_tree.nodes
    bsdfnodes = [n for n in nodes 
            if isinstance(n, bpy.types.ShaderNodeBsdfPrincipled)]
    for n in bsdfnodes:
        if not n.inputs[18].links:
            rgb = nodes.new("ShaderNodeRGB")
            rgb.outputs[0].default_value = (1, 1, 1, 1)
            m.node_tree.links.new(n.inputs[18], rgb.outputs[0])
$\endgroup$

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.