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:
The full code, including the RunScript method looks like this:
Try changing the color and intensity.