Is it possible to check to see if a certain node exists on a material from within Python, for example a diffuse node? I am using Cycles Material Nodes, not Blender Internal or Post-Processing.
You can use the
bpy_prop_collection.get method which acts like pythons
import bpy mat = bpy.data.materials["MyMaterial"] if mat.use_nodes: ntree = mat.node_tree node = ntree.nodes.get("Diffuse BSDF", None) if node is not None: print("We Found:", node)
This is similar to using:
node = ntree.nodes["Diffuse BSDF"]
However if the node isn't found you'll get an exception.
You can iterate through all the nodes in the node tree and check their properties.
For example, to find if a material node tree contains any diffuse shaders, you could do this:
import bpy mat = bpy.data.materials['MyMaterial'] # Test if 'Use Nodes' is enabled: if mat.use_nodes == True: nodes = mat.node_tree.nodes # Iterate through all the nodes in the node tree and test each one to see if it's a diffuse shader: for node in nodes: if node.bl_idname == 'ShaderNodeBsdfDiffuse': # Do stuff else: # Do other stuff
You can test any node property (e.g.
name, etc.), to see a full list check the API reference.