首页 > 解决方案 > Blender插件中的自定义节点套接字和CustomSocketInterface

问题描述

我正在使用 Blender 2.79,但 Blender 2.8+ 可能运行相同

自定义节点示例

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

我如何使用这个NodeSocketInterface类?

标签: pythonsocketsnodesblender

解决方案


这是由于没有注册NodeSocketInterface. 注册子类时,会自动创建一个没有所需方法/属性NodeSocket的虚拟对象。NodeSocketInterface

我基于对 Blender 源代码的理解编写了以下代码,它适用于将 aCustomSocketInterface关联到 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)

推荐阅读