Background color slider

Would it be possible to create a dockable color slider for the viewport background? I often have a variety of layer colors, some best viewed with a light background, some a dark background, and some in between. I have two wireframe display modes, white background and black background, but that’s not always enough.

I do a little scripting, but haven’t spent much time with the Rhino interface or application settings. If anybody can point me in the right direction for (a) how to create a dockable slider and (b) what class and/or method(s) to use to change the viewport background, I’d be quite appreciative.

Hi @ledisnomad,

this python scripts changes the background of the viewport. Check the SDK ([http://4.rhino3d.com/5/rhinocommon/?topic=html/T_Rhino_ApplicationSettings_AppearanceSettings.htm][1])
Python

import Rhino.ApplicationSettings.AppearanceSettings
import System
import scriptcontext

color = System.Drawing.Color.Pink
settings = Rhino.ApplicationSettings.AppearanceSettings
settings.ViewportBackgroundColor = color

scriptcontext.doc.Views.Redraw()


-M


  [1]: http://4.rhino3d.com/5/rhinocommon/?topic=html/T_Rhino_ApplicationSettings_AppearanceSettings.htm

Thanks, Miguel.

That answers (b) how to change the viewport background color. Now I just need direction on creating some kind of slider or interface. Then I’ll put it all together to have a dynamic background color.

you could create buttons. That would be the easiest way (https://stevebaer.wordpress.com/2011/04/11/running-scripts-from-toolbar-buttons/) to achieve what you want. Creating sliders in python its possible (http://mkmra2.blogspot.co.uk/2012/12/creating-graphical-user-interfaces-with.html)

Awesome! I’ll check these examples out. Very helpful, Miguel.

OK, Miguel. Bear with me, since I don’t use Python (I’m mostly familiar with Rhinoscript and starting to get my feet wet using VB.NET). I ran your script as a starting point. I had to add “from scriptcontext import doc”. Otherwise, it gave me a message "global name ‘doc’ is not defined.

import System
import Rhino.ApplicationSettings.AppearanceSettings
import scriptcontext
from scriptcontext import doc

def Main():
    color = System.Drawing.Color.CadetBlue
    settings = Rhino.ApplicationSettings.AppearanceSettings
    settings.ViewportBackgroundColor = color
    doc.Views.Redraw()
    
# Execute it...
if( __name__ == "__main__" ):
    Main()

Took me a bit to realize that my viewport display style needed to be set to use the document’s background color, but then it worked.

Using Meier’s UI samples, I constructed a very simple slider:

import random
import Rhino.ApplicationSettings.AppearanceSettings
import System
import scriptcontext

# 
# Background Colors
# by Damon Sidel (based on Mark Meier's UI examples)
# http://mkmra2.blogspot.com/2012/12/creating-graphical-user-interfaces-with.html
# 
def Main():
    # Make the UI object
    ui = AllControlExample()
    # Show the dialog from the UI class
    Rhino.UI.Dialogs.ShowSemiModal(ui.form)
    # Define a starting color for the background
    lightness = 127

# This is just a class to test all the UI controls
class AllControlExample():
    def __init__(self):
        # Make a new form (dialog)
        self.form = Meier_UI_Utility.UIForm("Background Color") 
        # Accumulate controls for the form
        self.addControls()
        # Layout the controls on the form
        self.form.layoutControls() 
    
    # Add each control to an accumulated list of controls
    def addControls(self):
        # The controls get added to the panel of the form
        p = self.form.panel
        
        p.addLabel("", "Brightness: ", None, False)
        p.addTrackBar("trackBar1", 0, 255, 1, 2, 1, 5, 150, False, self.trackBar1_OnValueChange)
        p.addLabel("", "Value: ", None, False)
        p.addReadonlyText("readonlyTextbox", "5", 50, True)
    
    # ====================== Delegates =====================
    # Called when the text changes in the box (every keypress)
    # Called when the value changes
    def trackBar1_OnValueChange(self, sender, e):
        try:
            c = self.form.panel.Controls.Find("readonlyTextbox", True)[0]
            c.Text = str(sender.Value)
            color = System.Drawing.Color.FromArgb(c, c, c)
            settings = Rhino.ApplicationSettings.AppearanceSettings
            settings.ViewportBackgroundColor = color
            scriptcontext.doc.Views.Redraw()
        except:
            pass

# Execute it...
if( __name__ == "__main__" ):
    Main()

In the “def trackBar1_OnValueChange” I put your code. This time, nothing happens. I’m sure I’m botching this up on a fundamental level, but like I said, bear with me.