Color Ramp Generator

I chose one of the most effortless node-based AI approaches for my first experiment: color ramp. It's similar to a gradient bar you see in Adobe Creative Suite, which in Houdini can be used to create a gradient effect over any line or shape made in Houdini; it's easy as it's a node that can be manipulated by parameter. By connecting Open AI API, I can make it a prompt-based approach without any problem where user text base input will be interpreted via a Python code and will give results to change the color ramps. 

Using Houdini for this project gives me an advantage as it allows me to interact with the nodes already in the Houdini workspace using Python, also known as Python (Houdini + Python). 

Color Ramp AI Node



1. Create a Python node named "color_ramp_GPT," adding the below code in the content area. 

from openai import OpenAI
import hou
import ast

node = hou.pwd()
client = OpenAI()

def get_completion(user_prompt, system_message):
    try:
        messages = [
            {"role": "system", "content": system_message},
            {"role": "user", "content": user_prompt}
        ]
        response = client.chat.completions.create(
            model="gpt-4-turbo-preview",
            messages=messages,
            temperature=1.0,
            seed=node.parm('seed').eval(),
            max_tokens=500
        )
        return response.choices[0].message.content
    except Exception as e:
        return str(e)

# Function to fetch system message from Houdini node
def get_system_message():
    system_message_node = hou.node('/obj/geo1/color_message_node')
    if system_message_node:
        return system_message_node.parm('message').eval().strip()
    else:
        hou.ui.displayMessage("Error: system_message_node not found in /obj/geo1!", severity=hou.severityType.Error)
        return ""

# Get system message from the Houdini node
system_message = get_system_message()

# Get user prompt
user_prompt = node.parm('prompt').eval()

# Fetch AI-generated color ramp
response = get_completion(user_prompt, system_message).split("\n")

# Process AI response
values = ast.literal_eval(response[0])
keys = ast.literal_eval(response[1])
bases = [hou.rampBasis.Constant] * len(keys)

# Apply the generated ramp to the color node
c_node = hou.node('/obj/geo1/color1')
ramp_parm = c_node.parm('ramp')

ramp = hou.Ramp(bases, keys, values)
ramp_parm.set(ramp)

 Also, create a Prompt parameter inside the python node to connect it to the code and add this inside "`chs("../color1/prompt")`" the string for connecting it to the Color Node Prompt.







2. In the Python node, edit the parameter by clicking the gear icon at the top, then "Edit Parameter Interface," and adding the Prompt, Seed, and Cook parameters.





3. Create a null node named "color_ramp_GPT" and edit the parameter by clicking the gear icon at the top and selecting "Edit Parameter Interface" to create a string inside the node. 



Pastes the below prompt in the string:
You are an expert at generating color ramps from user prompts.
Understand the prompt and output a 5-10 ramp color and position values list.
Each RGB color value should be a tuple of 3 float values in the range of 0-1. 
Each position value should be a single float value in the range of 0-1, arranged in ascending order.
Ensure smooth blending for natural gradients.
DO NOT repeat colors unless the prompt explicitly requests it.

VERY IMPORTANT - ONLY OUTPUT THE PARAMETER VALUES IN THIS EXACT FORMAT: 
[(r,g,b),(r,g,b),(r,g,b),...]
[position1, position2, position3,...]

DO NOT OUTPUT COMMENTS ON THE PROMPT.


4. Then create a color node named "color1," change the color type to Ramp from Attribute, and name your Attribute 'Gradient.' Also, add an edit Parameter similar to the above steps.



Callback Script: hou.node('/obj/geo1/color_ramp_GPT').cook(force=True)




Note: To get the "Cook" button next to the prompt area, mark "Horizontally Join to Next Parameter" in Prompt or any node earlier than the cook node.



Thank You, Houdini School, for explaining the foundations!
If you want to learn how to use Houdini, they are one of the best places to start with your VFX career.


Comments

Popular posts from this blog

Present Best Way to Install MLOPs in Houdini

Researching topics for My Thesis (Master's Edition)