0
$\begingroup$

I am making an add-on in which many node groups can be inserted using:

My_NodeGroup = bpy.context.object.modifiers.active.node_group.nodes.new(type='GeometryNodeGroup')

The problem is that the Node Groups can only be inserted at the top, in the Geometry Nodes.

enter image description here

How can I make dynamically that the node group is always inserted where the user is? So e.g. In a Node Group of a Node Group?

enter image description here

$\endgroup$
2
  • $\begingroup$ NodeGroup.001 is a node of type GROUP, so you can use node_tree = node.id_data which will give NodeGroup.node_tree $\endgroup$
    – Karan
    Commented Mar 16, 2023 at 10:24
  • $\begingroup$ And how do I use it to add the node group to the correct group the user is in? $\endgroup$
    – Andi
    Commented Mar 16, 2023 at 15:14

2 Answers 2

0
$\begingroup$

I found the solution in the node operator source code.

To insert a node where the user is located:

space = context.space_data
tree = space.edit_tree
My_NodeGroup = tree.nodes.new(type='GeometryNodeGroup')
$\endgroup$
0
$\begingroup$

Use node.id_data where

node = context.active_node

This will give you node_tree of the context.active_node

context.active_node is different for every node_tree, You have to select a node.

node_tree = context.active_node.id_data
node_tree.nodes.new(type='GeometryNodeGroup')
$\endgroup$
3
  • $\begingroup$ Context has no "active_node" attribute. And even if it did: Every group becomes active when you are in it. But groups can also be active when you are not in them. $\endgroup$
    – Andi
    Commented Mar 17, 2023 at 9:16
  • $\begingroup$ Are you running in the python console? $\endgroup$
    – Karan
    Commented Mar 17, 2023 at 10:03
  • $\begingroup$ Found the solution. Thank you, Karan. $\endgroup$
    – Andi
    Commented Mar 17, 2023 at 10:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .