Guest User

TutorialTemplateScript

a guest
Apr 6th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. import bpy
  2.  
  3. # Create compositor group
  4. def create_comp_group(context, operator, group_name):
  5.     # Create a group
  6.     test_group = bpy.data.node_groups.new(group_name, 'CompositorNodeTree')
  7.  
  8.     # Create group inputs
  9.     group_inputs = test_group.nodes.new('NodeGroupInput')
  10.     group_inputs.location = (-350,0)
  11.     test_group.inputs.new('NodeSocketFloat','in_to_greater')
  12.     test_group.inputs.new('NodeSocketFloat','in_to_less')
  13.  
  14.     # Create group outputs
  15.     group_outputs = test_group.nodes.new('NodeGroupOutput')
  16.     group_outputs.location = (300,0)
  17.     test_group.outputs.new('NodeSocketFloat','out_result')
  18.  
  19.     # Create three math nodes in a group
  20.     node_add = test_group.nodes.new('CompositorNodeMath')  
  21.     node_add.operation = 'ADD'
  22.     node_add.location = (100,0)
  23.  
  24.     node_greater = test_group.nodes.new('CompositorNodeMath')
  25.     node_greater.operation = 'GREATER_THAN'
  26.     node_greater.label = 'greater'
  27.     node_greater.location = (-100,100)
  28.  
  29.     node_less = test_group.nodes.new('CompositorNodeMath')
  30.     node_less.operation = 'LESS_THAN'
  31.     node_less.label = 'less'
  32.     node_less.location = (-100,-100)
  33.  
  34.     # Link nodes together
  35.     test_group.links.new(node_add.inputs[0], node_greater.outputs[0])
  36.     test_group.links.new(node_add.inputs[1], node_less.outputs[0])
  37.  
  38.     # Link inputs
  39.     test_group.links.new(group_inputs.outputs['in_to_greater'], node_greater.inputs[0])
  40.     test_group.links.new(group_inputs.outputs['in_to_less'], node_less.inputs[0])
  41.  
  42.     # link output
  43.     test_group.links.new(node_add.outputs[0], group_outputs.inputs['out_result'])
  44.  
  45.     # return the group
  46.     return test_group
  47.  
  48.  
  49. # Operator
  50. class NODE_OT_compGroup(bpy.types.Operator):
  51.     """Tooltip"""
  52.     bl_idname = "node.simple_operator"
  53.     bl_label = "Add Group (Operator)"
  54.  
  55.     @classmethod
  56.     def poll(cls, context):
  57.         space = context.space_data
  58.         return space.type == 'NODE_EDITOR'
  59.  
  60.     def execute(self, context):
  61.  
  62.         # Create the group
  63.         custom_node_name = "my_node"
  64.         my_group = create_comp_group(self, context, custom_node_name)
  65.         comp_node = context.scene.node_tree.nodes.new('CompositorNodeGroup')
  66.         comp_node.node_tree = bpy.data.node_groups[my_group.name]
  67.         comp_node.location = 100,0
  68.  
  69.         return {'FINISHED'}
  70.  
  71.  
  72. # Panel
  73. class NODE_PT_customPanel(bpy.types.Panel):
  74.     bl_idname = "NODE_PT_customPanel"
  75.     bl_space_type = 'NODE_EDITOR'
  76.     bl_label = "Custom Panel"
  77.     bl_region_type = "UI"
  78.     bl_category = "Custom Category"
  79.  
  80.     @classmethod
  81.     def poll(self,context):
  82.         return context.object is not None
  83.  
  84.     def draw(self, context):
  85.         layout = self.layout
  86.         layout.operator(NODE_OT_compGroup.bl_idname)
  87.         layout.separator()
  88.  
  89.  
  90. # Register
  91. def register():
  92.     bpy.utils.register_class(NODE_OT_compGroup)
  93.     bpy.utils.register_class(NODE_PT_customPanel)
  94.  
  95.  
  96. def unregister():
  97.     bpy.utils.unregister_class(NODE_OT_compGroup)
  98.     bpy.utils.unregister_class(NODE_PT_customPanel)
  99.  
  100.  
  101. if __name__ == "__main__":
  102.     register()
Add Comment
Please, Sign In to add comment