Last active
November 3, 2022 12:36
-
-
Save RobertHue/89b90d163da4bfceb5566f7b77485dbd to your computer and use it in GitHub Desktop.
This is a simple blender python script adds a button inside the UI of the ShadingEditor. The functionality of the button will add a simple node group defined by create_test_group.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
class NODE_PT_MAINPANEL(bpy.types.Panel): | |
"""Creates a Panel in the Object properties window""" | |
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(operator, context, group_name): | |
context.object.active_material.use_nodes = True | |
# create a new group | |
test_group = bpy.data.node_groups.new(group_name, "ShaderNodeTree") | |
# setup nodes inside the group | |
group_in = test_group.nodes.new("NodeGroupInput") | |
group_in.location = (-200,0) | |
test_group.inputs.new("NodeSocketFloatFactor", "Factor WaveMix") | |
test_group.inputs.new("NodeSocketFloatFactor", "Factor Noise") | |
test_group.inputs[0].min_value = 0.0 | |
test_group.inputs[0].max_value = 1.0 | |
test_group.inputs[0].default_value = 0.5 | |
group_out = test_group.nodes.new("NodeGroupOutput") | |
group_out.location = (400,0) | |
test_group.outputs.new("NodeSocketColor", "Output") | |
texNoise_node = test_group.nodes.new(type="ShaderNodeTexNoise") | |
texNoise_node.location = (0,0) | |
texNoise_node.inputs[2].default_value = 10 | |
texWave_node = test_group.nodes.new(type="ShaderNodeTexWave") | |
texWave_node.location = (0,-250) | |
mix_node = test_group.nodes.new(type="ShaderNodeMixRGB") | |
mix_node.location = (200,0) | |
mix_node.use_clamp = True | |
mix_node.blend_type = 'MIX' | |
# setup connections between the nodes | |
connect = test_group.links.new | |
connect(group_in.outputs[0], mix_node.inputs[0]) | |
connect(group_in.outputs[1], texNoise_node.inputs[2]) | |
connect(texNoise_node.outputs[0], mix_node.inputs[1]) | |
connect(texWave_node.outputs[0], mix_node.inputs[2]) | |
connect(mix_node.outputs[0], group_out.inputs[0]) | |
return test_group | |
class NODE_OT_TEST(bpy.types.Operator): | |
bl_idname = "node.test_operator" | |
bl_label = "Add Custom Node Group" | |
@classmethod | |
def poll(cls, context): | |
space = context.space_data | |
return space.type == "NODE_EDITOR" | |
def execute(self, context): | |
custom_node_name = "Test Node" | |
my_group = create_test_group(self, context, custom_node_name) | |
node_tree = context.object.active_material.node_tree | |
new_node = node_tree.nodes.new("ShaderNodeGroup") | |
new_node.node_tree = bpy.data.node_groups[my_group.name] | |
new_node.use_custom_color = True | |
new_node.color = (0.5, 0.4, 0.3) | |
return {"FINISHED"} | |
def register(): | |
bpy.utils.register_class(NODE_OT_TEST) | |
bpy.utils.register_class(NODE_PT_MAINPANEL) | |
def unregister(): | |
bpy.utils.unregister_class(NODE_OT_TEST) | |
bpy.utils.unregister_class(NODE_PT_MAINPANEL) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment