3
$\begingroup$

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  <-- context.spacedata.node_tree (always)
  └ Group
    └ Group in a group  <-- 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).

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

You can use edit_tree instead of node_tree to get the tree currently being edited and access its nodes property for the list of nodes, e.g.:

context.space_data.edit_tree.nodes
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.