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
from openai import OpenAIimport houimport astnode = 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.contentexcept Exception as e:return str(e)# Function to fetch system message from Houdini nodedef 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 nodesystem_message = get_system_message()# Get user promptuser_prompt = node.parm('prompt').eval()# Fetch AI-generated color rampresponse = get_completion(user_prompt, system_message).split("\n")# Process AI responsevalues = ast.literal_eval(response[0])keys = ast.literal_eval(response[1])bases = [hou.rampBasis.Constant] * len(keys)# Apply the generated ramp to the color nodec_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.
Comments
Post a Comment