# How to make node group of an existing material into a new group? (in python)

This is what I would like: I'm looking for an alternative to bpy.ops.node.group_make() Because it seems like a bad approach. I simply want to group a new group nodes of an already existing material, without having to recreate anything, after which I would add the inputs and outputs (the latter does not seem to me a difficult passage) But the problem is that I can't find answers anywhere, I probably try badly, but basically I just want to move my nodes within a group, just like it does bpy.ops.node.group_make()This, however, has problems in the context area so I discarded it regardless

Edit: I want to clarify that this must happen in the context 3D_view the context nodes editor is not necessarily open

Edit 2: I was also thinking if it is possible to easily copy the nodes and their links / parameters without necessarily having to write an exaggeratedly long script, to then insert them in the group node

• Just for clarification, you would be fine with using bpy.ops.node.group_make() if the issue of passing the correct context would be solved? – Robert Gützkow Nov 4 '19 at 0:44
• @rjg Hi, thanks for your interest. Only if it is the only possibility without an alternative, I find it quite frustrating to have to do a loop that checks all the links of the nodes and then having to reconnect them within the group. Basically, I think that's what bpy.ops does, but I'm not sure because I have little experience with groups – Noob Cat Nov 4 '19 at 2:08
• The operator does exactly what happens when you select nodes and then use Add > Group > Make Group or CTRL + G to create a new group based on the selected nodes. Is that the behavior you want or something else? – Robert Gützkow Nov 6 '19 at 13:28
• @rjg Basically yes, but there are context problems, as I need it to work also with the closed nodes editor. This is very important.Currently if I run this bpy.ops.node.group_make () I get (rightly) this error: RuntimeError: Operator bpy.ops.node.group_make.poll () failed, context is incorrect. I need this to work from the "UI" panel in the 3DView via a button – Noob Cat Nov 6 '19 at 14:32
• You could change one of the areas to the node editor and switch back afterwards, but this is not a nice solution. How do you know which nodes should be grouped, since the node editor isn't open so presumably the user didn't select them? – Robert Gützkow Nov 8 '19 at 23:42

I have one method May be this is what you wan't this may work for you if you do not wan't to use bpy.ops. .... then you have to create a node group

import bpy

for mat in bpy.data.materials:
if "Material" in mat.name:
nodes = mat.node_tree.nodes
for node in nodes:
if node.type != 'OUTPUT_MATERIAL':  # skip the material output node as we'll need it later
nodes.remove(node)

#Creating Group Input
input_node = group.nodes.new("NodeGroupInput")
input_node.location = (0, 0)

#Setting Default Values For Node Sockets

group.inputs[0].default_value = (1.000)
group.inputs[1].default_value = (2.000)

#Creating Group Output
output_node = group.nodes.new("NodeGroupOutput")
output_node.location = (500, 0)

# Creating Principled bsdf Node
#You can create any node here which you think are required to be in the group as these will be created automatically in a group

Principled_Bsdf_node.location = (100, 0)

#creating links between nodes in group

# Putting Node Group to the node editor
tree = bpy.context.object.active_material.node_tree