Nodes - How to attach a property to a socket?

Hi I want to attach a socket to a custom node and then map this socket to a property. For example when the Node is updated it will print the value of the property.

However as I see here in this wiki, this example code might be outdated.
http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes

I get this error…
AttributeError: ‘NodeSocketFloat’ object has no attribute ‘value_property’


import bpy

class MyCustomNode(bpy.types.Node):
    bl_label = "Custom Node"
    bl_idname = "CustomNodeType"
    myFloatPropertyA = bpy.props.FloatProperty(name="Size A")

    def init(self, context):
        print("Node Initialized: ", self.name)
        socket = self.inputs.new("NodeSocketFloat", "Input A")
        socket.value_property = "myFloatPropertyA"
        print(dir(socket))

    def update(self):
        print("Updating node: ", self.name)

class MyCustomNodeAdd(bpy.types.Operator):
    bl_label = "Add Custom Node"
    bl_idname = "node.add_custom_node"

    def execute(self, context):
        bpy.ops.node.add_node(type="CustomNodeType")
        return {"FINISHED"}

bpy.utils.unregister_module(__name__)
bpy.utils.register_module(__name__)

What is the way to solve this?

first of all ID name must have a Dot in name and no upper case letters !

bl_idname = “customnode.type”

you also need to select the mat list and be in the node part for it !

also is this for cycles ?

I have examples for cycles but not for Bl!

happy bl

Hi Ricky

first of all ID name must have a Dot in name and no upper case letters !
I know this that is true for operators, but is it for Nodes too? I have looked at other code repositories as well and people seem to declare the ID of the node in TitleCase, if it is.

you also need to select the mat list and be in the node part for it !
I get only Python exception/error when the operator runs, when I have not an active NodeTree. I try to find a way to prevent this from happening (operator should poll if it’s an active node tree, if this is correct approach).

also is this for cycles ?
My node will not be targeted specifically to Internal or Cycles, it will be more of a math/logic node that handles only numbers.

Questions:

  1. This tutorial might seem outdated, I can’t see if there’s a way to connect an existing property to a socket.
    http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes

  2. I think that there is no point to link socket and properties, since sockets can handle data by themselves.

  3. I think that node properties are useful only at the draw method, otherwise they have no use at all.
    http://www.blender.org/api/blender_python_api_2_73_release/bpy.types.NodeSocket.html#bpy.types.NodeSocket.draw

I have tried some new stuff and here is the code:

import bpy

class MyCustomNode(bpy.types.Node):
    bl_label = "Custom Node"
    bl_idname = "CustomNodeType"
    
    #myFloatProperty = bpy.props.FloatProperty(name="Property")
    
    def init(self, context):
        print("Node Initialized: ", self.name)
        self.inputs.new("NodeSocketFloat", "Value1")
        self.inputs.new("NodeSocketFloat", "Value2")
        self.outputs.new("NodeSocketFloat", "Result")

    def update(self):
        print("Updating node: ", self.name)
        
        self.outputs["Result"].default_value = \
            self.inputs["Value1"].default_value + \
            self.inputs["Value2"].default_value
        print("Result: ", self.outputs["Result"].default_value)
        
# Temporary operator for adding "MyCustomNode".
# Search in spacebar menu from any node tree.
class MyCustomNodeAdd(bpy.types.Operator):
    bl_label = "Add Custom Node"
    bl_idname = "node.add_custom_node"
    def execute(self, context):
        bpy.ops.node.add_node(type="CustomNodeType")
        return {"FINISHED"}

bpy.utils.unregister_module(__name__)
bpy.utils.register_module(__name__)

For any other suggestions or ideas I am interested to learn. :slight_smile:

well I have some examples for making cycles nodes
but not using the method with custom class for nodes!

check the big thread on nodes scripting
this guy might be able to help for this !

happy cl