7
$\begingroup$

How can I set the output socket type of a nodegroup output socket from python?

I don't even know of any good way to do this in the UI.. The only way I know of in the UI is to connect a node output of the type I want (e.g. a yellow color output) to the Group Output node.

I tried setting socket.type, however there is no error message and the socket type does not change.

This is important because the default is a 'VALUE' socket, which converts colors into grayscale.

Here is the relevant part of my script:

main_group = bpy.data.node_groups.new(type="ShaderNodeTree", name="Object Color")
main_group.outputs.new("NodeSocketFloat", "Object Color")
output_node = main_group.nodes.new("NodeGroupOutput")
output_node.location = (600, 0)
output_node.inputs["Object Color"].type = 'RGBA'
$\endgroup$

2 Answers 2

8
$\begingroup$

You define the type when you create the socket with new() Looking at the list on the bpy.types page there are several starting with NodeSocket that can be used.

The variations of each type alter the visual display - eg. FloatPercent shows % with the value.

NodeSocketBool
NodeSocketColor
NodeSocketFloat
NodeSocketFloatAngle
NodeSocketFloatFactor
NodeSocketFloatPercentage
NodeSocketFloatTime
NodeSocketFloatUnsigned
NodeSocketInt
NodeSocketIntFactor
NodeSocketIntPercentage
NodeSocketIntUnsigned
NodeSocketShader
NodeSocketString
NodeSocketVector
NodeSocketVectorAcceleration
NodeSocketVectorDirection
NodeSocketVectorEuler
NodeSocketVectorTranslation
NodeSocketVectorVelocity
NodeSocketVectorXYZ
$\endgroup$
4
$\begingroup$

To update sambler's answer, here's the updated list of node socket types.

Here I did a little script that creates a node group with all sockets available (2.92)

import bpy

allSockets = bpy.data.node_groups.new("All Sockets Shader", "ShaderNodeTree")
#toggle comment the previous line and uncomment the next line to create a Geometry Node Group
#allSockets = bpy.data.node_groups.new("All Sockets Geometry", "GeometryNodeTree")

for nm in ["Bool",
            "Collection",
            "Color",
            "Float",
            "FloatAngle",
            "FloatFactor",
            "FloatPercentage",
            "FloatTime",
            "FloatUnsigned",
            "Geometry",
            "Image",
            "Int",
            "IntFactor",
            "IntPercentage",
            "IntUnsigned",
            "Object",
            "Shader",
            "String",
            "Vector",
            "VectorAcceleration",
            "VectorDirection",
            "VectorEuler",
            "VectorTranslation",
            "VectorVelocity",
            "VectorXYZ",
            "Virtual"]:
    type = "NodeSocket" + nm
    print(nm +" is a node socket of type "+ type)
    allSockets.inputs.new(type, nm)
    allSockets.outputs.new(type, nm)

EDIT: updated list for later versions

for nm in [
    "Bool",
    "Collection",
    "Color",
    "Float",
    "FloatAngle",
    "FloatDistance",        #2.93
    "FloatFactor",
    "FloatPercentage",
    "FloatTime",
    "FloatTimeAbsolute",    #3.0
    "FloatUnsigned",
    "Geometry",
    "Image",
    "Int",
    "IntFactor",
    "IntPercentage",
    "IntUnsigned",
    "Material",             #3.0
    "Menu",                 #4.1
    "Object",
    "Rotation",             #4.0
    "Shader",
    "String",
    "Texture",              #3.0
    "Vector",
    "VectorAcceleration",
    "VectorDirection",
    "VectorEuler",
    "VectorTranslation",
    "VectorVelocity",
    "VectorXYZ",
    "Virtual",
]:
$\endgroup$

You must log in to answer this question.

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