Export python script from Grasshopper

Hi,

I have a question on exporting a model.

For example, I have made a parametric model using Grasshopper. Would it be possible to export all dimensions, features …etc. of the model as a python script format?

Do you mean, you made a grasshopper definition, using various grasshopper components, and you want to have some mechanism that converts that definition into a python script?
That isn’t (automatically) possible…If I am understanding your question correctly.
Of course, you can replicate/script almost any functionality you want, but there isn’t an automated conversion mechanism.

Hi Chris thank you for your reply.

I should have told that I am totally beginner of G.H

I think you understood correctly. As you said, I have made a model in Grasshopper (using various grasshopper components & dimensions) and what I asked was that would it be possible to export these components and dimensions as a python script format.

I thought it would be possible if I use Python component in G.H. Isn’t it what Python component do?

You can’t “export” the code behind native components. (I will leave the topic/ethics of decompiling out of the conversation for now).
But, the ghpython component is a powerful customizeable component. I wouldn’t advocate for replicating code/functionality in a ghpython component, that can already be accomplished with a native component…but…I could see where it might be handy.
by way of a simple example:
The native Circle component in GH takes inputs of a base plane and a radius. It outputs a circle.

The ghpython equivalent of that would be something like this: (code inside a ghpython component)

import Rhino

plane = Rhino.Geometry.Plane.WorldXY
radius = 1.0
a = Rhino.Geometry.Circle(plane, radius)

or, even simpler, you could name the inputs of your ghpython component “plane” and “radius”, and modify them as desired.

I do think that, as a code learning exercise, you could practice trying to replicate a couple of components.
There are lots of examples on this forum.
There are also some good learning materials here:

Thank you for your info. I think I do not have a enough knowledge to understand what you said…but definitely I will have a look. Very appreciate.

two more questions, so I can save the Grasshopper file as .gh format or .ghx format.

  1. Which of them include CAD model information? (dimension, features…etc)
  2. Would it be possible to see the script of .gh or .ghx file (without opening GH canvas or Rhino)

a grasshopper file, (gh or ghx), contains information about the component/s that are on the canvas in that definition, but it won’t reveal any information about the internal/behind the scenes code of the how the component works.
You can open a ghx file in any text editor, (it’s an xml file), to see for yourself.

1 Like

Hello,

Are you trying to export the full parametric definition or trying to export one specific geometry with dimensions, etc?

Hi Graham,

That’s very good question! We (our team) have not decided yet but we are thinking both cases

  1. exporting all parameters
  2. exporting some parameters we need for parametric optimisation.

@jrlee8290,
I think you misunderstood. Graham is asking if you expect to export the complete .GH file as a single python script, or some geometry created in Rhino by a single component inside Grasshopper.

You’re talking about “parameters” what parameters?

Another option could be to try the Rhino3dm package inside CPython …

2 Likes