0
$\begingroup$

The material is looking like this:

enter image description here

I'm trying with this code:

nodes = mat.node_tree.nodes
for n in mat.node_tree.nodes:
    print(m.name)
    #I use startswith so I can check if there are node groups duplicated
    if nodes.name.startswith("Custom Node"):
          #Here I want to dive inside the node group and check if there is an image texture node inside

And this is what I get in the console: Material Output Principled BSDF Group

I'm wondering how can I get the actual name of the group.

Then I would like to dive inside the node groups (all the node groups whose name start with "Custom Node" actually) and check if a certain node is inside them, let's say for example an image texture node. The code should be something like this

mat = bpy.data.materials.get(bpy.context.selected_objects[0].active_material.name)
mat.use_nodes = True
nodes = mat.node_tree.nodes
for n in mat.node_tree.nodes:
    print(n.name)
    #I use startsswith so I can check if there are node groups duplicated
    if n.name.startswith("Custom Node"):
        #Here I want to dive inside the node group and check if there is an image texture node inside. 
        #This DOESN'T WORK but helps to have an idea of what i want
        for n2 in n.node_tree.nodes:
            if n2.name == "ShaderNodeTexImage":
                print("Found Image texture")
$\endgroup$
1
  • $\begingroup$ there are a few issues. print(m.name) should be n.name mat = bpy.context.selected_objects[0].active_material is way shorter mat.use_nodes = True might reset to a new nodetree that only has a principled. There is only one = where there should be 2. Please paste working code $\endgroup$ Commented Feb 22, 2023 at 10:43

1 Answer 1

2
$\begingroup$

So, a few things...

startwith should be startswith

To get the actual name of the group you need to check its label
if n.label.startswith("Custom Node"):

To compare you need to use ==
A single = is used to assign values.

To check for a particular node inside your group, use the following:

mat = bpy.data.materials.get(bpy.context.selected_objects[0].active_material.name)
mat.use_nodes = True
nodes = mat.node_tree.nodes
for n in nodes:
    if n.label.startswith("Custom Node"):
        for n2 in n.node_tree.nodes.values():
            if n2.name == "Image Texture":
                print (n2.name)
                print("Found Image texture")
$\endgroup$
0

You must log in to answer this question.

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