LAK132

socket_value_update not working

Oct 27th, 2019
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1.  
  2. bl_info = {"name": "Test2.8", "category": "Node"}
  3.  
  4. import bpy
  5. import nodeitems_utils
  6.  
  7. class CustomNodeTree(bpy.types.NodeTree):
  8.     """Custom Node Tree"""
  9.     bl_idname = "CustomNodeTree"
  10.     bl_label = "Custom Node Tree"
  11.     bl_icon = "NODETREE"
  12.  
  13.     @classmethod
  14.     def poll(tree_class, context):
  15.         return True
  16.  
  17. # class CustomProperty(bpy.types.PropertyGroup):
  18. #     pass
  19.  
  20. # class CustomSocket(byp.types.NodeSocket):
  21. #     pass
  22.  
  23. class CustomNode(bpy.types.Node, CustomNodeTree):
  24.     """Custom Node"""
  25.     bl_idname = "CustomNode"
  26.     bl_label = "Custom Node"
  27.     bl_icon = "OBJECT_DATA"
  28.  
  29.     color: bpy.props.FloatVectorProperty(name="color", size=4, subtype='COLOR')
  30.  
  31.     # socket_value_update(context)
  32.     #   Update after property changes
  33.     def socket_value_update(self, context):
  34.         print(str(self) + ": socket_value_update")
  35.  
  36.     def read_input(self, name):
  37.         if name not in self.inputs:
  38.             return None
  39.         for link in self.inputs[name].links:
  40.             if link.is_valid:
  41.                 return link.from_socket.default_value
  42.         return self.inputs[name].default_value
  43.  
  44.     # update()
  45.     #   Update on editor changes
  46.     def update(self):
  47.         print(str(self) + ": update")
  48.         self.color = self.read_input("Input")
  49.         self.outputs["Output"].default_value = self.color
  50.         print(str(self.inputs["Input"]))
  51.         print(str(self.color))
  52.         print(str(self.outputs["Output"]))
  53.  
  54.     # init(context)
  55.     #   Initialize a new instance of this node
  56.     def init(self, context):
  57.         print("init")
  58.         self.inputs.new("NodeSocketColor", "Input")
  59.         self.outputs.new("NodeSocketColor", "Output")
  60.  
  61.     # copy(node)
  62.     #   Initialize a new instance of this node from an existing node
  63.     #   Parameters: node (Node, (never None)) - Node, Existing node to copy
  64.     def copy(self, node):
  65.         print(str(self) + ": copy from " + str(node))
  66.         self.inputs["Input"].default_value = node.inputs["Input"].default_value
  67.         self.color = node.color
  68.  
  69.     # draw_buttons(context, layout)
  70.     #   Draw node buttons
  71.     #   Parameters: layout (UILayout, (never None)) - Layout, Layout in the UI
  72.     def draw_buttons(self, context, layout):
  73.         layout.prop(self, "color")
  74.  
  75.     # draw_buttons_ext(context, layout)
  76.     #   Draw node buttons in the sidebar
  77.     #   Parameters: layout (UILayout, (never None)) - Layout, Layout in the UI
  78.     def draw_buttons_ext(self, context, layout):
  79.         layout.prop(self, "color")
  80.  
  81.     # draw_label()
  82.     #   Returns a dynamic label string
  83.     #   Returns: Label
  84.     #   Return type: string, (never None)
  85.     def draw_label(self):
  86.         return "Custom Node"
  87.  
  88.     @classmethod
  89.     def poll(node_class, context):
  90.         return True
  91.  
  92. class CustomNodeCategory(nodeitems_utils.NodeCategory):
  93.     @classmethod
  94.     def poll(category_class, context):
  95.         return True
  96.  
  97. node_categories = [
  98.     CustomNodeCategory(
  99.         "CUSTOM",
  100.         "Custom",
  101.         items = [
  102.             nodeitems_utils.NodeItem("CustomNode")
  103.         ]
  104.     )
  105. ]
  106.  
  107. registered = False
  108.  
  109. classes = (
  110.     CustomNodeTree,
  111.     CustomNode,
  112. )
  113.  
  114. class_reg, class_unreg = bpy.utils.register_classes_factory(classes)
  115.  
  116. def register():
  117.     class_reg()
  118.     nodeitems_utils.register_node_categories("CUSTOM", node_categories)
  119.     registered = True
  120.  
  121. def unregister():
  122.     try:
  123.         nodeitems_utils.unregister_node_categories("CUSTOM")
  124.     except:
  125.         print("Failed to unregister CUSTOM")
  126.     class_unreg()
  127.     registered = False
  128.  
  129. if __name__ == "__main__":
  130.     print("\n")
  131.     try:
  132.         unregister()
  133.     except:
  134.         print("Failed to unregister")
  135.     # try:
  136.     #     register()
  137.     # except:
  138.     #     print("Failed to register")
  139.     register()
Advertisement
Add Comment
Please, Sign In to add comment