How do I get a list of nodes in the currently active group? context.space_data.node_tree.nodes always returns a list of the nodes at the base level of the tree (not inside any group), even if the user is actually editing the nodes inside a group.

My Material  &lt;-- context.spacedata.node_tree (always)
  └ Group
    └ Group in a group  &lt;-- user is editing this


How can I get a list of the nodes in whatever group the user is busy with, no matter how far down the rabbit hole they've gone?

context.active_node.id_data.nodes is one solution, but it does not work if there is no active node (e.g. if the user just deleted it and hasn't selected anything since).
There doesn't seem to be any obvious solution, so I've come up with a bit of a hack:

The trick is, context.active_node is always the actual active node in the tree the user is currently editing. context.space_data.node_tree.nodes.active (let's call it tree_active for short) is the active node for the base level (outside all groups). If the user is editing a group, tree_active will be a group node (because it was first selected+made active before the user hit Tab to edit it).

So if context.active_node is not the same as tree_active, then we know the user is inside a group. We don't know how deep they are though, so we need to recursively check if tree_active is the same as context.active_node - if it's not, then we use tree_active's node tree as the new base tree we're checking against.

If we are in a group, tree_active will always be a group node (unless it is the same as context.active_node).

Here's the code I eventually came up with:

tree = context.space_data.node_tree  # Base level tree
if tree.nodes.active:
    while tree.nodes.active != context.active_node:
        # We are in a group, check next level
        tree = tree.nodes.active.node_tree


tree will be whatever node tree the user is currently editing.