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