Modifying Rhino object attributes from GH

A few days ago, I posted a video which demo’ed how to dynamically edit point light objects in Grasshopper.  Grasshopper does not typically accept light objects as referenced parameters, but with a bit of scripting, Grasshopper can really edit any Rhino object.  The main way to reference objects is through their GIUD.  Everything you make in Rhino has a unique GUID generated at the time of creation.

To bring in objects which GH does not natively recognize (lights, blocks, point clouds, clipping planes, etc), drop an ID parameter on the canvas, right-click on it, and set one ID (or multiples if you like).  Let’s try this with a point light object.  Create a point light in Rhino.  Also, create some surfaces as a basic environment.

Next, bring down a C# component from MATHS > SCRIPT.  Change the ‘x’ input to ‘id’ and set the type hint to GUID.

In this scenario we will be modifying the attributes of a point light.  Let’s say we want to change the color and the intensity.  So before we get into coding, let’s continue to set up our scripting component.  Change the ‘y’ input to ‘col’ and set the type hint to ‘Color.’  Add another input, change the name to ‘pow’ and set the type hint to ‘double.’

Let’s add the inputs.  First, let’s reference the point light as I mentioned above.  Place a PARAMS > PRIMITIVE > GUID component on the canvas.  Right-Click on it, andset one Guid.  Select the point light object.  Connect the Guid parameter to the ‘id’ input of our scripting component.  Place a color swatch and number slider on the canvas, connect them to the ‘col’ and ‘pow’ inputs of the script component.

Now let’s add some code to the scripting component.  Double-click the c# icon to open up the script editor.  First we need to reference this object so we can get access to its attributes.  we do this in the following manner:

Rhino.DocObjects.ObjRef obj = new Rhino.DocObjects.ObjRef(id);
Rhino.DocObjects.LightObject lobj = obj.Object() as Rhino.DocObjects.LightObject;

The first line references the Rhino object through its Guid.  The second line further specifies the type of object.  In this case, transferring the specific light object attributes such as light color and intensity to the lobj variable.

Now, we have access to the attributes, let’s modify them:

lobj.LightGeometry.Diffuse = col;
lobj.LightGeometry.Intensity = pow;

Here we set the Diffuse color and Intensity to our inputs.  But we aren’t done just yet.  Because we have referenced the object, we have to commit the changes:

lobj.CommitChanges();

The full code, including the RunScript method looks like this:

Try changing the color and intensity.



6 Comments

  1. Petras wrote:

    How to bake these lights in grasshopper? As I get “out” and “A” as an empty output..

  2. Ilya Bourim wrote:

    Hello,
    Thanks for a great explanation. I was wondering if GH is capable of creating new lights? For example, how would you simply create a string of lights along a curve?
    Thanks you!

    • lfraguada wrote:

      Hello! Sure you can make new lights. Check out the LightTable class. You can add lights along a curve to the document (from a GH C# component) like this:


      //pts is a list of points on a curve.
      foreach(var pt in pts)
      {
      Light light = new Light();
      light.LightStyle = LightStyle.WorldPoint;
      light.Location = pt;
      light.Intensity = 0.5;
      RhinoDocument.Lights.Add(light);
      }

  3. Kaspar Krass wrote:

    Any news how to bake the output?

    • lfraguada wrote:

      Why do you need to bake the output? The lights are already in the Rhino document.

Leave a Reply