Curve Tool Generator

In my second experiment, I wanted to work with curves and save old coordinates so that I could reuse them for similar curve shapes.

Curve AI Node

 

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

from openai import OpenAI
import hou
node = hou.pwd()
client = OpenAI()
def get_completion(user_prompt, curve_message):
    """ Calls OpenAI API and returns structured curve coordinates. """
    try:
        messages = [
            {"role": "system", "content": curve_message},
            {"role": "user", "content": user_prompt}
        ]
        response = client.chat.completions.create(
            model="gpt-4-turbo-preview",
            messages=messages,
            temperature=1.0,
            max_tokens=500
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        return str(e)
# Get user input
user_prompt = node.parm('prompt').eval()
# Retrieve curve message from curve_message_node
curve_message_node = hou.node('/obj/geo1/curve_message_node')
curve_message = curve_message_node.parm('curve_message').eval() if curve_message_node else ""
# Retrieve previous response from previous_response_node
previous_response_node = hou.node('/obj/geo1/previous_response_node')
prev_response = previous_response_node.parm('prev_response').eval() if previous_response_node else ""
# Modify curve message to include previous response if it exists
if prev_response:
    curve_message = f"""{curve_message}\n
                        If there was a previous response, take it into account for the new response.
                        Previous Response:\n
                        {prev_response}
                        Adjust all necessary parameters based on the new prompt.
                        INCLUDE ALL PARAMETERS FROM PREVIOUS RESPONSES IN THE NEW RESPONSE.
                        DO NOT INCLUDE COMMENTS IN OUTPUT."""
# Fetch AI-generated curve coordinates
response = get_completion(user_prompt, curve_message)
# **Save the latest AI response into previous_response_node**
if previous_response_node:
    previous_response_node.parm('prev_response').set(response)
# Apply generated coordinates to the curve
c_node = hou.node('/obj/geo1/curve1')
if c_node:

    c_node.parm('coords').set(response)

 Also, create a Prompt parameter inside the python node to connect it to the code and add this inside "`chs("../curve1/prompt")`" the string for connecting it to the Curve 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 and Cook parameters.




3. Create a null node named "curve_message_node" 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 specialize in spatial awareness and procedural 3D curve generation.  
Interpret the prompt and generate a structured list of 3D coordinates defining a continuous space curve.  
If the prompt lacks details, intelligently infer the most natural curve layout while ensuring smooth transitions and logical point connections.  

VERY IMPORTANT - ONLY OUTPUT THE COORDINATE VALUES IN THIS EXACT FORMAT:  
X1,Y1,Z1 X2,Y2,Z2 X3,Y3,Z3 ...  

DO NOT INCLUDE COMMENTS, EXPLANATIONS, OR ANY EXTRA TEXT.  
ENSURE A CONSISTENT FLOW BETWEEN POINTS WITHOUT SHARP OR UNNATURAL BREAKS.  

4. Create a null node named "previous_response_node" and edit the parameter by clicking the gear icon at the top and selecting "Edit Parameter Interface" to create a string inside the node. This node will store previous coordinates or responses for better curve generation if the user wants to focus on one type of curve.



5. Then create a color node named "curve1" and change the curve asset and path from "Curve::2.0" to "curve." Now, add the Prompt parameter below in the interface. If you want close curved, tick close in "Curve Properties."




Callback Script: hou.node('/obj/geo1/curve_node_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

Color Ramp Generator

Researching topics for My Thesis (Master's Edition)