Search code examples
pythonsocketsnodesblender

Custom node sockets and CustomSocketInterface in Blender Addon


I'm using Blender 2.79 but Blender 2.8+ likely operates the same

Example for custom nodes

Initially, I was using NodeSocket subclasses as custom sockets, but eventually I got this error AttributeError: 'NodeSocketInterface' object has no attribute 'draw_color'. Others also had issues with default_value not being present in NodeSocketInterface.

How do I use this NodeSocketInterface class?


Solution

  • This is due to not registering a NodeSocketInterface. When registering the NodeSocket subclass, a dummy NodeSocketInterface without the required methods/properties is automatically created.

    I wrote the following code based on understanding the Blender source code, and it works for registering a CustomSocketInterface associated to a CustomSocket:

    # Blender 2.79
    # (bl_idname defaults to class names if not set)
    
    class CustomSocketInterface(bpy.types.NodeSocketInterface):
        bl_socket_idname = 'CustomSocket' # required, Blender will complain if it is missing
        # those are (at least) used under Interface in N-menu in
        # node editor when viewing a node group, for input and output sockets
        def draw(self, context, layout):
            pass
        def draw_color(self, context):
            return (0,1,1,1)
    
    class CustomSocket(bpy.types.NodeSocket):
        def draw(self, context, layout, node, text):
            pass
        def draw_color(self, context, node):
            return (0,1,1,1)
    ...
    # register (didn't try but order should matter)
        bpy.utils.register_class(CustomSocketInterface)
        bpy.utils.register_class(CustomSocket)