Get list of selected Nodes

Is there any way to get list of selected nodes in active editor?

Within operator the “context.active_node” returns the active node in active editor.
but I did not found some thins like “context.selected_node” to return the list of nodes has selected.

Other types hase “selected” method too
…context.active_object, context.selected_objects
…context.active_bone, context.selected_bones
…context.selected_sequences

Python list aggregation might be the quickest way here:

sel = [x for x in context.node_tree.nodes if x.select]

2 Likes

Great

This sel = [x for x in context.scene.node_tree.nodes if x.select] worked.

But works only for Composite Node editor. each editor has different area type such as ‘material’, ‘world’, ‘texture’, and …

Thank you :blush:

So use the proper node_tree then.

Just use context.selected_nodes :stuck_out_tongue:

Ha Ha " context.selected_nodes" was the first thing I tested and did not worked on 2.80, but now it`s working on the last build. :smile:

1 Like

Does it though? Am I stupid or why doesn’t this work for me?
2020-02-13 21_29_51-Blender

Hi,
If you type the “bpy.context.selected_node” in console you will face the error but if you call that inside node view it will work.

class Node_OT_test(Operator):
    bl_idname = "node.test"
    bl_label  = "Test"
    def execute(self, context):
	selection = context.selected_nodes
        print(selection)
	return {'FINISHED'}
1 Like