2
$\begingroup$

I created a custom pynode intended to multiply vectors. This script is based on the vid from Atom and various code I found on the internet and the blender wiki pages.

The problem I am having is that I can not get correct input values from the socket which is inserted. Added on top of that I do not have the feeling it is updating correctly. A simple setup with only values (math nodes + combine xyz) and print commands in the nodes proofs that it is only getting value (0,0,0).

What am I doing wrong?

I noticed some remarks on not getting updates on nodes but a simple setup should work.


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

class MyMultTree(NodeTree):
    bl_idname = 'MultTreeType'
    bl_label = 'Mult Node Tree'

class MyMultTreeNode :
    @classmethod
    def poll(cls, ntree):
        b = False
        if ntree.bl_idname == 'ShaderNodeTree': b = True
        return b

class MyMultNode(Node, MyMultTreeNode):
    '''A custom node'''
    bl_idname = 'MultNodeType'
    bl_label = 'Vector Multiply'
    bl_icon = 'INFO'

    def update_value(self, context):

        self.inputs["Vector1"].default_value = self.vector_value
        Vector1 = self.inputs["Vector1"]
        self.inputs["Vector2"].default_value = self.vector_value
        Vector2 = self.inputs["Vector2"]
        self.outputs["Vector3"].default_value = Vector(Vector1.default_value) * Vector(Vector2.default_value)
        self.update ()

    vector_value = bpy.props.FloatVectorProperty(default=(0.5, 0.5, 0.5), update = update_value) 

    def init(self, context):
        self.inputs.new('NodeSocketVector', "Vector1")
        self.inputs["Vector1"].default_value = self.vector_value

        self.inputs.new('NodeSocketVector', "Vector2")
        self.inputs["Vector2"].default_value = self.vector_value

        self.outputs.new('NodeSocketVector', "Vector3")
        self.outputs["Vector3"].default_value = self.vector_value

    def update(self):
        try:
            in_v1 = self.inputs["Vector1"]
            in_v2 = self.inputs["Vector2"]
            out = self.outputs["Vector3"]
            can_continue = True
        except:
            can_continue = False

        if can_continue:
            if in_v1.is_linked:
                for i1 in in_v1.links:
                    if i1.is_valid:
                        self.inputs["Vector1"].default_value = i1.from_socket.node.outputs[i1.from_socket.name].default_value

            if in_v2.is_linked:
                for i2 in in_v2.links:    
                    if i2.is_valid:
                        self.inputs["Vector2"].default_value = i2.from_socket.node.outputs[i2.from_socket.name].default_value

            if out.is_linked:
                for o in out.links:
                    if o.is_valid:
                        o.to_socket.node.inputs[o.to_socket.name].default_value = self.outputs["Vector3"].default_value

    def draw_label(self):
        return "Multiply vector"

import nodeitems_utils
from nodeitems_utils import NodeCategory, NodeItem

class MyNodeCategory(NodeCategory):
    @classmethod
    def poll(cls, context):
        b = False
        if context.space_data.tree_type == 'ShaderNodeTree': b = True
        return b

node_categories = [
    MyNodeCategory("SOMENODES", "Mult Vector", items=[
        NodeItem("MultNodeType"),
        ]),
    ]

def register():
     bpy.utils.register_class(MyMultNode)
     nodeitems_utils.register_node_categories("MULT_NODES", node_categories)

def unregister():
    nodeitems_utils.unregister_node_categories("MULT_NODES")
    bpy.utils.unregister_class(MyMultNode)

if __name__ == "__main__":
    register()

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.