Interactive Palette Color of materials for Display Color and Layer Color

This script was conceived for use the interactive palette color of materials to edit the Display Color and Layer Color in Shaded Mode.

All work occurs in Shaded Mode or any other mods without support displaying the render materials.



How it should work.

The selected object should be switched temporarily to the Rended mode. Inside the script actions something like this: _Setobjectdisplaymode _pause “Rended”.
And necessary to create a new Custom material. Take an RGB from the current display color with the material settings matches your Shaded Mode settings. And temporarily give this material to the object.

I have default settings:

Material name = ShadedMode
Material type = Custom
Material color = Current RGB by Display Color (if the start be next time)
Gloss Finish = 50
Reflectivity = 0
Transparency = 0
Environment = 30
Map = "C:\Program Files\Rhino 7\System\rhino_shaded_mode_emap_background.png"

You may have other settings. Material settings you can see below:
Options - View - Dysplay Modes - Shadeed - Shading Settings - Custom Object Attributes Settings.
Then new Custom material will be displayed according to your Saded Mode settings.

To simulate the material that is used the Shaded Mode, is only suitable the Custom Type. I tried all the types, but only Custom has an Environment option that creates lighting in Shaded Mode. All other types are not suitable for simulating the material that use Shaded Mode.

I’ve been fought for two days to create a Custom Type, but I didn’t succeed in anything sensible. I read all the topics on the forum related to the creation the material, but didn’t find a suitable Custom Material code to be use for modifications.
The forum has several beautiful codes with attributes by @nathanletwory but it for Physically Based, Plastic and Double Sided for metals.

Nathan's code
import System
import Rhino.Display
import Rhino.Render

import scriptcontext
import random

# get object selection
object_selection = [ob for ob in scriptcontext.doc.Objects if ob.IsSelected(False)]

# only if we have a selection do the work
if object_selection:
    # create material
    render_material = Rhino.Render.RenderContent.Create(
        System.Guid("E6CD1973-B739-496E-AB69-32957FA48492"),
        Rhino.Render.RenderContent.ShowContentChooserFlags.None,
        scriptcontext.doc)
    render_material.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
    render_material.Name = "Double-Sided Material " + System.Guid.NewGuid().ToString()
    
    front_material = Rhino.Render.RenderContent.Create(
        Rhino.Render.RenderMaterial.MetalMaterialGuid,
        render_material,
        "front",
        Rhino.Render.RenderContent.ShowContentChooserFlags.None,
        scriptcontext.doc)
    
    back_material = Rhino.Render.RenderContent.Create(
        Rhino.Render.RenderMaterial.PlasterMaterialGuid,
        render_material,
        "back",
        Rhino.Render.RenderContent.ShowContentChooserFlags.None,
        scriptcontext.doc)
    random_color1 = Rhino.Display.Color4f(
        random.random(),
        random.random(),
        random.random(),
        1.0)
    random_color2 = Rhino.Display.Color4f(
        random.random(),
        random.random(),
        random.random(),
        1.0)
    # set color for front material
    front_material.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
    front_material.SetParameter("color", random_color1)
    # set color for back material
    back_material.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
    back_material.SetParameter("color", random_color2)
    render_material.EndChange()
    
    # assign
    for ob in object_selection:
        print("Adding material", render_material.Name, "to", ob)
        ob.RenderMaterial = render_material
        ob.CommitChanges()

I could not figure out how to adapt these codes for Custom material. If I change the lines:

Rhino.render.rendermaterial.plastermaterialguid 
Rhino.render.rendermaterial.metalmaterialguid
rr.Contentuuids.physicalbosedMaterialtype

to
Rhino.render.rendermaterial.createbasicmaterial

Then all the time, and always I get an error.
However, I managed to change the primitive code.

My code
import Rhino
import scriptcontext 
import rhinoscriptsyntax as rs
import random

matname = "ShadedMode"
texture = "C:\Program Files\Rhino 7\System\rhino_shaded_mode_emap_background.png"

mat = Rhino.DocObjects.Material()
mat.Name = matname
rendmat = Rhino.Render.RenderMaterial.CreateBasicMaterial(mat)
scriptcontext.doc.RenderMaterials.Add(rendmat)

If we can get the code for creating Custom material with required attributes, then the next action of the script should open the “Material Color Palette” and the user will be able to configure the color.

After the color configured and the user pressed “ok” button and Color Palette closes then the Mitch’s code comes in. This code takes the color from RGB material and gives it to the Display Color in the Object Properties or layer Color.

Code by Mitch
import rhinoscriptsyntax as rs

def ObjMatColorToObjDispColor():
    objs=rs.GetObjects("Select objects to process",preselect=True)
    if not objs: return
    
    for obj in objs:
        mat_source=rs.ObjectMaterialSource(obj)
        if mat_source==0:
            #material source is by layer
            #get the object's layer material index in the materials table
            layer=rs.ObjectLayer(obj)
            idx=rs.LayerMaterialIndex(layer)
        elif mat_source==1:
            #material source is by object
            #get the object material's index in the materials table
            idx=rs.ObjectMaterialIndex(obj)
            
        #get the color of the material by looking it up via its index
        mat_color = rs.MaterialColor(idx)
        if mat_color:
            #convert object DISPLAY color source to 'by object'
            rs.ObjectColorSource(obj,1)
            #apply the color to the object
            if mat_color: rs.ObjectColor(obj, mat_color)

ObjMatColorToObjDispColor()

Сomplete script should be possible to give the RGB to the Display Color, and return the object to the usual display mode. Inside the script actions something like this: _SetObjectDisplayMode _Pause _Mode=_UseView.

In order not to knock down the current settings and do not produce a lot of materials in the library, the script must delete a temporary material or temporary layer and return the object material that was before. Since maybe the user has already set up Render, the material assigned to the object or layer for other purposes. For example, metal or glass for the Rhino Render or other.