Is it possible to set color overlay for breps per Layout Detail viewport?

Hi there!

I am looking for a script method to assign a (random) color overlay to an object, that is only rendered in a specific layout Detail View and different per Brep.

I can’t use object Object color, print color or render material for this, because these attributes are already used for different purposes.

hope you can help me out!

cheers,
Tim

I’ve found a solution using Rhino.Display.DisplayConduit.DrawForeground

import Rhino
import scriptcontext as sc
import System
from ast import literal_eval

class CustomConduit(Rhino.Display.DisplayConduit):

    def __init__(self):
        super(CustomConduit, self).__init__()

    def DrawForeground(self, e):
        # use DrawForeground to create color overlay
        
        # only for detailviewports 
        if e.Viewport.ViewportType == Rhino.Display.ViewportType.DetailViewport:

            for item in sc.doc.ActiveDoc.Objects:
    
                try:
    
                    bb = item.Geometry.GetBoundingBox(True)
                    overlay_color = item.Attributes.GetUserString('tnm_overlay_color')
                    if not e.Viewport.IsVisible(bb) or not overlay_color:
                        continue
                        
                    # create 2 duplicates of object attributes
                    old_attr = item.Attributes.Duplicate()
                    new_attr = item.Attributes.Duplicate()                
                    
                    # apply new color to the object
                    color_tuple = literal_eval(overlay_color)
                    new_attr.ObjectColor = System.Drawing.Color.FromArgb(
                        255, *color_tuple
                    )
                    new_attr.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
                    sc.doc.Objects.ModifyAttributes(item.Id, new_attr, False)
                    
                    # draw object with new color
                    e.Display.DrawObject(item)
                    
                    # restore object color
                    sc.doc.Objects.ModifyAttributes(item.Id, old_attr, False)
                    
                    old_attr.Dispose()
                    new_attr.Dispose()
                    
                except Exception as exc:
                    print exc
            
def main():
    
    if sc.sticky.has_key('overlay'):
        # remove custom displayconduit
        conduit = sc.sticky['overlay']
        conduit.Enabled = False
        print 'conduit off'
        sc.sticky.Remove('overlay')
    else:
        # add custom displayconduit
        conduit = CustomConduit()
        conduit.Enabled = True
        sc.doc.Views.Redraw()

        sc.sticky['overlay'] = conduit
        print 'conduit on'
    
if __name__ == '__main__':
    main()