algorithmic modeling for Rhino


I am trying to learn Python and write my first script with GhPython.

I was able to get the script to save the Rhino Doc to and stl file base on path and file inputs, which was a big step for me ; )

Now I am trying to make the script bake the input geometry to the Rhino Doc.

Similar to the Lunchbox Box Bake Component, but written in Python so that I can integrate with the rest of my script.

How difficult of a task is it?

I looked through an old bake component written in C# (not sure who authored), and it looks like the script has to check case by case, what type of geometry it is.

How would this be implemented with Python?


Views: 9367

Replies to This Discussion

Hi Gui,

Not very difficult. I think you are referring to Giulio Piacentino's example. IMO that is a good reference to base your script on.

In short use something like

scriptcontext = Rhino.RhinoDoc.ActiveDoc

then get the proper layertable and object tables e.g. scriptcontext.Layers.AddLayer(......

Its probably best that you know what you kind of object you are trying to bake

To typecheck:

p = rg.Point3d(0,0,0)
print isinstance(p, rg.Point3d)   # returns True

though i think the more pythonic way to do it could be:

p = rg.Point3d(0,0,0)
print hasattr(p,"SortAndCullPointList")

where you check for a particular attribute that is definitive for the class (duck-typing)

hope this helps!

Hi Gui,


that script was written a long time ago, even originally before RhinoCommon, and now there's quite more freedom when choosing how to add objects to the document.

Let's first of all have a look at the various available ways to reference geometry and data in ghPython:


ghdoc is the standard document for rhinoscriptsyntax. We can use it all the time and it is meant to be as quick as possible, in order to slow down rhinosciptsyntax in Grasshopper as little as possible.

It is also possible to just reference or create geometry in memory using RhinoCommon methods.

The third and last document is the usual Rhino document, which is also accessible in ghPython: Rhino.RhinoDoc.ActiveDoc.


All these three styles are valid and useful for certain purposes and code writing styles.


Specifically, you are asking to take data from ghdoc, obtain the geometry, and finally add it to the Rhino document. This is essentially baking in ghPython, right?

I am attaching an example.


When you have a document available and a Guid, you can use rhinoscriptsyntax.coerceXXX(guid) in order to obtain geometry, or specific types of geometry.

Similarly, scriptcontext.doc.Objects.AddXXX() allows to add geometry to the document. There are lots of variants for this. You can refer to the RhinoCommon documentation for all methods in the ObjectTable class, or all methods in the ghPython one are on GitHub.

Please let me know if you need more information. I hope this is helpful,


Giulio Piacentino
for Robert McNeel & Associates, 5 KB

Wow. Thank you both for your comprehensive replies.


Trying to learn python in parallel with rhinoscript + rc methods from scratch can be a bit overwhelming, but there seems to be a lot of documentation and resources available.


You guys definitely gave me enough to get started.


Hi, why 

scriptcontext.doc = Rhino.RhinoDoc.ActiveDoc


scriptcontext.doc = ghdoc

if easier

rc.RhinoDoc.ActiveDoc.Objects.AddCurve(geometry, attributes)


Because then all rhinoscript functions will we redirected to the Rhino document, not only RhinoCommon calls.

Does it help?


Giulio Piacentino
for Robert McNeel & Associates

Thanks Giulio, this helped

Easier to understand file!

Attachments: fails thus:

<type 'NoneType'>
Runtime error (MissingMemberException): 'NoneType' object has no attribute 'Attributes'
  line 15, in script

LINE 15:     attributes = doc_object.Attributes

What are you trying to bake?

Giulio Piacentino
for Robert McNeel & Associates

Hi Giulio;

I downloaded the script and I am getting an error. 

I might be missing something.

The error is:

<type 'NoneType'>
Runtime error (MissingMemberException): 'NoneType' object has no attribute 'Attributes'

The coerce doesn't seem to be giving any attributes.

Has the syntax changed in the latest release, or did I install int incorrectly?

Thank for your awesome work!!!

Hey Nicolas,

hope you managed to fix it! for future reference, i'll post my findings here(correct me if i'm wrong somewhere):

the error happened because scriptcontext was not set to ghdoc (default) when you ran the script.

One possibility is that you ran another python script somewhere that switched scriptcontext to Rhino.RhinoDoc.ActiveDoc and forgot to switch it back, so when you then ran Giulio's script, it tried to call rs.coercerhinoobject in rhino environment, and not the grasshopper environment. Hence it got nothing from line_id because geometries in line_id only exist in ghdoc space.

To fix it, add this line:

scriptcontext.doc = ghdoc

before the line:

doc_object = rs.coercerhinoobject(line_id)

I now have a deeper appreciation for all the youtube videos that warned to "switch scriptcontext back to ghdoc once you're done fiddling with it". :)

p.s. that particular python script also works with item access, not list access, so make sure it is set that way.

p.s.s thanks Giulio for the beautiful geometry reference chart above!

p.s.s.t watch the video link by Ethan below, it cleared up a lot of things for me.

Giulio, what would the syntax to use if I wanted to bake several objects, which I have for exampled moved in my definition, retaining their original layers?

I guess I should try to get the layers from the original guid and then apply the same to the objects I have moved but I can't find a way to do both operations.

See example attached

Thanks in advance







  • Add Photos
  • View All


  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service