Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5938

Using Chat GTP to write Python scripts for Grasshopper

$
0
0

Hi!
To help me troubleshoot the calculation order for a big defenition I have, I would like to visualize the draw order of the components on the canvas. This requires some scripting in which I am not capable of. So, I asked the Chat GPT AI (ChatGPT: Optimizing Language Models for Dialogue) to write me a Pyhon example. The answer looks quite promising, but there is something wrong, because the list it creates is empty even though I have many components on the canvas. Maybe you can help me find out where it goes wrong?

I asked for a script that highlights the components placed on the current canvas based on their draw order, on a gradient from red to blue.
I got this answer:


import Rhino

# Get all the objects in the current file
objects = Rhino.RhinoDoc.ActiveDoc.Objects.GetObjectList(Rhino.DocObjects.ObjectType.InstanceReference)

# Sort the components by draw order
components = sorted(objects, key=lambda x: x.Attributes.DrawOrder)

# Get the number of components
num_components = len(components)

# Define the color ramp from red to blue
color_ramp = [Rhino.Display.ColorHSV(0, 1, 1), Rhino.Display.ColorHSV(240, 1, 1)]

# Highlight each component with a color from the color ramp
for i, component in enumerate(components):
    color = Rhino.Display.ColorRamp.Evaluate(i/num_components, color_ramp)
    component.Attributes.ObjectColor = color
    Rhino.RhinoDoc.ActiveDoc.Views.Redraw()

6 posts - 5 participants

Read full topic


Viewing all articles
Browse latest Browse all 5938

Trending Articles