Blender Addon 中的自定义节点套接字和 CustomSocketInterface

Custom node sockets and CustomSocketInterface in Blender Addon

我使用的是 Blender 2.79,但 Blender 2.8+ 可能运行相同

Example for custom nodes

最初,我使用 NodeSocket subclasses 作为自定义套接字,但最终我遇到了这个错误 AttributeError: 'NodeSocketInterface' object has no attribute 'draw_color'。其他人也有 default_value 不存在于 NodeSocketInterface 的问题。

我该如何使用这个 NodeSocketInterface class?

这是因为没有注册NodeSocketInterface。注册 NodeSocket 子类时,没有所需 methods/properties 的虚拟 NodeSocketInterfaceautomatically created.

我在理解 Blender 源代码的基础上编写了以下代码,它适用于注册与 CustomSocket 关联的 CustomSocketInterface:

# 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)