2
$\begingroup$

This is what I would like: enter image description here 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

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Robert Gützkow Nov 4 '19 at 0:44
  • $\begingroup$ @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 $\endgroup$ – Pastrokkio Nov 4 '19 at 2:08
  • $\begingroup$ 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? $\endgroup$ – Robert Gützkow Nov 6 '19 at 13:28
  • $\begingroup$ @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 $\endgroup$ – Pastrokkio Nov 6 '19 at 14:32
  • $\begingroup$ 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? $\endgroup$ – Robert Gützkow Nov 8 '19 at 23:42
0
$\begingroup$

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)
group = bpy.data.node_groups.new(type="ShaderNodeTree", name="Test Group")


#Creating Group Input
group.inputs.new("NodeSocketShader", "Input1")
group.inputs.new("NodeSocketShader", "Input2")
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
group.outputs.new("NodeSocketShader", "Output1")
group.outputs.new("NodeSocketShader", "Output2")
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 = group.nodes.new('ShaderNodeBsdfPrincipled')
Principled_Bsdf_node.location = (100, 0)


#creating links between nodes in group
group.links.new(input_node.outputs["Input1"], Principled_Bsdf_node.inputs[0])
group.links.new(Principled_Bsdf_node.outputs["BSDF"], output_node.inputs[0])

# Putting Node Group to the node editor
tree = bpy.context.object.active_material.node_tree
group_node = tree.nodes.new("ShaderNodeGroup")
group_node.node_tree = group
group_node.location = (-40,0)

#connections bewteen node group to output 

ShaderNodeOutputMaterial_node = tree.nodes["Material Output"]
links = tree.links    
link = links.new(group_node.outputs[0], ShaderNodeOutputMaterial_node.inputs[1])
$\endgroup$
  • 1
    $\begingroup$ Thank you for the answer, but this method does not group, this creates a new group but does not contain the existing ones, it seems to me very far from bpy.ops.node.group_make (), the problem is how to group existing nodes. This method you indicated requires the creation of new nodes which is good if you want to build a set of nodes from "0" but not useful for grouping existing nodes $\endgroup$ – Pastrokkio Nov 8 '19 at 18:18

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.