0
$\begingroup$

I am scripting an add-on and I want to make a button for creating sky shader. I type this, I want the nodes to work in WORLD space of shader editor. When pressing the button in UI, the add-on create a simple gradient sky for you

def draw(main, context):
    bpy.ops.preferences.addon_enable(module= "node_wrangler")

class Shader_Gradient_Sky(bpy.types.Operator):
    """Add a Gradient Sky Shader."""
    bl_label = "gradient sky"
    bl_idname = 'shader.gradient_sky'
    
    col_0 = bpy.props.FloatVectorProperty(name= "Color 0", subtype= 'COLOR_GAMMA' , size=4, default= (0.314, 0.477, 0.777, 1))
    col_1 = bpy.props.FloatVectorProperty(name= "Color 1", subtype= 'COLOR_GAMMA' , size=4, default= (0.012, 0.142, 1, 1))

    
    def draw(self, context):
        layout= self.layout
        layout.separator(factor= 0.1)
        box = layout.box()
        box.prop(self, "col_0", slider= True)
        box.prop(self, "col_1", slider= True)

        layout.separator(factor= 1)
    
    @classmethod
    def poll(cls, context):
        space = context.space_data
        return space.type == "NODE_EDITOR" and space.shader_type == "WORLD" and space.tree_type == "ShaderNodeTree"
    
    
    def execute(self, context):
        space = context.space_data
        node_tree = space.node_tree
        node_active = context.active_node
        node_selected = context.selected_nodes
        
      
        material_gradient_sky = bpy.data.materials.new(name= "Gradient Sky")
            #Use Nodes for this Material
        material_gradient_sky.use_nodes = True
        material_gradient_sky.node_tree.nodes.remove(material_gradient_sky.node_tree.nodes.get('Principled BSDF'))
            #Get material output
        
        world_output = material_gradient_sky.node_tree.nodes.get('Output World')
            
        world_output.location = (600, 0)
            #Deselect the node
        world_output.select = False
        
        viewer_node = material_ gradient_sky.node_tree.nodes.new('ShaderNodeEmissionViewer')
        viewer_node.location = (-200,100)
        viewer_node.select = False

        ColorRamp_node = material_gradient_sky.node_tree.nodes.new('ShaderNodeValToRGB')
        ColorRamp_node.location = (-300, 200)
        ColorRamp_node.color_ramp.elements[0].position = 0.000
        ColorRamp_node.color_ramp.elements[1].position = 0.837
        ColorRamp_node.color_ramp.elements[0].color = self.col_0
        ColorRamp_node.color_ramp.elements[1].color = self.col_1
        ColorRamp_node.select = False
        
        
        GradientTex_node = material_gradient_sky.node_tree.nodes.new('ShaderNodeTexGradient')
        GradientTex_node.location = (-300, 200)
        ColorRamp_node.input[0].gradient_type = "LINEAR"
        ColorRamp_node.select = False
        
        mapping_node = material_ gradient_sky.node_tree.nodes.new('ShaderNodeMapping')
        mapping_node.location = (-200,100)
        mapping_node.select = False
             
             
        TexCoord_node = material_ gradient_sky.node_tree.nodes.new('ShaderNodeTexCoord')
        TexCoord_node.location = (-250,-300)
        TexCoord_node.select = False
             
             
        world_output = material_gradient_sky.node_tree.nodes.get('Output World')
        world_output.location = (200,0)
        world_output.select = False

        
        
        world_gradient_sky.node_tree.links.new(viwer_node.outputs[0], world_output.inputs[0])
        

            
            
        bpy.context.object.active_material = material_gradient_sky
        
        main(context)
        return {'FINISHED'}
    

    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self, width= 230)

This script is still writing and not finished, so you can see I did not create links between nodes. But after I link one node and run the script. Got this in ui enter image description here

What's missing in my script?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.