Using Chat GTP to write Python scripts for Grasshopper

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()

Maybe ask ChatGPT to debug the script as well? :roll_eyes:

5 Likes

Haha, yes.
I tested the code and asked for a revised version many times. It apologized and tried different approaches for the initial bit, but to no success. This was the last one before I gave up. Previous variations tried with, for example:


import rhinoscriptsyntax as rs
import Rhino

# Get all the components in the current file
components = rs.GetComponents()

and:

import Rhino

# Get all the components in the current file
components = Rhino.RhinoDoc.ActiveDoc.GetComponents()

and:


import Rhino

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

# Get all the components in the current file
components = [o for o in objects if o.IsInstanceDefinitionObject]


Hi @isak.wadso

the script from ChatGPT looks for all blocks in the currently open rhino file (not grasshopper canvas) and tries to re-color them in a wrong way, so my takeaway from this is it did not understand your request that well.

Instead of using the rhino library (import Rhino) you rather need to use the grasshopper library (import Grasshopper). I can not help you write that code as I’m not too familiar with the grasshopper API, but the grasshopper docs would be a good way to look and start to get into that topic.

Generally ChatGPT is quite limited when it comes to obscure or rare programming tasks and as there are not many grasshopper developers, this definitely qualifies

Hi Isak,

I’d be very interested to see a transcript of the request you made of Chat GTP.

Regards
Jeremy

It’s certainly helpful with understanding how to get started and i’ve found it useful for regex expressions, but swings and misses are more likely than homeruns. The next version looks promising (trained on 100x more data).

1 Like

Ah, thank for the explanation. Maybe better to use it for problems related to the big and well known programming environments, and not have to high hopes for Grasshopper related stuff.

I will give you the full transcript tomorrow when I get home.

Thank you.

Here is the conversation:

Thanks Isak.

I solved the problem at hand by the way.
objects = GrasshopperDocument.Objects; was enough to supply the metahopper “Highlight objects”


Display draw order.gh (6.4 KB)