Rhino 7 - material assignments to layers not saving

I’m using Version 7 SR10 (7.10.21256.17001, 2021-09-13) and trying to apply different materials to all the layers for exporting information easily to Twinmotion and/or Unreal Engine. On saving a file, and re-opening it, the material for the first layer in the list is applied to all the layers.

I start a file, no materials applied to the layers.

Using a Python script (see end), I create make and apply new materials to each layer, material named by the layer.

New materials in material palette:
3

I save the file, re-open it, and the material from the first layer in the list is applied to every layer.

NOT GOOD.

All material definitions are gone.
5

Python material application code:

> """USE FOR COORDINATION WITH TWINMOTION DATASMITH SYNCHRONIZATION"""
> 
> import scriptcontext as sc
> import rhinoscriptsyntax as rs
> import Rhino as rh
> import Rhino.DocObjects
> import scriptcontext
> 
> def AddMaterialToLayer(layer):
>     """Add material to a layer and returns the new material's index. If the
>     layer already has a material, then the layer's current material index is
>     returned
>     Parameters:
>       layer = name of an existing layer.
>     Returns:
>       Material index of the layer if successful
>       None if not successful or on error
>     """
> 
>     print layer
> 
>     layName = rs.LayerName(layer)
>     print layName
> 
>     layer=rs.LayerId(layName)
>     print layer
>     
>     #if rs.LayerMaterialIndex(layer)>-1: 
>     #    return rs.LayerMaterialIndex(layer)
>     
>     material_index = scriptcontext.doc.Materials.Add()
>     rs.MaterialName(material_index,layName)
>     rs.LayerMaterialIndex(layer, material_index)
>    
> lays = rs.LayerIds()
> 
> for lay in lays:
>     AddMaterialToLayer(lay)

Apparently works just fine in Rhino6.

Apparently works in Rhino7 if each material is assigned a different color.

@johnc - is this fallout from changes to consolidate identical materials?
@Mdmowry is is OK to have random colors? If so, this may help, if you have not done something similar


import scriptcontext as sc
import rhinoscriptsyntax as rs
import Rhino as rh
import Rhino.DocObjects
import scriptcontext
import random
def AddMaterialToLayer(layer, color):
    """Add material to a layer and returns the new material's index. If the
    layer already has a material, then the layer's current material index is
    returned
    Parameters:
    layer = name of an existing layer.
    Returns:
    Material index of the layer if successful
    None if not successful or on error
    """

    
    print layer
    
    layName = rs.LayerName(layer)
    print layName
    
    layer=rs.LayerId(layName)
    print layer
    
    #if rs.LayerMaterialIndex(layer)-1: 
    #    return rs.LayerMaterialIndex(layer)
    
    material_index = scriptcontext.doc.Materials.Add()
    rs.MaterialName(material_index,layName)
    rs.MaterialColor(material_index, color)
    
    rs.LayerMaterialIndex(layer, material_index)

def AddMatsTolayers():
    lays = rs.LayerIds()
    colors = []
    for lay in lays:
        while True:
            r = random.randint(0,255)
            g = random.randint(0,255)
            b = random.randint(0,255)
            color = [r,g,b]
            if color not in colors:
                break
        colors.append(color)
        print color
        AddMaterialToLayer(lay, color)
        
AddMatsTolayers()

-Pascal

-Pascal

@pascal - thanks for the reply! I could care less what the colors are so long as the materials are different, which is the only relevant thing for drag-and-drop material application in other programs which can/do cluster parts based on material name. I raised this issue because I can literally watch the layer materials change on opening the file in Rhino7. All layer materials are unique, and then…they’re all the same, and all the definitions disappear from the materials tab. I’m not familiar with the material consolidation you referenced in your question to @johnc, but it sure seemed like something is happening under Rhino’s hood that should be user-controllable. Is there a setting I’m not aware of to control this behavior with?

best -
M

@pascal I think the problem is that all the materials are being considered identical and they are being pruned down to one on loading – which is a bug because the names are different – so they are not really identical.

@Mdmowry Even though the random color workaround should solve it for now, I will investigate this and try to fix it.

https://mcneel.myjetbrains.com/youtrack/issue/RH-65772

John

2 Likes

This is fixed in the next service release (7.12).

John

1 Like