Developer: RhinoScript
Summary: Demonstrates how to modify the color of objects using RhinoScript.
I am looking for way way to change the colors of objects without using Rhino's Properties command, which tends to be slow when assigning colors to lots of objects. Also, would it be possible to assign randomized colors across multiple objects? And, it would also be nice to pick two colors and have Rhino generate all the blend colors. Is any or all of this possible?
All of this is possible with the help of RhinoScript's GetColor and ObjectColor methods.
The following sample script contains the following subroutines:
If you want to just use the script, you can just download it here.
To run the script, just extract it from the zip file onto your desktop, and then drag it on top of a running Rhino and drop it. Doing this will define three command aliases: SetObjectColor, SetObjectColorRandom, and SetObjectColorGraded.
Here is the script source code.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' [[developer:sdksamples:modifyobjectcolor|ObjectColor]].rvb -- February 2009 ' If this code works, it was written by Dale Fugier. ' If not, I don't know who wrote it. ' Works with Rhino 4.0. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit Randomize ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Sets object colors ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sub SetObjectColor() Dim objects, color objects = Rhino.GetObjects("Select objects to change colors", 0, True, True) If IsNull(objects) Then Exit Sub color = Rhino.GetColor If IsNull(color) Then Exit Sub Rhino.ObjectColor objects, color End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Set random object colors ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sub SetObjectColorRandom() Dim objects, red, green, blue, i objects = Rhino.GetObjects("Select objects for randomly color change", 0, True, True) If IsNull(objects) Then Exit Sub Rhino.EnableRedraw False For i = 0 To UBound(objects) red = Int(255 * Rnd) green = Int(255 * Rnd) blue = Int(255 * Rnd) Rhino.ObjectColor objects(i), RGB(red, green, blue) Next Rhino.EnableRedraw True End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Sets gradient object colors (based on the order picked) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sub SetObjectColorGraded() Dim objects, color0, color1, color, i, bound Dim red, red0, red1 Dim green, green0, green1 Dim blue, blue0, blue1, percent objects = Rhino.GetObjects("Select objects for gradient color change", 0, True, True) If IsNull(objects) Then Exit Sub color0 = Rhino.GetColor If IsNull(color0) Then Exit Sub color1 = Rhino.GetColor If IsNull(color1) Then Exit Sub ' Extract red-green-blue components red0 = color0 And &HFF red1 = color1 And &HFF green0 = (color0 \ &H100) And &HFF green1 = (color1 \ &H100) And &HFF blue0 = (color0 \ &H10000) And &HFF blue1 = (color1 \ &H10000) And &HFF bound = UBound(objects) Rhino.EnableRedraw False For i = 0 To bound ' A linearly interpreted gradient just calculates the new RGB values by applying a ' target value percent of the linear range to the each RGB component range. percent = i/bound red = red0 + Int(percent * (red1 - red0)) green = green0 + Int(percent * (green1 - green0)) blue = blue0 + Int(percent * (blue1 - blue0)) Rhino.ObjectColor objects(i), RGB(red, green, blue) Next Rhino.EnableRedraw True End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Rhino.AddStartupScript Rhino.LastLoadedScriptFile Rhino.AddAlias "SetObjectColor", "_NoEcho _-RunScript (SetObjectColor)" Rhino.AddAlias "SetObjectColorRandom", "_NoEcho _-RunScript (SetObjectColorRandom)" Rhino.AddAlias "SetObjectColorGraded", "_NoEcho _-RunScript (SetObjectColorGraded)"