operator_node.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import bpy
  2. def main(operator, context):
  3. space = context.space_data
  4. node_tree = space.node_tree
  5. node_active = context.active_node
  6. node_selected = context.selected_nodes
  7. # now we have the context, perform a simple operation
  8. if node_active in node_selected:
  9. node_selected.remove(node_active)
  10. if len(node_selected) != 1:
  11. operator.report({'ERROR'}, "2 nodes must be selected")
  12. return
  13. node_other, = node_selected
  14. # now we have 2 nodes to operate on
  15. if not node_active.inputs:
  16. operator.report({'ERROR'}, "Active node has no inputs")
  17. return
  18. if not node_other.outputs:
  19. operator.report({'ERROR'}, "Selected node has no outputs")
  20. return
  21. socket_in = node_active.inputs[0]
  22. socket_out = node_other.outputs[0]
  23. # add a link between the two nodes
  24. node_link = node_tree.links.new(socket_in, socket_out)
  25. class NodeOperator(bpy.types.Operator):
  26. """Tooltip"""
  27. bl_idname = "node.simple_operator"
  28. bl_label = "Simple Node Operator"
  29. @classmethod
  30. def poll(cls, context):
  31. space = context.space_data
  32. return space.type == 'NODE_EDITOR'
  33. def execute(self, context):
  34. main(self, context)
  35. return {'FINISHED'}
  36. def register():
  37. bpy.utils.register_class(NodeOperator)
  38. def unregister():
  39. bpy.utils.unregister_class(NodeOperator)
  40. if __name__ == "__main__":
  41. register()