The post you linked has a functional solution, but it isn't doing what you ask here.
tree = context.space_data.node_tree
# set the location if node type is
for n in tree.nodes:
if n.type == "TEX_IMAGE":
print (n.location.x)
n.location.x += 50
# filter by type
image_texture_nodes = [n for n in tree.nodes if n.type == "TEX_IMAGE"]
# get the names
image_texture_names = [n.name for n in image_texture_nodes]
# check the console
print (image_texture_nodes)
print (image_texture_names)
The only things it prints are the X location and name of Image Texture nodes. It isn't designed to print all the nodes' names in the material you run that operator from. You need to tweak that code for that.
import bpy
class NodeOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "node.simple_operator"
bl_label = "Simple Node Operator"
# make sure we are in the Node Editor to run the operator
@classmethod
def poll(cls, context):
space = context.space_data
return space.type == 'NODE_EDITOR'
def execute(self, context):
# get the current node tree in the editor
tree = context.space_data.node_tree
# cycle through each nodes in the node tree
for n in tree.nodes:
# print the node's name
print(n.name)
return {'FINISHED'}
def register():
bpy.utils.register_class(NodeOperator)
def unregister():
bpy.utils.unregister_class(NodeOperator)
if __name__ == "__main__":
register()
That being said, if your goal is to work on specific kinds of nodes like image texture nodes, then you need to add back the if n.type == "TEX_IMAGE":
condition in the for
loop, like the other post's answer did.
I highly recommand you to check the documentation, it doesn't just describe the python API, it also explains things like how to access data.
<bpy_collection[2], Nodes>
is a collection. You only need to iterate over it:for n in nodes: ...
$\endgroup$