Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to change the Active Document in Python for Grasshopper

In this blog post, I’m going to explain how to change the Active Document in your Grasshopper Python component. I’m using Python, as I’m a big fan of this programming language. It is easy to learn and very efficient to write. You can download this free plugin for Grasshopper from here. I’m going to show you two simple examples that might come in handy for you where changing the active document is useful. The first one is changing the color of a layer and the second one is changing the color of a Rhino object.

Most commands run smoothly in Python, but some commands give you an error. For instance the command LayerColor (changes a layer’s color) or ObjectColor (changes a color of an object in Rhino). This has to do with the scripting environment used as the active one within Python. The default is set to the Grasshopper scripting environment or ghdoc. Because the default is set to the Grasshopper scripting environment, one cannot access elements from the Rhino document without first changing the active document to it. There are many more useful commands you can run in Python after changing the active document, like changing an object to another layer, changing the name of a layer, adding new layers, changing the print color, the print width etc. Be sure to check them all out! This can be very useful if you would like to change the color of a lot of objects without doing it manually, changing sets of objects to specific layers or creating a set of layers with a specific color.

Example 1 – Changing the color of a Layer

Let’s get started with our first example by changing the color of our layer. First, let’s place the python component on our Grasshopper canvas. You can find the component under the Math > Script tab.
If you double click on the component the Python scripting editor will open and you’re ready to start scripting. We would have to write a simple piece of code to get us started. First, let’s import our rhinoscriptsyntax library to access the LayerColor command and also our System library to access some standard colors. Second, let’s type in rs.LayerColor. This command takes two arguments, the first one is the layer name and the second one is the color you would like to change it into. Let’s use our “Layer 01” as our layer and let’s use green as our color. This color can be found under System.Drawing.Color.Green. Now when running the definition you’ll see an error, telling you it was unable to change the layer color.

So here comes the magic trick. As you might have seen in the error message, there is a hint of changing the active document and that is exactly what we’re going to do. Let’s go back to our Python scripting editor and let’s import the Rhino library to access the Rhino document and also let’s import the scripcontext library to set the active document to. Like I mentioned before, by default the active document is set to the Grasshopper document or ghdoc. Let’s start by changing this to our Rhino document. The line of code we’d have to write is sc.doc = rh.RhinoDoc.ActiveDoc. Now if you run the code again you’ll see it ran smoothly and if you look at your “Layer 01″ in Rhino, you’ll notice the layer color got changed to green.

Example 2 – Changing the color of an object in Rhino

Let’s continue to our next example and that is changing the color of an object in Rhino. First, let’s draw a simple box in Rhino and let’s link this box into a Brep component into Grasshopper.
Before linking this component into our Python component we have to extract its GUID or Globally Unique Identifier. A GUID is a unique 128-bit string of our object and all objects in Rhino and Grasshopper have this attribute. In my case this is b74724a6-f3ab-4c89-b887-c9b8b61fa2e4. But in your case this is a different one. Every time a new object is created in Rhino or Grasshopper a new unique GUID is assigned to this object. We need this GUID to be able to tell Python “Hey, I want to change this object’s color” and for Python to know what object you’re talking about.

Let’s link in our GUID output to our x input from our Python component and let’s open the Python scripting editor again. Now this time let’s create our own custom color c. Let’s give it an RGB value of 255,30,123, just a random color. Next, let’s type in the command ObjectColor to change to color of our cube. This command, just like LayerColor takes two arguments. First, the object you would like to use and second, the color you would like to set it to. Let’s use x as the variable for our object and c as our color we previously set. And that’s it, our cube got changed into the color we set.

A final thing I would like to mention is that some other commands don’t run well when the active document is set to the Rhino document. Be sure to change the active document back to its default by adding the line sc.doc=ghdoc and putting a # in front of the sc.doc=rh.RhinoDoc.ActiveDoc. This way you can switch easily between active documents.

I hope this post was informative, good luck and go do something cool!

The post How to change the Active Document in Python for Grasshopper appeared first on Designplaygrounds.

This post first appeared on Designplaygrounds - Interactive And Generative Design, please read the originial post: here

Share the post

How to change the Active Document in Python for Grasshopper


Subscribe to Designplaygrounds - Interactive And Generative Design

Get updates delivered right to your inbox!

Thank you for your subscription