Hi everyone, I am refactoring a script I wish to use in 4.0, which I believe to be written in 2022? The script takes in an image loaded in to blender and puts it into a color ramp. Here’s an example image and the code:
import bpy
from bpy import data as D
imageFile = "Image.png"
materialName = "Material"
colorCount = 15
# Get the reference to the specified image
img = D.images[imageFile]
# Determine height and width of the image
width = img.size[0]
height = img.size[1]
# Get the reference to the specified material
material = bpy.data.materials[materialName]
# Get the node collection of the material
nodes = material.node_tree.nodes
# Create a color ramp node
color_ramp = material.node_tree.nodes.new("ShaderNodeValToRGB")
# Get a reference to the colorRamp node
cr=D.materials[materialName].node_tree.nodes["ColorRamp"].color_ramp
# Set interpolation between color stops to "CONSTANT"
cr.interpolation = "CONSTANT"
# Convert sRGB to linear RGB
def sRGB2linear(s):
if s <= 0.0404482362771082:
lin = s / 12.92
else:
lin = ((s + 0.055) / 1.055) ** 2.4
return lin
# Iterate through the colors
for x in range(colorCount):
# Define the position of the pixel we want to read out
target = [round(x * (width / colorCount) + width / colorCount/2), round(height / 2)]
# Determine the index of the pixel to be read out (#RGBA)
index = (target[1] * width + target[0]) * 4
if (x > 0 and x < colorCount -1):
# Add a new position to the ColorRamp
cr.elements.new(position = 1 / (colorCount - 1) * x)
# Read the color at the given index and set the color in the ColorRamp
cr.elements[x].color=(
sRGB2linear(img.pixels[index]), # R
sRGB2linear(img.pixels[index + 1]), #G
sRGB2linear(img.pixels[index + 2]), #B
1, #A
)
Here’s the order of operations on how this is supposed to function:
-
Dimensions of the image are determined.
-
A reference to the specified material is created and a color ramp node is created there.
-
In the loop, using x number of color values specified, the pixel for reading the color is determined.
-
Color values are transferred directly to the new positions of the ColorRamp created at the same time.
I keep getting a key error: KeyError: 'bpy_prop_collection[key]: key "ColorRamp" not found'
. Which I believe to be blender internally changing this key? It adds the color ramp into a material fine but gets stuck on this part. Is there any specific guide on how to refactor this code? Or key-renaming guide? Any guidance would be appreciated as I couldn’t really find much googling around.