0
$\begingroup$

I'm trying to fix an old discontinued addon, but the new bpy changes in 4.0 are throwing me off. I've tried implementing the new 'tree.interface' elements, but it keeps spitting errors. And of course ChatGPT was of no help as it's stuck on the old bpy.

        materialname = bpy.context.active_object.active_material.name
        material = bpy.data.materials[materialname]
        matnodes = material.node_tree.nodes
        i = 0
        while i < len(matnodes):
            if matnodes[i].select:
                node_group = matnodes[i].node_tree
                j = 0
                while j < len(matnodes[i].inputs):
                    node_group.inputs[j].default_value = matnodes[i].inputs[j].default_value
                    j += 1
            i += 1

Basically I to use this functionality from 3.0 BPY, where it takes your arbitrarily selected node group, and you hit a button that runs this function, and it takes all of the values of the sliders in your node groups UI, and sets those sockets defaults as their current values.

I've been trying to fix this for a while as my workflow is dependent on this functionality for mass amounts of custom group presets, and being able to set their new defaults as their current set display values is important for appending those groups through an addon or simply through the append option.

Many thanks in advance,

Cody (SMOUSE)

$\endgroup$
1
  • $\begingroup$ fwiw you can replace your while loops with for i in range(len(matnodes)): $\endgroup$
    – Gorgious
    Jan 8 at 15:03

1 Answer 1

2
$\begingroup$

node_group is the node group inside the material node tree

node_group.inputs will get inputs of that node_group

to change the default values of Group Inputs of that node_group

you have to change thenode_group.node_tree.interface.items_tree[input.name].default_value to input.default_value by looping over node_group.inputs

import bpy


node_group = bpy.data.materials['Material'].node_tree.nodes['Group']


for input in node_group.inputs:
    node_group.node_tree.interface.items_tree[input.name].default_value = input.default_value
import bpy
from bpy.types import Operator


class NODE_OT_set_group_default(Operator):
    bl_idname = "node.set_group_default"
    bl_label = "Set Group Default"
    bl_description = "Set Group Default Value"
    bl_options = {"REGISTER", "UNDO"}

    @classmethod
    def poll(cls, context):
        return context.active_node is not None and context.active_node.type == "GROUP"

    def execute(self, context):
        node_group = context.active_node

        for input in node_group.inputs:
            # if input.is_linked or input.hide_value:
            #     continue

            node_group.node_tree.interface.items_tree[input.name].default_value = input.default_value

        self.report({'INFO'}, "Group Default Value Set")
        return {"FINISHED"}


classes = (NODE_OT_set_group_default,)


def draw_operator(self, context):
    layout = self.layout
    layout.operator(NODE_OT_set_group_default.bl_idname)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.NODE_HT_header.append(draw_operator)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

    bpy.types.NODE_HT_header.remove(draw_operator)


if __name__ == "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ That worked flawlessly, thank you very much! :) $\endgroup$ Jan 8 at 6:31

You must log in to answer this question.

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