0
$\begingroup$

Hi I am struggling to get rid of InfroError caused by ColorRamp. Is there any concise way to represent the colorRamp shown below?

enter image description here

My messy code

enter image description here

The Error Message

enter image description here

import bpy

class Shader2MainPanel(bpy.types.Panel):

bl_label = "2ndShaderLibrary"
bl_idname = "ShaderMainPanel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "ShaderLibraryV2"
def draw(self,context):
    layout = self.layout
    row = layout.row()
    row.operator("shader.ruby_operator")

class RUBY_SHADER(bpy.types.Operator):

bl_label = "RUBY"
bl_idname = "shader.ruby_operator"


def execute(self,context):
    
   
    activeObject = bpy.context.active_object
      
    mat_ruby = bpy.data.materials.new(name="Ruby")

    mat_ruby.use_nodes = True

    activeObject.data.materials.append(mat_ruby)

    #delete the default shader   
    mat_ruby.node_tree.nodes.remove(mat_ruby.node_tree.nodes.get('Principled BSDF'))

    # material output
    mat_output = mat_ruby.node_tree.nodes.get("Material Output")

    mat_output.location = (-400,0)

    #mix_shader
    mixSDR = mat_ruby.node_tree.nodes.new("ShaderNodeAddShader")
    #connect mix_shader to material output
    mat_ruby.node_tree.links.new(mixSDR.outputs[0],mat_output.inputs[0])

    #1st glass shader
    glassA= mat_ruby.node_tree.nodes.new('ShaderNodeBsdfGlass')
    glassA.location = (-600,0)
    glassA.inputs[1].default_value=0.141

    #2nd glass shader
    glassB = mat_ruby.node_tree.nodes.new('ShaderNodeBsdfGlass')
    glassB.location = (-600,200)
    glassB.inputs[0].default_value=(0.396755,0.177888,0.201556,1)
    glassB.inputs[1].default_value=0.141

    #add 1st and 2nd glass into mix_shader
    mat_ruby.node_tree.links.new(glassA.outputs[0], mixSDR.inputs[0])
    mat_ruby.node_tree.links.new(glassB.outputs[0], mixSDR.inputs[1])

    #colorRamp
    ramp = mat_ruby.node_tree.nodes.new("ShaderNodeValToRGB")
    ramp.location = (-800,100)
    for r in ramp:
            
        ramp.color_ramp.elements[0].color = (0.215861,0,0.017642,1)
        ramp.color_ramp.elements[0].position = 0.102
        ramp.color_ramp.elements[1].color = (0.396755,0,0.032,1)
        ramp.color_ramp.elements[1].position = 0.636
        ramp.color_ramp.elements[2].color = (0.701102,0.147,0.016,1)
        ramp.color_ramp.elements[2].position = 0.794
        ramp.color_ramp.elements[3].color = (1.0,0.292,0.0,1)
        ramp.color_ramp.elements[3].position = 0.893
        ramp.color_ramp.elements[4].color = (1.0,1.0,1.0,1)
        ramp.color_ramp.elements[4].position = 1
        

    
    mat_ruby.node_tree.links.new(ramp.outputs[0], glassA.inputs[0])
    

    #Vector Math ('DOT_PRODUCT')
    math = mat_ruby.node_tree.nodes.new('ShaderNodeVectorMath')
    math.location = (-1000,100)
    math.operation = 'DOT_PRODUCT'
    mat_ruby.node_tree.links.new(math.outputs[0], ramp.inputs[0])

    #Geometry
    geo = mat_ruby.node_tree.nodes.new("ShaderNodeGeometry")
    geo.location = (-1200,100)
    mat_ruby.node_tree.links.new(geo.outputs[1], math.inputs[0])
    mat_ruby.node_tree.links.new(geo.outputs[4], math.inputs[1])

    return {'FINISHED'}

def register():
    bpy.utils.register_class(Shader2MainPanel)
    bpy.utils.register_class(RUBY_SHADER)

def unregister():
    bpy.utils.register_class(Shader2MainPanel)
    bpy.utils.register_class(RUBY_SHADER)

if __name__ == "__main__":
    register()
    
$\endgroup$
2

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.