How do I debug a GhPython script?

I have been using GhPython for a while now. And one thing that really slows me down is not being able to see the geometry I am building. When I get an error, I can’t just select the variable to see its content (like selecting a component in Grasshopper and having it show up in green). Currently, I have to find the source of the problem by doing a=mylist (to make it an output) and then “”" all the lines after (so that python terminates before the error). Is there a better way to view the geometry generated in GhPython?

How do people write python scripts for rhino outside of grasshopper?

They use the EditPythonScript editor and debugger, which comes with Rhino for Windows.

No I have not used editpythonscript. I primarily used GhPython for specific tasks that are too complicated to do in grasshopper. Does editpythonscript work with GhPython?

No, but you can certainly use it to write and debug (probably) the bulk of your code before moving it into Grasshopper.

If I were to do it the way you described, I would need to bake the inputs from grasshopper and then input them into the python editor. How do you do this? With GhPython, your variables (not sure if this is the right terminology) have the same name as your input (default x and y). How do I input stuff into editpythonscript? Secondly, how do I retain the complex grasshopper data tree?

Do you guys have a tutorial video?

Does anyone else have a solution that works with GhPython in Grasshopper?

There is no direct debugger support in GHPython right now. This is a feature we are hoping to implement in the future.

For the time being, you can add some level of “tracing” support by using the print statement. All of the print statements are directed to the ‘out’ output.

editpythonscript” that Dale mentions is the editor that appears when you call the Rhino _EditPythonScript command. Regarding the tutorial, there is the RhinoScript for Python 101.

We definitely want to add debugging support to GhPython. Thanks for reminding us.

One thing that might come handy in your case just came to mind. You could assign the entire ghdoc to an output variable. Because the document is enumerable, its entire content will be streamed out of it, so you will not need to pick a specific variable. Does that help, at least a little bit?

Example: show-all.gh (2.6 KB)

2 Likes

Thanks for the suggestions. I looked at the script you posted and I have a few questions. What is the difference between rhinoscritptsyntax and rhinocommon? What is scriptcontext? I tend to avoid rhinoscriptsyntax because I’ve had a lot of frustration with it before (always something to do with guid- whatever that is). In you example you made lines, but where do those lines live, how do you reference them later (maybe you want to divide them later)? Wouldn’t you do myline=rs.AddLine((1,2,3),(4,5,6)) ?

They live in the document. I did not need to address them if the following code, so I just added them to the document. You can get the ID (Guid) of each of them by assigning it to a variable, as you mention. My scriptcontext.doc suggestion is useful mainly for rhinoscriptsyntax.

You can seek answers to more general questions by clicking the center of an empty Python component and choosing “Help…”, as well as reading the 101 manual mentioned above.

Thanks,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

1 Like

This is a great question, the short answer is definitely print().

I’d love to see a debugging mode similar to the object viewer accessible through the Rhino Python script editor.

Another tip for the GHPython component: I almost always make a few empty “output lists” in the outer scope of my script at the very top right below the imports for debugging purposes. Typically I name these by the default output parameters names given when using the ZUI to add more outputs (a,b,c etc.). The idea is that you can then append/extend these lists with geometry from within a loop, function or class without having to return/assign. Obviously this is not considered good coding practice and these lists should be deleted once you move on, but for debugging and “figuring out what the heck is going on” it is really rather useful. Hope that helps…

1 Like

@angel wrote a post a while back. I think that this is exactly what you are after.