1
$\begingroup$

I have some code that generates a blender texture for me:

texture

and can now access it through:

bpy.data.textures[0]

I would like to use this color ramp as a shader node whereby the color ramp node has the same levels/colors as my texture.

Is there a simple way to do this?

enter image description here

$\endgroup$
8
  • $\begingroup$ The texture system has pretty much been abandoned by the devs for a few years now. It doesn't (AFAIK) interact with the shader nodes. $\endgroup$
    – Gorgious
    Jan 16 at 18:51
  • $\begingroup$ dang. I'm using template_color_ramp in some add-on I am creating, and it requires the ramp to be stored as a texture to use the UI function. I eventually want to create a shader node tree with a matching coloramp node. Thought there would be an elegant way to reuse the texture. $\endgroup$
    – Derek Eden
    Jan 16 at 19:27
  • $\begingroup$ Ok so bear with me. You can create a material and make it not appear in the material list if its name begins with .. Then, inside this material node tree, create a color ramp node. Then you can reference this color ramp node in the template inside your addon. Does that make sense ? docs oh and you can directly edit the material color ramp in your example too $\endgroup$
    – Gorgious
    Jan 16 at 20:04
  • $\begingroup$ I think I understand and just tested something in my add-on. It looks like to do this I would have to create this dummy material before I even create the UI element, which doesn't really fit with my workflow. I need to create the material after the fact. And it seems like the color_ramp attributes are read only so I cant create a material and then reset the color_ramp attribute to the one that's in my add-on element. Maybe I'm just confusing myself.. $\endgroup$
    – Derek Eden
    Jan 16 at 20:24
  • $\begingroup$ My workaround is to have a unique ID for the texture name, and upon material creation, also have this unique ID as the name. And in my custom node in my add-on, my update function for this particular node will check for this matching material and sync the colorramps dynamically $\endgroup$
    – Derek Eden
    Jan 16 at 21:23

1 Answer 1

1
$\begingroup$

Nodes

If you have a regular image texture, you can recreate the ramp node with other nodes.

So we have an image with a gradient along it's length and we need to remap it's length, so X coordinate, to the intensity of some other image. It's possible to do that with nodes:

enter image description here

I needed to reverse direction with the invert node.

Color Ramp node with Python

If the color gradient is generated by script, why not output the colors to the Color Ramp node instead of an image? bpy.data.materials["Material"].node_tree.nodes["ColorRamp"].color_ramp.elements[0].color That's the first color of my test Color Ramp node that I have in "Material". Apparently it's easy to add new elements to the ramp as well: bpy.data.materials["Material"].node_tree.nodes["ColorRamp"].color_ramp.elements.new(0.5)

Use autocomplete function(Tab) in the Python console to easily find how things work in the Python API: enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you, while helpful, this doesn't really answer my question or help me. I am trying to find the best way to "sync" an existing internal blender texture color ramp to a shader color ramp node. Your solution would work if I could replace the image texture In your node setup with something representing the blender texture. $\endgroup$
    – Derek Eden
    Jan 18 at 23:07
  • $\begingroup$ I understood that you generate the ramp with some code. Can you share the code? $\endgroup$ Jan 19 at 6:16

You must log in to answer this question.

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