2
$\begingroup$

How can I create a ColorRamp for a Cycles material through Python?

I am able to create a new shader output node with nodes.new(type='ShaderNodeOutputMaterial'). Searching online, I see that it is only possible through C code (however, these pages dates back several years).

Is there a way to fake a ColorRamp (maybe with multiple Math nodes in greater/less than mode) or create a ColorRamp for a Cycles material through Python?

$\endgroup$

1 Answer 1

4
$\begingroup$

The node type you need for creating a color ramp is ShaderNodeValToRGB. Below is an example that demonstrates how to create a material that contains a color ramp that is connected to the base color of a Principled BSDF.

import bpy


# Create a material that uses nodes
material = bpy.data.materials.new(name="Example_Material")
material.use_nodes = True

# Get the collection of nodes in the material
nodes = material.node_tree.nodes

# Create a color ramp node
color_ramp = material.node_tree.nodes.new("ShaderNodeValToRGB")

# Example for connecting the color output of the color ramp to the Principled BSDF
principled_bsdf = nodes.get("Principled BSDF")
material.node_tree.links.new(principled_bsdf.inputs["Base Color"], color_ramp.outputs["Color"])
$\endgroup$

You must log in to answer this question.

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