1
$\begingroup$

I try to build a operator for some specific shaders by using Python but got stuck on how to make a node connection between ColorRamp (input) and Vector Math (output):

enter image description here

When I try to use shadername.links.new(VectorMath.outputs[0], ColorRamp.inputs[0]) both nodes are disconnected and I am not sure why:

enter image description here

Some coding excerpt:

 #colorRamp
    ramp = mat_ruby.node_tree.nodes.new("ShaderNodeValToRGB")
    ramp.location = (-800,100)

    ramp.color_ramp.elements[0].position=0.102
    ramp.color_ramp.elements[0].color = (0.215861,0,0.017642,1)
  
    ramp.color_ramp.elements.new(0.636)
    ramp.color_ramp.elements[1].color = (0.396755,0,0.032,1)

    ramp.color_ramp.elements.new (0.794)
    ramp.color_ramp.elements[2].color = (0.701102,0.147,0.016,1)
    
    ramp.color_ramp.elements.new(0.893)
    ramp.color_ramp.elements[3].color = (1.0,0.292,0.0,1)

    ramp.color_ramp.elements[4].position = 1
    ramp.color_ramp.elements[4].color = (1.0,1.0,1.0,1)
   
    mat_ruby.node_tree.links.new(ramp.outputs[0], glassA.inputs[0])
    

  
    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])

Q: How can I link the output of a Vector Math node to the input of eg. a Color Ramp?

$\endgroup$
1
  • $\begingroup$ Hello, I've answered your question and taken the liberty to modify the title to make it a bit more clear, feel free to revert if it doesn't feel like what you intended :) $\endgroup$
    – Gorgious
    Dec 14, 2020 at 9:33

1 Answer 1

2
$\begingroup$

The Vector Math node actually has 2 outputs : One of type Vector, and one of type float. They get hidden / shown depending on the node's operation.

The float type is accessed with the second index of the outputs. Change your last line to :

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

Or (not advised since it is language-dependant)

mat_ruby.node_tree.links.new(math.outputs["Value"], ramp.inputs[0])
$\endgroup$
2
  • $\begingroup$ That raises an interesting question.. I wonder.. can you use locale-translated strings to look up into this kind of dictionary? $\endgroup$
    – Robin Betts
    Dec 14, 2020 at 9:41
  • 1
    $\begingroup$ @RobinBetts Yup that would be handy ! I never really dived into that and it's quite hard to find related docs given the keywords locale and translation :) I think it would depend if the string is translated at runtime or at compile time ? Anyway this would still be unadvisable since one could work with a material created by another person with a different locale language... $\endgroup$
    – Gorgious
    Dec 14, 2020 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .