Is there any way to create a boolean input to a ShaderNodeGroup in Blender 4.x using python?

I’m working now on a plugin that includes NodeGroups and I want to make a Boolean Socket. I remember that in the older versions it was possible to do this, but in the Interface system, I didn’t really find a way because it limits to very specific types of input when creating a Socket.
So is there a way to do it?

A boolean socket or a Boolean property?

Eevee and Cycles don’t use Booleans, so a Socket will need to be a SocketFloat (unless the socket is not used in the nodetree).

For a Boolean property, you need to inherit from ShaderNodeCustomGroup, and add the property to your own class, and add it to the layout of your node in the draw_buttons call.

2 Likes

Boolean Sockets are mentioned in the 4.0 API

Like Secrop mentioned, you could also use a Float, but I’ve found that an Integer will work even better if you can set a min and max: 0 will return False, and 1 will return True.

EDIT: Secrop and testure are right, the Boolean sockets are only for Geometry Nodes, and you cannot use an Integer socket in a standard ShaderNodeGroup, your only options are Float, Vector, Shader and Color.

2 Likes

there’s a lot of shared code for nodetrees between shaders and geometry nodes, I would assume that the boolean you see mentioned in the docs is for geometry nodes

2 Likes

It’s still possible to use a SocketBoolean in a ShaderNode (even when is a ShaderNodeGroup that references a Nodetree with such an input in the interface, aka Nodegroups)…
But without any ‘under the blankets’ conversion, the engines (Eevee and Cycles) will stop right there, when reading the Nodetree, and anything beyond that (any connections to that socket or its value) will be completly ignored!

With ShaderNodeCustomGroups, one can do on-the-fly conversions and changes, whenever the nodetree/socket value changes, and, if needed, calculate the incoming value from a link or from the socket interface, and make the appropriate modifications to the inner Nodetree/out sockets that can be translated to the render engine.

Edit: The Node Editor still accepts all kind of Sockets and Nodes, because the Editor can be used in other engines (LuxCore, AppleSeed, etc), and they may have a builtin logic to deal with whatever a socket or a node may output.

2 Likes