Copy print color to layer color? (python)

Does anyone happen to have a python script that will copy the print color to the layer color by chance? thank you in advance.

1 Like

@kleerkoat, do you want the script to assign the objects print color to the layer the object is on or to the layer the user picks ? Below does the latter:

import rhinoscriptsyntax as rs
    
def DoSomething():
    id = rs.GetObject("Object to get print color from", 0, True, False)
    if not id: return
    
    print_color = rs.ObjectPrintColor(id)
    if not print_color: return
    
    layer = rs.GetLayer("Layer to apply color to", None, False, False)
    if not layer: return
    
    rs.LayerColor(layer, print_color)
    
DoSomething()

c.

i’d like to just do it for every layer. it doesn’t even need to be interactive. each layer’s print color to to it’s layer color. i’m having an issue importing it into autocad and i don’t want to figure out how to get autocad to use the print color i’ve assigned in Rhino. does that make sense?

the script you did post will be of use though. thank you. :slight_smile:

Below is an example. I’ve kept the dialog to let the user choose one or more layers, to have the abillity to exclude some. To select multiple layers, just use Shift-Key:

import rhinoscriptsyntax as rs
    
def DoSomething():
    layers = rs.GetLayers("Select Layers", False)
    if not layers: return
    
    for layer in layers:
        print_color = rs.LayerPrintColor(layer)
        if print_color: 
            rs.LayerColor(layer, print_color)
    
DoSomething()

c.

2 Likes

clement you are awesome, this saved me a ton of work, thank you sooooo much!

Hi Clement,
I have a similar question, so I decided to raise this old topic.
Is it possible to copy the RGB parameters from the material color to the layer or the Object Properties (display color) by analogy of your example?
I changed your code a little. But the changed code does not work. May I ask you help with this?

import rhinoscriptsyntax as rs
    
def DoSomething():
    layers = rs.GetLayers("Select Layers", False)
    if not layers: return
    
    for layer in layers:
        Mat_color = rs.MaterialColor(layer)
        if Mat_color: 
            rs.LayerColor(layer, Mat_color)
    
DoSomething()

I took LayerMaterialIndex.
Now the RGB of the layer is always changing to black and becomes transparent.


import rhinoscriptsyntax as rs
    
def DoSomething():
    layers = rs.GetLayers("Select Layers", False)
    if not layers: return
    
    for layer in layers:
        matcolor = rs.LayerMaterialIndex(layer)
        if matcolor: 
            rs.LayerColor(layer, matcolor)
    
DoSomething()

rs.LayerMaterialIndex returns a single number which indicates the material’s place in the materials table. Rhino won’t recognize that as a color.

rs.MaterialColor wants the index of the material in the materials table, not a color.

So, you need to first get the index of the material, which allows Rhino to find the material in the table, you can then query its base color:

import rhinoscriptsyntax as rs

def LayerMatColorToLayerDispColor():
    layers = rs.GetLayers("Select Layers", False)
    if not layers: return
    
    for layer in layers:
        #get the layer material's index in the materials table
        idx=rs.LayerMaterialIndex(layer)
        #get the color of the material by looking it up via its index
        mat_color = rs.MaterialColor(idx)
        #apply the color to the layer
        if mat_color: rs.LayerColor(layer, mat_color)

LayerMatColorToLayerDispColor()
1 Like

Hi Mitch!
Thank you for responding.
Your code works great.
I think for it also needs to take the color not from the layer, but from the material of the object, and give color not the layer, but the object display color. This will be the second option for the big task in future. :slight_smile:

I began to transform your code, but as always I can’t finish.

import rhinoscriptsyntax as rs

def MatColorToDispColor():
    Material = rs.GetObject("Select Object", False)
    if not Material: return
    


MatColorToDispColor()

It’s just slightly more complicated because individual objects can have display color and material either ‘by layer’ or ‘by object’

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()
1 Like

Super!
This code works in the best way.
It’s great that there are no unnecessary dialogs. The code is automatically executed.
The problem with copy color is solved. Therefore, the next step I’ll formulate a request and open another topic.
Once again, thank you, Mitch for didn’t leave indifferent!