How can I modify the layout.label in a custom node in the Node Editor?

by sameen kee   Last Updated January 14, 2019 10:15 AM

enter image description here

As you can see, I created some custom nodes, and I need to modify the middle label in each node when I create a new node, how can I do this?

Here is my source code:

import bpy
from bpy.types import NodeTree, Node, NodeSocket

class VadCustomTree(NodeTree):
    '''A vad node tree type that will show up in the node editor header'''
    bl_idname = 'VadTreeType'
    bl_label = 'Vad Node Tree'
    bl_icon = 'NODETREE'

# Vad socket type
class VadCustomSocket(NodeSocket):
    '''Vad node socket type'''
    bl_idname = 'VadSocketType'
    bl_label = 'Vad Node Socket'

    def draw(self, context, layout, node, text):
        layout.label(text)

    def draw_color(self, context, node):
        return (1.0, 0.4, 0.216, 0.5)


class VadCustomTreeNode:
    @classmethod
    def poll(cls, ntree):
        return ntree.bl_idname == 'VadTreeType'


class VadCustomNode(Node, VadCustomTreeNode):
    '''A custom node'''
    bl_idname = 'VadNodeType'
    bl_label = 'Vad Node'
    bl_icon = 'SOUND'

    def init(self, context):
        self.inputs.new('VadSocketType', " ")

        self.outputs.new('NodeSocketColor', " ")

    def copy(self, node):
        print("Copying from node ", node)

    def free(self):
        print("Removing node ", self, ", Goodbye!")

    def draw_buttons(self, context, layout):
        layout.label("0x70238a30")

    def draw_label(self):
        return "vad node"

import nodeitems_utils
from nodeitems_utils import NodeCategory, NodeItem

class VadNodeCategory(NodeCategory):
    @classmethod
    def poll(cls, context):
        return context.space_data.tree_type == 'VadTreeType'

node_categories = [
    # identifier, label, items list
    VadNodeCategory("SOMENODES", "Some Nodes", items=[
        # our basic node
        NodeItem("VadNodeType"),
        ]),
    ]

def register():
    bpy.utils.register_class(VadCustomTree)
    bpy.utils.register_class(VadCustomSocket)
    bpy.utils.register_class(VadCustomNode)

    nodeitems_utils.register_node_categories("CUSTOM_NODES", node_categories)


def unregister():
    nodeitems_utils.unregister_node_categories("CUSTOM_NODES")

    bpy.utils.unregister_class(VadCustomTree)
    bpy.utils.unregister_class(VadCustomSocket)
    bpy.utils.unregister_class(VadCustomNode)


if __name__ == "__main__":
    register()
    bpy.context.scene.use_nodes = True
    tree = bpy.context.scene.node_tree

    # clear default nodes
    for node in tree.nodes:
        tree.nodes.remove(node)

    # create input vad node
    vad_node = tree.nodes.new(type='VadNodeType')
    vad_node.location = 0,0


    vad_node1 = tree.nodes.new(type='VadNodeType')   
    vad_node1.location = 400,0
    vad_node1.label = "lalalala" # vad node's header, not the content

    vad_node2 = tree.nodes.new(type='VadNodeType')
    vad_node2.location = 400,200

    links = tree.links
    links.new(vad_node.outputs[0], vad_node1.inputs[0])
    links.new(vad_node.outputs[0], vad_node2.inputs[0])

I really appreciate your answer.

Tags : python nodes


Related Questions


Updated July 29, 2018 21:15 PM

Updated April 05, 2018 14:15 PM

Updated July 23, 2018 02:15 AM

Updated April 13, 2015 02:06 AM

Updated May 03, 2015 21:06 PM