Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- class NODE_PT_MAINPANEL(bpy.types.Panel):
- bl_label = "Custom Node Group"
- bl_idname = "NODE_PT_MAINPANEL"
- bl_space_type = 'NODE_EDITOR'
- bl_region_type = 'UI'
- bl_category = 'New Tab'
- def draw(self, context):
- layout = self.layout
- row = layout.row()
- row.operator('node.test_operator')
- def create_test_group(context, operator, group_name):
- #enable use nodes
- bpy.context.scene.use_nodes = True
- test_group = bpy.data.node_groups.new(group_name, 'ShaderNodeTree')
- group_in = test_group.nodes.new('NodeGroupInput')
- group_in.location = (-300,0)
- test_group.inputs.new('NodeSocketFloat','Factor Value') #0
- test_group.inputs.new('NodeSocketColor','Color Input') #1
- group_out = test_group.nodes.new('NodeGroupOutput')
- group_out.location = (400,0)
- test_group.outputs.new('NodeSocketColor','Output')
- node_add = test_group.nodes.new('ShaderNodeMath')
- node_add.operation = 'ADD'
- node_add.location = (100,0)
- node_greater = test_group.nodes.new('ShaderNodeMath')
- node_greater.operation = 'GREATER_THAN'
- node_greater.label = 'greater'
- node_greater.location = (-100,100)
- node_less = test_group.nodes.new('ShaderNodeMath')
- node_less.operation = 'LESS_THAN'
- node_less.label = 'less'
- node_less.location = (-100,-100)
- # enlazar los nodos juntos
- test_group.links.new(node_add.inputs[0], node_greater.outputs[0])
- test_group.links.new(node_add.inputs[1], node_less.outputs[0])
- # vincular las entradas
- test_group.links.new(node_add.outputs[0], group_out.inputs[0])
- # vincular la salida
- test_group.links.new(node_greater.inputs[0], group_in.outputs[0])
- test_group.links.new(group_in.outputs[0], node_less.inputs[0])
- return test_group
- class NODE_OT_TEST(bpy.types.Operator):
- bl_label = "Add Custom Node Group"
- bl_idname = 'node.test_operator'
- def execute(self, context):
- #First we must create a material and give it a name. I call it, Test
- #Primero debemos crear un material y darle un nombre. Lo llamo, prueba
- material_test = bpy.data.materials.new(name= "Test")
- #Next Enable the Nodes
- #Siguiente Habilitar los nodos
- material_test.use_nodes = True
- #Then remove the Principled Node: in this instance we do not need it
- #Luego elimine el Nodo Principled: en este caso no lo necesitamos
- material_test.node_tree.nodes.remove(material_test.node_tree.nodes.get('Principled BSDF'))
- #create a reference to the Output node (we need this later)
- #crear una referencia al nodo Salida (necesitamos esto más adelante)
- material_output = material_test.node_tree.nodes.get('Material Output')
- #set the location for the output
- #establecer la ubicación para la salida
- material_output.location = (400,0)
- #This will give the Custom Group a Name, I call it Test Node
- #Esto le dará un nombre al grupo personalizado, lo llamo Test Node
- custom_node_name = "Test Node"
- #Here we tell the Test Node to use the Group we created
- #Aquí le decimos al nodo de prueba que use el grupo que creamos
- my_group = create_test_group(self, context, custom_node_name)
- #This will add a blank Custom Node to the Material
- #Esto agregará un nodo personalizado en blanco al material
- test_node = material_test.node_tree.nodes.new('ShaderNodeGroup')
- #Here we tell the node to use our group
- #Aquí le decimos al nodo que use nuestro grupo
- test_node.node_tree = bpy.data.node_groups[my_group.name]
- #This will link the Custom Group Node and the output Node together
- #Esto vinculará el nodo del grupo personalizado y el nodo de salida juntos
- material_test.node_tree.links.new(test_node.outputs[0], material_output.inputs[0])
- #This will add the Material Shader to the current selected object
- #Esto agregará el sombreador de material al objeto seleccionado actual
- bpy.context.object.active_material = material_test
- return {'FINISHED'}
- def register():
- bpy.utils.register_class(NODE_PT_MAINPANEL)
- bpy.utils.register_class(NODE_OT_TEST)
- def unregister():
- bpy.utils.unregister_class(NODE_PT_MAINPANEL)
- bpy.utils.unregister_class(NODE_OT_TEST)
- if __name__ == "__main__":
- register()
Advertisement
Add Comment
Please, Sign In to add comment