0
$\begingroup$

I'm new to scripting in Blender 2.91.0 and I'm trying to modify nodes via code. According to the API I should be able to access my current node selection via the Node Context.

My material is using nodes and I can access them like this:

bpy.data.materials[0].node_tree.nodes["Image Texture"]

However, for some reason, I am not able to access the node context at all. Neither bpy.context.selected_nodes nor bpy.context.active_node seem to exist, so I can't get the current node selection. Contradicting the documentation my Python console autocompletion doesn't show them as valid options (see image).

Screenshot of Python console autocompletion. active_node is not available.

Note: Executing bpy.context.active_node anyway returns the error 'Context' object has no attribute 'active_node'

Does anyone have an idea what I am missing? Any help is appreciated.

$\endgroup$
2
  • 1
    $\begingroup$ The context members active_node is available when in the node editor space. Eg can use it in a panel that is registered to that space. Then it is run within the context of the Node editor space. A material is associated with that space., the node tree, the nodes and the active one. The context when run from the text editor or python console does not. If your operator is invoked from a button in that space it has "Node context". $\endgroup$ – batFINGER Feb 7 at 12:54
  • $\begingroup$ Related: Control Cycles material nodes and material properties in Python $\endgroup$ – brockmann Feb 7 at 14:31