DarkfallBlender

Untitled

Mar 18th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.75 KB | None | 0 0
  1. import bpy
  2.  
  3.  
  4. class NODE_PT_MAINPANEL(bpy.types.Panel):
  5.     bl_label = "Custom Node Group"
  6.     bl_idname = "NODE_PT_MAINPANEL"
  7.     bl_space_type = 'NODE_EDITOR'
  8.     bl_region_type = 'UI'
  9.     bl_category = 'New Tab'
  10.  
  11.     def draw(self, context):
  12.         layout = self.layout
  13.  
  14.         row = layout.row()
  15.         row.operator('node.test_operator')
  16.  
  17.  
  18.  
  19. def create_test_group(context, operator, group_name):
  20.    
  21.         #enable use nodes
  22.     bpy.context.scene.use_nodes = True
  23.    
  24.     test_group = bpy.data.node_groups.new(group_name, 'ShaderNodeTree')
  25.    
  26.     group_in = test_group.nodes.new('NodeGroupInput')
  27.     group_in.location = (-300,0)
  28.     test_group.inputs.new('NodeSocketFloat','Factor Value') #0
  29.     test_group.inputs.new('NodeSocketColor','Color Input') #1
  30.    
  31.    
  32.     group_out = test_group.nodes.new('NodeGroupOutput')
  33.     group_out.location = (400,0)
  34.     test_group.outputs.new('NodeSocketColor','Output')
  35.    
  36.    
  37.     node_add = test_group.nodes.new('ShaderNodeMath')
  38.     node_add.operation = 'ADD'
  39.     node_add.location = (100,0)
  40.    
  41.     node_greater = test_group.nodes.new('ShaderNodeMath')
  42.     node_greater.operation = 'GREATER_THAN'
  43.     node_greater.label = 'greater'
  44.     node_greater.location = (-100,100)
  45.    
  46.     node_less = test_group.nodes.new('ShaderNodeMath')
  47.     node_less.operation = 'LESS_THAN'
  48.     node_less.label = 'less'
  49.     node_less.location = (-100,-100)
  50.    
  51.     # enlazar los nodos juntos
  52.     test_group.links.new(node_add.inputs[0], node_greater.outputs[0])
  53.     test_group.links.new(node_add.inputs[1], node_less.outputs[0])
  54.    
  55.     # vincular las entradas
  56.     test_group.links.new(node_add.outputs[0], group_out.inputs[0])
  57.    
  58.    
  59.     # vincular la salida
  60.     test_group.links.new(node_greater.inputs[0], group_in.outputs[0])
  61.     test_group.links.new(group_in.outputs[0], node_less.inputs[0])
  62.    
  63.    
  64.    
  65.     return test_group
  66.    
  67.    
  68.  
  69.  
  70.            
  71. class NODE_OT_TEST(bpy.types.Operator):
  72.     bl_label = "Add Custom Node Group"
  73.     bl_idname = 'node.test_operator'
  74.    
  75.     def execute(self, context):
  76.        
  77.        
  78.         #First we must create a material and give it a name. I call it, Test
  79.         #Primero debemos crear un material y darle un nombre. Lo llamo, prueba
  80.         material_test = bpy.data.materials.new(name= "Test")
  81.        
  82.        
  83.         #Next Enable the Nodes
  84.         #Siguiente Habilitar los nodos
  85.         material_test.use_nodes = True
  86.        
  87.        
  88.         #Then remove the Principled Node: in this instance we do not need it
  89.         #Luego elimine el Nodo Principled: en este caso no lo necesitamos
  90.         material_test.node_tree.nodes.remove(material_test.node_tree.nodes.get('Principled BSDF'))
  91.        
  92.        
  93.         #create a reference to the Output node (we need this later)
  94.         #crear una referencia al nodo Salida (necesitamos esto más adelante)
  95.         material_output = material_test.node_tree.nodes.get('Material Output')
  96.         #set the location for the output
  97.         #establecer la ubicación para la salida
  98.         material_output.location = (400,0)
  99.        
  100.        
  101.         #This will give the Custom Group a Name, I call it Test Node
  102.         #Esto le dará un nombre al grupo personalizado, lo llamo Test Node
  103.         custom_node_name = "Test Node"
  104.         #Here we tell the Test Node to use the Group we created
  105.         #Aquí le decimos al nodo de prueba que use el grupo que creamos
  106.         my_group = create_test_group(self, context, custom_node_name)
  107.        
  108.        
  109.         #This will add a blank Custom Node to the Material
  110.         #Esto agregará un nodo personalizado en blanco al material
  111.         test_node = material_test.node_tree.nodes.new('ShaderNodeGroup')
  112.        
  113.         #Here we tell the node to use our group
  114.         #Aquí le decimos al nodo que use nuestro grupo
  115.         test_node.node_tree = bpy.data.node_groups[my_group.name]
  116.        
  117.        
  118.         #This will link the Custom Group Node and the output Node together
  119.         #Esto vinculará el nodo del grupo personalizado y el nodo de salida juntos
  120.         material_test.node_tree.links.new(test_node.outputs[0], material_output.inputs[0])
  121.        
  122.        
  123.         #This will add the Material Shader to the current selected object
  124.         #Esto agregará el sombreador de material al objeto seleccionado actual
  125.         bpy.context.object.active_material = material_test
  126.  
  127.        
  128.        
  129.         return {'FINISHED'}
  130.            
  131.    
  132.    
  133.    
  134.    
  135. def register():
  136.     bpy.utils.register_class(NODE_PT_MAINPANEL)
  137.     bpy.utils.register_class(NODE_OT_TEST)
  138.    
  139.    
  140.    
  141.  
  142.  
  143. def unregister():
  144.     bpy.utils.unregister_class(NODE_PT_MAINPANEL)
  145.     bpy.utils.unregister_class(NODE_OT_TEST)
  146.  
  147.  
  148. if __name__ == "__main__":
  149.     register()
Advertisement
Add Comment
Please, Sign In to add comment